Pre-allocate memory between HostApp and DLL
- by Leo
I have a DLL which provided a decoding function, as follows:
function MyDecode (Source: PChar; SourceLen: Integer; var Dest: PChar; DestLen: Integer): Boolean; stdcall;
The HostApp call "MyDecode", and transfer into the Source, SourceLen and Dest parameters, the DLL returns decoded Dest and DestLen.
The problem is: The HostApp impossible to know decoded Dest length, and therefore would not know how to pre-allocated Dest's memory.
I know that can split "MyDecode" into two functions:
function GetDecodeLen (Source: PChar; SourceLen: Integer): Integer; stdcall; // Return the Dest's length
function MyDecodeLen (Source: PChar; SourceLen: Integer; var Dest: PChar): Boolean; stdcall;
But, My decoding process is very complicated, so if split into two functions will affect the efficiency.
Is there a better solution?