11

I have a DLL in which I have a function that returns a pchar. (as to avoid having to use borlndmm) What I was doing originally was casting a string as a pchar and returning that

Result := pChar(SomeFuncThatReturnsString)

But I was getting expected results 90% of the time and the other times I would get back nothing.

I then got to thinking that I needed to allocate the memory for the pchar and that doing it my original way was having a pchar point to memory that was not always going to be what was there when the function was called originally. So I now have this

Result := StrAlloc(128);
Strcopy(Result,PAnsiChar(Hash(Hash(Code,1,128),2,128)));

But this leaves me with having to clean up the allocated memory on the programs end which I do with

StrDispose(Pstr);    

So the $64 question is: Do I have to allocate memory when returning a PChar from a function inside a DLL or can I just cast it to a PChar?

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
Tim
  • 1,549
  • 1
  • 20
  • 37

3 Answers3

11

The typical approach to this issue is to have the app allocate the memory and then pass it to the DLL to fill in (even better if the DLL allows the app to query how much memory it needs to allocate so it does not have to over-allocate memory):

function GetAString(Buffer: PChar; BufLen: Integer): Integer; stdcall;
var
  S: String;
begin
  S := SomeFuncThatReturnsString;
  Result := Min(BufLen, Length(S));
  if (Buffer <> nil) and (Result > 0) then
    Move(S[1], Buffer^, Result * SizeOf(Char));
end;

This allows the app to decide when and how to allocate the memory (stack versus heap, reusing memory blocks, etc):

var
  S: String;
begin
  SetLength(S, 256);
  SetLength(S, GetAString(PChar(S), 256));
  ...
end;

var
  S: String;
begin
  SetLength(S, GetAString(nil, 0));
  if Length(S) > 0 then GetAString(PChar(S), Length(S));
  ...
end;

var
  S: array[0..255] of Char;
  Len: Integer;
begin
  Len := GetAString(S, 256);
  ...
end;

If this is not an option for you, then you need to have the DLL allocate the memory, return it to the app for use, and then have the DLL export an additional function that the app can call when it is done to pass the pointer back to the DLL for freeing:

function GetAString: PChar; stdcall;
var
  S: String;
begin
  S := SomeFuncThatReturnsString;
  if S <> '' then
  begin
    Result := StrAlloc(Length(S)+1);
    StrPCopy(Result, S);
  end else
    Result := nil;
end;

procedure FreeAString(AStr: PChar); stdcall;
begin
  StrDispose(AStr);
end;

var
  S: PChar;
begin
  S := GetAString;
  if S <> nil then
  try
    ...
  finally
    FreeAString(S);
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • exposing shutdown function is so Windows 3.1 just do of all cleanup in DllMain when fdwReason = DLL_PROCESS_DETACH – Free Consulting Nov 24 '10 at 23:46
  • What does that have to do with my answer? Besides, cleanup is not always possible in DllMain(), there are some operations that cannot be safely performed inside of DllMain() directly. – Remy Lebeau Nov 25 '10 at 00:54
  • "The WEP (Windows exit procedure) callback function performs cleanup for a dynamic-link library (DLL) before the library is unloaded." isnt it what you suggested? re: unsafe operations - care to give example(s)? – Free Consulting Nov 25 '10 at 03:48
  • 1
    WEP only applies to 16-bit DLLs. DllMain() replaced WEP in 32-bit. As for limitations, read MSDN's documentation. – Remy Lebeau Nov 26 '10 at 07:52
5

DLL and your main app have two different memory managers, so it is incorrect to allocate memory in DLL but free it in main app and vice versa.

You can use WideString type for returning string from dll or passing it to dll — WideString is a wrapper around system BSTR type and memory for WideString variables is allocated automatically by system memory manager.

Another solution is to use SimpleShareMem instead of ShareMem (Delphi 2007 and older only) — it works like ShareMem but does not need any borlnmm.dll-like libraries to redistribute.

1

When you return a string as PChar from function the string is held in stack, which is why it's sometimes corrupted. I use process heap memory for returning strings, or pointer to global buffer array of chars.

Also you can use built-in assembler and do this:

Function GetNameStr : PChar;
Asm
  Call   @OverText
  DB     'Some text',0
@OverText:
  Pop    EAX
End;
BoB
  • 71
  • 4