Xitauw Enter
Xitauw Enter

Reputation: 13

Passing var string from c++ to Delphi dll

I'm can't use the function of a dll in delphi. I'm having some problems with the conversions of types.

This is the function I want to call the Delphi DLL:

function SyncFunc(var Type:string; var MaxUsers:integer; var ErrCode :Word):boolean;

C++ code:

unsigned char(WINAPI    *SyncFunc)(PCHAR Type, INT *MaxUsers, WORD *ErrCode);

HMODULE hLib;
BOOL Res = FALSE;
WORD ErrCode = 0;
INT MaxUsers = 0;
CHAR Type[256];
hLib = LoadLibrary("delphi.dll");
Res = SyncFunc(Type, &MaxUsers, &ErrCode);

Someone please help. P.S. similar question here C++ consuming delphi DLL (but my function uses string, not WideString)

Upvotes: 1

Views: 1003

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

function SyncFunc(var Type:string; var MaxUsers:integer; var ErrCode :Word):boolean;

There are two problems facing you here:

  1. string is a native Delphi type that can only be created and consumed by Embarcadero tools. Further more, since it uses the Delphi runtime heap, ShareMem or similar must be used.
  2. The function uses the default register calling convention which again is only available using Embarcadero tools.

The obvious way forward is to fix the DLL and arrange for it to use standard platform interop types and calling conventions. You might use BSTR (WideString in Delphi) for strings, and stdcall as the calling convention.

If you cannot change the DLL then your only hope is to write an adapter DLL using the same compiler as was used to compile this errant DLL. But that can only work if the errant DLL was compiled using a shared memory manager. If that is not the case then your task is next to impossible.

Upvotes: 1

Related Questions