Add an Example of Returning A String from a DLL

Found an issue in the documentation ? Please report it here !

Moderator: Documentation Editors

swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Add an Example of Returning A String from a DLL

Post by swhite »

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
Simon White
dCipher Computing
User avatar
mk-soft
Always Here
Always Here
Posts: 5382
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Add an Example of Returning A String from a DLL

Post by mk-soft »

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
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Add an Example of Returning A String from a DLL

Post by swhite »

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.
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
Simon White
dCipher Computing
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Add an Example of Returning A String from a DLL

Post by swhite »

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
Simon White
dCipher Computing
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Add an Example of Returning A String from a DLL

Post by Paul »

swhite wrote: Sun Feb 25, 2024 3:56 am 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.
As mk-soft pointed out, the docs already state that you will get a pointer to the return string.
Image Image
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2057
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Add an Example of Returning A String from a DLL

Post by Andre »

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... :oops:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply