Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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):</p> <pre><code>function GetAString(Buffer: PChar; BufLen: Integer): Integer; stdcall; var S: String; begin S := SomeFuncThatReturnsString; Result := Min(BufLen, Length(S)); if (Buffer &lt;&gt; nil) and (Result &gt; 0) then Move(S[1], Buffer^, Result * SizeOf(Char)); end; </code></pre> <p>This allows the app to decide when and how to allocate the memory (stack versus heap, reusing memory blocks, etc):</p> <pre><code>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) &gt; 0 then GetAString(PChar(S), Length(S)); ... end; var S: array[0..255] of Char; Len: Integer; begin Len := GetAString(S, 256); ... end; </code></pre> <p>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:</p> <pre><code>function GetAString: PChar; stdcall; var S: String; begin S := SomeFuncThatReturnsString; if S &lt;&gt; '' 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 &lt;&gt; nil then try ... finally FreeAString(S); end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload