gpt4 book ai didi

c++ - 如何从C++调用Delphi DLL WideString参数(包括var参数)

转载 作者:行者123 更新时间:2023-12-02 10:31:14 25 4
gpt4 key购买 nike

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

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

在C++方面,我尝试过:
[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

我应该怎么做呢?

最好的祝福,

加里

最佳答案

C++中没有DllImport。那是针对.NET PInvoke的。所以删除它。

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

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

不要忘记静态链接到DLL的导入.LIB文件(如果需要,可以使用C++ Builder的命令行IMPLIB.EXE工具创建该文件)。

然后,在应用程序的代码中,您可以像这样调用DLL函数:
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字符串的智能包装器类,例如:
HRESULT __stdcall SomeFuncThatReturnsABStr(BSTR** Output);
WideString output;
SomeFuncThatReturnsABStr(&output);

因此,无法使用 &运算符获得指向WideString本身的指针。因此,获得真正的 WideString指针的唯一方法(我知道)是动态分配 WideString,例如:
WideString *pStr = new WideString;
...
delete pStr;

关于c++ - 如何从C++调用Delphi DLL WideString参数(包括var参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com