Qt 和 VS C++ dll函数返回值
我正在尝试从 Qt 中连接的 DLL 获取 std::string/std::wstring 返回值,但我遇到了问题。
来自 DLL 的代码:
using namespace std;
extern "C++" __declspec(dllexport) string test()
{
return "Passed!";
}
我的 Qt 应用程序(Qt Creator)中的代码:
typedef std::string (*Test)();
QLibrary *lib = new QLibrary("dllname");
lib->load();
.... dll load check ....
Test test = (Test) lib->resolve("test");
std::string s = test();
QString name = QString::fromStdString(s);
结果“name”变量将包含“H”而不是“Passed!” 我做错了什么?
提前致谢
I'm trying to get std::string/std::wstring returned value from connected DLL in Qt and I having problem with this.
code from DLL:
using namespace std;
extern "C++" __declspec(dllexport) string test()
{
return "Passed!";
}
code in my Qt application (Qt Creator):
typedef std::string (*Test)();
QLibrary *lib = new QLibrary("dllname");
lib->load();
.... dll load check ....
Test test = (Test) lib->resolve("test");
std::string s = test();
QString name = QString::fromStdString(s);
In result "name" variable will have "H" insted of "Passed!"
What I'm doing wrong?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您的评论,我已经做到了:
Qt 端:
变量“名称”现在应该是“通过!”
在 QLibrary 类参考中提到了仅支持
指令。
已更新谢谢@MSalters
Thanks for your comments, I've made it:
Qt side:
Variable "name" now should be "Passed!"
In QLibrary class reference says about support only
directive.
UPDATED Thanks @MSalters
问题是
extern "C++"
函数的名称被破坏了。这允许超载。extern "C"
函数不能重载。QLibrary
无法处理重载,也无法处理名称修改。因此它需要extern "C"
函数。然而,这些可能仍然使用C++类型。如果失败,您将得到“未定义的行为”。你运气不好,如果直接摔下来就更好了。
The problem is that
extern "C++"
functions have their name mangled. This allows overloading.extern "C"
functions cannot be overloaded.QLibrary
cannot deal with overloading, nor with name mangling. It therefore needsextern "C"
functions. However, these may still use C++ types.If you fail, you will get Undefined Behavior. You're unlucky, it would have been better if it would just have crashed.