DLL, Ascii and freememory?

Just starting out? Need help? Post your questions and find answers here.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

DLL, Ascii and freememory?

Post 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?
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: DLL, Ascii and freememory?

Post by walbus »

Code: Select all

Global *result

ProcedureDLL FreeMem()
  FreeMemory(*result)
EndProcedure

ProcedureDLL myfunction()
  *result = Ascii("myresult!")
  ProcedureReturn *result
EndProcedure
Fred
Administrator
Administrator
Posts: 18244
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: DLL, Ascii and freememory?

Post 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.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: DLL, Ascii and freememory?

Post 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?
User avatar
mk-soft
Always Here
Always Here
Posts: 6252
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: DLL, Ascii and freememory?

Post 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)
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
Post Reply