如何从C++调用Delphi DLL WideString参数(包括var参数)
How to call Delphi DLL WideString Parameters from C++ (including var parameters)
我有一个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和应用程序
我该怎么做呢?
致问候,
garry
C++中没有DllImport
。这是针对.NET PInvoke的。所以把它去掉。
C++函数声明的其余部分与Delphi函数声明不匹配。正确的C++声明如下:
void __stdcall ProduceOutput(WideString request, WideString inputs, WideString &ResultString);
不要忘记静态链接到DLL的import.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++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 包括一个类来定义全局变量参数 c++ :(
- 如何查看完整的CMD执行命令,包括参数和标志?
- 使用模板参数还包括 constexpr 成员函数enable_if单独定义和声明模板成员函数
- 包括一个大标头以使用对象作为默认参数
- CreateProcess正在调用cmd.exe,包括没有显示(闪烁)窗口的参数
- 不完整的参数错误,包括其他.cpp文件中的postgres.cpp工作文件
- 如何从C++调用Delphi DLL WideString参数(包括var参数)
- 函数类型和参数,包括它们作为参数的类型
- 在c++ 11中,是否有一种方法可以在调用以任何可调用对象(包括绑定方法)作为参数的函数时不需要模板参数
- 函数声明应该包括参数名吗?
- 试图编译一个c++代码与根(Cern)参数包括