Pre-allocate memory between HostApp and DLL

Posted by Leo on Stack Overflow See other posts from Stack Overflow or by Leo
Published on 2010-04-12T09:57:52Z Indexed on 2010/04/12 10:03 UTC
Read the original article Hit count: 374

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about dll