如何从C ++调用Delphi DLL WideString参数(包括var参数) [英] How to call Delphi DLL WideString Parameters from C++ (including var parameters)

查看:79
本文介绍了如何从C ++调用Delphi DLL WideString参数(包括var参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Delphi DLL,当被delphi应用程序调用时该DLL可以工作,并导出声明为:

I have a Delphi DLL that works when called by delphi apps and exports a method declared as:

Procedure ProduceOutput(request,inputs:widestring; var ResultString:widestring);stdcall;

在C ++方面,我尝试过:

On the C++ side I have tried:

[DllImport( "ArgumentLab.dll", CallingConvention = CallingConvention.StdCall, CharSet=CharSet.WideString )];
 extern void ProduceOutput(WideString request, WideString inputs, WideString ResultString);

WideString arequest = WideString(ComboBox1->Text);
WideString ainput = "<xml> Input Text Goes Here </XML>";
WideString  aresultstring;
WideString &aresultstringpointer = aresultstring;
aresultstring = " ";
ProduceOutput(arequest, ainput, &aresultstringpointer);

Memo1->Lines->Text = aresultstring;

我的控制台错误为:

 Unit1.cpp(13): candidate function not viable: no known conversion from 'BSTR *' (aka 'wchar_t **') to 'System::WideString' for 3rd argument;

我已经使用Rad Studio XE4构建了DLL和c ++测试应用程序-它是64位DLL和APP

I have built the DLL and the c++ test app using Rad Studio XE4 - it is a 64 bit DLL and APP

我应该怎么做呢?

最诚挚的问候,

加仑

推荐答案

C ++中没有 DllImport .那是针对.NET PInvoke的.因此,将其删除.

There is no DllImport in C++. That is for .NET PInvoke instead. So remove that.

C ++函数声明的其余部分与Delphi函数声明不匹配.正确的C ++声明如下:

The remainder of your C++ function declaration does not match the Delphi function declaration. The correct C++ declaration is as follows:

void __stdcall ProduceOutput(WideString request, WideString inputs, WideString &ResultString);

别忘了静态链接到DLL的导入.LIB文件(可以根据需要使用C ++ Builder的命令行IMPLIB.EXE工具创建该文件).

Don't forget to statically link to the DLL's import .LIB file (which you can create using C++Builder's command-line IMPLIB.EXE tool, if needed).

然后,在应用程序的代码中,您可以像这样调用DLL函数:

Then, in the app's code, you can call the DLL function like this:

WideString arequest = ComboBox1->Text;
WideString ainput = "<xml> Input Text Goes Here </XML>";
WideString aresultstring;
ProduceOutput(arequest, ainput, aresultstring);

Memo1->Lines->Text = aresultstring;

出现转换错误的原因是因为 WideString 类会覆盖& 运算符,以返回指向其内部 BSTR 的指针成员.这样做的原因是允许 WideString 充当ActiveX/COM字符串的智能包装器类,例如:

The reason you are getting the conversion error is because the WideString class overrides the & operator to return a pointer to its internal BSTR member. The reason for this is to allow WideString to act like a smart wrapper class for ActiveX/COM strings, eg:

HRESULT __stdcall SomeFuncThatReturnsABStr(BSTR** Output);

WideString output;
SomeFuncThatReturnsABStr(&output);

这样,就不可能使用& 运算符获得指向WideString本身的指针.因此,获得真正的 WideString 指针的唯一方法(我知道)是动态分配 WideString ,例如:

As such, it is not possible to obtain a pointer to a WideString itself using the & operator. Because of that, the only way (that I know of) to get a real WideString pointer is to dynamically allocate the WideString, eg:

WideString *pStr = new WideString;
...
delete pStr;

这篇关于如何从C ++调用Delphi DLL WideString参数(包括var参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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