The DLL function should look like this:
procedure Test(Buffer: Pointer; Length: Integer); stdcall; begin //Buffer points to the start of the buffer. //The Buffer size if Length bytes. end;
Assuming you are calling it from another Delphi module, the call might look like this:
procedure Test(Buffer: Pointer; Length: Integer); stdcall; external 'test.dll'; procedure CallTest; var Buffer: array of Byte; begin SetLength(Buffer, 1000);
It is always advisable to define an interface that requires memory to be allocated and freed in the same module.
In the above example, it is allocated and deallocated in the caller module. This means that the Test method must either fully process the Buffer before returning, or return a copy of the contents of the Buffer before returning.
Now, although there is the possibility of distribution and release in the called party module, this is less common. This is less common because it is usually less convenient to do so. It often entails more API functions, or perhaps a more complex interface. You will be redirected to the distribution route of the called party when the calling party cannot determine the appropriate size for the buffer.
When data is transferred from the caller to the callee, than allocating the caller is always a better choice. When data moves in the other direction, it is more likely that the allocation of the called party will be appropriate.
source share