1
votes

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)

1
can you change string to pchar ?Passella
You can only call that function from code compiled with the same version of Delphi that built the DLL. With sharemem too. You need to fix your DLL before proceeding. What you need to understand is that the native string type is a Delphi specific type. Using WideString is good way to solve your problem.David Heffernan
I can't change dll (got this from previous developer 'as is' without source)Xitauw Enter

1 Answers

1
votes
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.