I'm not able to use the function of a dll developed in delphi. I'm having some difficulties with the conversions of types.
This is the function I want to call the DLL:
function rData(ID: Cardinal; queue: WideString): WideString; stdcall;
My code in C++ was so:
typedef string (*ReturnDataSPL)(DWORD, string);
string result;
HMODULE hLib;
hLib = LoadLibrary("delphi.dll");
pReturnDataSPL = (ReturnDataSPL)GetProcAddress(hLib,"rData");
if (NULL != pReturnDataSPL)
result = pReturnDataSPL(JobID,printerName);
The problem I'm not able to make it work. I do not know which type is compatible with Delphi WideString and Cardinal.
Someone help me
EDIT:
This is the function I want to call the DLL:
procedure rData(ID: Cardinal; queue: WideString; var Result: WideString); stdcall;
After changing the code looked like this:
typedef void (__stdcall *ReturnDataSPL)(DWORD, BSTR, BSTR&); HMODULE hLib; BSTR result = NULL; hLib = LoadLibrary("delphi.dll"); pReturnDataSPL = (ReturnDataSPL)GetProcAddress(hLib,"rData"); if (NULL != pReturnDataSPL) { pReturnDataSPL(JobID,(BSTR)"Lexmark X656de (MS) (Copiar 2)",result); }
You've got very little chance of calling that function.
For a start your current code can't hope to succeed since I presume
string
isstd::string
. That's a C++ data type which Delphi code cannot either provide or consume. To match up against Delphi'sWideString
you need to use the COMBSTR
data type.Another problem with your code as it stands is that it uses
cdecl
in the C++ side, andstdcall
on the Delphi side. You'll need to align the calling conventions.However, that will also fail because of a difference between Delphi's ABI for return values, and the platform standard. That topic was covered in detail here: Why can a WideString not be used as a function return value for interop?
Your best bet is to stop using
WideString
as a return value and convert it into a C++ reference parameter. You'll want to convert the Delphi to match.You are looking at something like this:
Delphi
C++