Hi
I read through the documentation about creating a DLL and the only condition it mentioned was that the string must be declared as Global but it does not mention that the return value is a pointer to the string. So It would be very helpful if there was an example of returning a string from a DLL.
Thanks,
Simon
Add an Example of Returning A String from a DLL
Moderator: Documentation Editors
Add an Example of Returning A String from a DLL
Simon White
dCipher Computing
dCipher Computing
Re: Add an Example of Returning A String from a DLL
See PB-Help 'Building a DLL' -> https://www.purebasic.com/documentation ... e/dll.html
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Add an Example of Returning A String from a DLL
Hi
I read the documentation before posting this suggestion. You cannot just return a string as shown in the example below. You have to return the pointer to the string.
Here is a working example of returning an encrypted string. I use this with Visual FoxPro and it works correctly by using the returned pointer to get the string value.
Simon
I read the documentation before posting this suggestion. You cannot just return a string as shown in the example below. You have to return the pointer to the string.
Global ReturnString$
ProcedureDLL.s MyFunction(var.s)
ReturnString$ = var + " test"
ProcedureReturn ReturnString$
EndProcedure
Here is a working example of returning an encrypted string. I use this with Visual FoxPro and it works correctly by using the returned pointer to get the string value.
Code: Select all
;
; AES Encryption key and Initialization Vector
;
DataSection
Key:
Data.b $25, $64, $24, $47, $7D, $BC, $80, $BF, $2B, $7A, $72, $2A, $29, $41, $6E, $2B, $4B, $23, $5E, $75, $65, $34, $3D, $70, $43, $3F, $3B, $49, $6A, $36, $51, $30
IV:
Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
EndDataSection
Global *Buffer1,*Buffer2
ProcedureDLL DetachProcess(tInstance)
If *Buffer1
FreeMemory(*Buffer1)
EndIf
If *Buffer2
FreeMemory(*Buffer2)
EndIf
EndProcedure
ProcedureDLL Encrypt(tcTxt.s)
Define lnLen.i
If Len(tcTxt) < 16
tcTxt = LSet(tcTxt,16," ")
EndIf
lnLen = StringByteLength(tcTxt)
*Buffer1 = AllocateMemory(lnLen+SizeOf(Character))
*Buffer2 = AllocateMemory(MemorySize(*Buffer1)*2)
AESEncoder(@tcTxt,*Buffer1,lnLen,?Key,256,?IV,#PB_Cipher_CBC)
lnLen = Base64EncoderBuffer(*Buffer1,lnLen,*Buffer2,MemorySize(*Buffer2))
; If CreateFile(1,"PBEncrypt.txt",#PB_Ascii)
; WriteString(1,PeekS(*Buffer2,lnLen,#PB_Ascii))
; CloseFile(1)
; EndIf
ProcedureReturn *Buffer2
EndProcedure
;Debug PeekS(Encrypt("782502000000000001"),-1)
Simon White
dCipher Computing
dCipher Computing
Re: Add an Example of Returning A String from a DLL
Hi
After more experimenting I discovered that you can return a string or a pointer to a memory buffer. If I return a string I still must treat the return value as a pointer and convert the string from Unicode to UTF8 in Visual Foxpro. If I use the a memory buffer I do not have to convert the result to UTF8.
So the only change in the documentation is to point out the return value will still be a pointer even if you choose to return a string as the documentation shows.
Simon
After more experimenting I discovered that you can return a string or a pointer to a memory buffer. If I return a string I still must treat the return value as a pointer and convert the string from Unicode to UTF8 in Visual Foxpro. If I use the a memory buffer I do not have to convert the result to UTF8.
So the only change in the documentation is to point out the return value will still be a pointer even if you choose to return a string as the documentation shows.
Simon
Simon White
dCipher Computing
dCipher Computing
Re: Add an Example of Returning A String from a DLL
As mk-soft pointed out, the docs already state that you will get a pointer to the return string.
- Andre
- PureBasic Team
- Posts: 2071
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Add an Example of Returning A String from a DLL
Anyone with a concrete suggestion for another sentence at 'Remarks when returning a string from DLL"?
I just checked the (german) docs, and there couldn't be found a remark about pointers etc...
It's not a topic I'm familiar with, so I'm asking here...
I just checked the (german) docs, and there couldn't be found a remark about pointers etc...
It's not a topic I'm familiar with, so I'm asking here...