Qt 和 VS C++ dll函数返回值

发布于 2024-12-13 03:43:37 字数 551 浏览 4 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星光不落少年眉 2024-12-20 03:43:37

感谢您的评论,我已经做到了:

extern "C" __declspec(dllexport) int test(wchar_t* out)
{
    wcscpy_s(out, MAX_PATH, L"Passed!"); // I'm using sys paths in my app, 
                                           // so MAX_PATH is ok for me
    return 0;
}

Qt 端:

typedef int (*Test)(wchar_t*);
QLibrary *lib = new QLibrary("dllname");
lib->load();
.... dll load check ....
Test test = (Test) lib->resolve("test");
wchar_t s[MAX_PATH];
test(s);
QString name = QString::fromWCharArray(s);

变量“名称”现在应该是“通过!”

在 QLibrary 类参考中提到了仅支持

extern "C" __declspec(dllexport)

指令。

已更新谢谢@MSalters

Thanks for your comments, I've made it:

extern "C" __declspec(dllexport) int test(wchar_t* out)
{
    wcscpy_s(out, MAX_PATH, L"Passed!"); // I'm using sys paths in my app, 
                                           // so MAX_PATH is ok for me
    return 0;
}

Qt side:

typedef int (*Test)(wchar_t*);
QLibrary *lib = new QLibrary("dllname");
lib->load();
.... dll load check ....
Test test = (Test) lib->resolve("test");
wchar_t s[MAX_PATH];
test(s);
QString name = QString::fromWCharArray(s);

Variable "name" now should be "Passed!"

In QLibrary class reference says about support only

extern "C" __declspec(dllexport)

directive.

UPDATED Thanks @MSalters

决绝 2024-12-20 03:43:37

问题是 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 needs extern "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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文