Reputation: 41
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
Upvotes: 2
Views: 1953
Reputation: 41
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
Upvotes: 1
Reputation: 179859
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.
Upvotes: 0