C ++使用delphi DLL [英] C++ consuming delphi DLL

查看:205
本文介绍了C ++使用delphi DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能使用Delphi开发的dll的功能。我在转换类型时遇到一些困难。



这是我要调用DLL的函数:

 函数rData(ID:Cardinal; queue:WideString):WideString; STDCALL; 

我的C ++代码如此:

  typedef string(* ReturnDataSPL)(DWORD,string); 

字符串结果;
HMODULE hLib;
hLib = LoadLibrary(delphi.dll);
pReturnDataSPL =(ReturnDataSPL)GetProcAddress(hLib,rData);
if(NULL!= pReturnDataSPL)
result = pReturnDataSPL(JobID,printerName);

问题我无法使其工作。我不知道哪种类型与Delphi WideString和Cardinal兼容。



有人帮助我


编辑:



这是我要调用DLL的函数:


$ b $程序rData(ID:Cardinal; queue:WideString; var Result:WideString); b

  STDCALL; 

更改代码如下所示:

  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);
}



解决方案

p>您几乎没有机会调用该函数。



为了开始,您当前的代码不能希望成功,因为我假设字符串 std :: string 。这是Delphi代码不能提供或消费的C ++数据类型。要匹配Delphi的 WideString ,您需要使用COM BSTR 数据类型。



代码的另一个问题是它使用C ++方面的 cdecl stdcall 在Delphi方面。您需要调用调用约定。



但是,由于Delphi的返回值的ABI与平台标准之间的区别,这也将失败。该主题在此详细介绍:为什么可以将WideString用作interop的函数返回值?



最好的办法是停止使用 WideString 作为返回值,并将其转换为C ++参考参数。您将需要将Delphi转换为匹配。



您正在查看以下内容:



Delphi

 程序rData(ID:Cardinal; queue:WideString; var Result:WideString); STDCALL; 

C ++

  typedef void(__stdcall * ReturnDataSPL)(DWORD,BSTR,BSTR&); 


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 is std::string. That's a C++ data type which Delphi code cannot either provide or consume. To match up against Delphi's WideString you need to use the COM BSTR data type.

Another problem with your code as it stands is that it uses cdecl in the C++ side, and stdcall 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

procedure rData(ID: Cardinal; queue: WideString; var Result: WideString); stdcall;

C++

typedef void (__stdcall *ReturnDataSPL)(DWORD, BSTR, BSTR&);

这篇关于C ++使用delphi DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆