[fpc-pascal] fp libraries do not like cmem ?

DaWorm daworm at gmail.com
Fri Feb 14 23:14:08 CET 2014


Or always allocate and free the buffer in the calling program and load the
contents into the buffer in the dll.  This way you never have to worry
about allocating in one place and freeing in the other.  Windows API does
stuff like this a lot.

In the DLL:

Procedure GetSomeString(StringPointer: Pointer, Var StringLen: Integer);
Begin
  StringLen = CalculateSizeOfString
  if StringPointer != nil then
    // copy string to StringPointer
End;

In your App:

Var StringLen: Integer;
AllocedMem : Pointer;
. . .
// Find out how much memory needed
GetSomeString(nil, &StringLen);
GetMem(AllocedMem, StringLen);
GetSomeString(AllocedMem, &StringLen);
// use the string for whatever you need
. . .
FreeMem(AllocedMem);

Very bad psuedo code there, but it should give you the idea.

To summarize, your app needs to:

1. Call once with nil to get how much memory needed
2. Allocate the memory
3. Call again with pointer to memory
4. Use the allocated memory
5. Free the allocated memory

And your dll function needs to:

1. Check pointer for nil
2. If so, only set the length parameter to the size of memory needed and
exit
3. If not, copy data to pointer as well

Jeff.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140214/a7c5ef49/attachment.html>


More information about the fpc-pascal mailing list