anbinder
anbinder

Reputation: 55

How to translate Delphi's “array of string” param to C++?

I'm trying to use DLL that is written in Delphi in my C++ program (Visual Studio 2008). In DLL documentation function is declared as:

function ReadInfo(pRetBuffer: Pointer) : boolean;

where pRetBuffer - pointer to variable of type "array of string" where result is returned. How should I declare this parameter (array of string) in c++?

In DLL's documentation I have an example how to use this function in Delphi:

function ReadInfo(pRetBuffer: Pointer): boolean; stdcall; external 'SOME.dll'

var
   RetBuffer: array of string;
.
.
.

procedure Test();
var
   Result: Boolean;
begin
.
.
.
   Result := ReadInfo(@RetBuffer);
.
.
.
end;

Upvotes: 0

Views: 367

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

The simple answer is that you cannot call that function from C++. A Delphi array of string variable is a managed type, private to Delphi. You cannot pass one of those across an interop boundary. It's even dubious for a Delphi host to attempt to call that DLL function.

You'll need to change the function to use valid interop types.

You could use a SAFEARRAY. You could get the DLL to return a pointer to null terminated array of PChar. But you'd then need to export a deallocator, or allocate off a shared heap such as the COM heap. Do that with a BSTR and you solve the allocation problem at the same time.

Or, as you yourself suggest in the comments, you could use a Delphi DLL act as a bridge.

Upvotes: 2

Related Questions