Call Delphi DLL from C++\CLI with many parameters
I have Delphi 2010 built DLL with two methods:
function Foo1(a, b: Integer):PChar; export; stdcall;
function Foo2(a, b, c:Integer):PChar; export; stdcall;
exports Foo1, Foo2;
Each of them returns Result := PChar('Test')
.
My C++CLI code
in header
typedef const wchar_t* (*pFUNC1)(int a, int b);
pFUNC1 TestFoo1;
typedef const wchar_t* (*pFUNC2)(int a, int b, int c);
pFUNC2 TestFoo2;
Initialize by LoadLibrary
and GetProcAddress
functions. Usage: TestFoo1(0,0)
and TestFoo2(0,0,0)
;
Both works in Release mode.
But in Debug mode Foo2 is being aborted.
Please advise what is wrong.
Most likely you have calling convention mismatch. Change the stdcall
in the Delphi to cdecl
to match your C++/CLI code.
As an aside, you will need to be careful with the lifetime of your strings if ever you attempt to return a value from the DLL that is not a literal stored in read-only memory in the data segment. But that's not the problem here because PChar('Test')
has the same lifetime as the DLL.