Page 1 of 1
DLL, Ascii and freememory?
Posted: Thu Jul 13, 2017 6:20 am
by Poshu
Hello!
I'm currently writing a DLL for a software than only support ascii.
Code: Select all
ProcedureDLL myfunction()
Protected *result = Ascii("myresult!")
ProcedureReturn *result
EndProcedure
Works nicely... Except I can't figure how/when can I freememory() the buffer allocated by ascii()... Any help?
Re: DLL, Ascii and freememory?
Posted: Thu Jul 13, 2017 8:13 am
by walbus
Code: Select all
Global *result
ProcedureDLL FreeMem()
FreeMemory(*result)
EndProcedure
ProcedureDLL myfunction()
*result = Ascii("myresult!")
ProcedureReturn *result
EndProcedure
Re: DLL, Ascii and freememory?
Posted: Thu Jul 13, 2017 9:15 am
by Fred
A DLL function which return a dynamic alllocated pointer needs a counter part 'free' function which takes this pointer back and free it. You can change your API to have an 'out' pointer passed as argument, so it's the caller which is responsible of allocation and free.
Re: DLL, Ascii and freememory?
Posted: Thu Jul 13, 2017 3:36 pm
by Poshu
I should have been more precise : it's a dll for a closed source game, so I cant make the game call function it doesnt usually do.
So, solution left : use PB 5.43 or create a global *buffer for all my procedurereturns?
Re: DLL, Ascii and freememory?
Posted: Thu Jul 13, 2017 5:26 pm
by mk-soft
Same away as Windows API
Code: Select all
ProcedureDLL myfunctionA(*pResult, ByteSize)
Protected result.s, len
result = "ascii myresult!"
len = Len(result)
If *pResult And ByteSize
PokeS(*pResult, result, ByteSize, #PB_Ascii)
EndIf
ProcedureReturn len
EndProcedure
ProcedureDLL myfunctionW(*pResult, ByteSize)
Protected result.s, len
result = "unicode myresult!"
len = Len(result)
If *pResult And ByteSize
PokeS(*pResult, result, ByteSize / 2, #PB_Unicode)
EndIf
ProcedureReturn len
EndProcedure
; Test
Define len
len = myfunctionA(0,0)
*text = AllocateMemory(len + 1)
len = myfunctionA(*text, len)
Debug PeekS(*text, -1, #PB_Ascii)
FreeMemory(*text)
len = myfunctionW(0,0)
*text = AllocateMemory(len * 2 + 2)
len = myfunctionW(*text, len * 2)
Debug PeekS(*text, -1, #PB_Unicode)
FreeMemory(*text)