PDA

View Full Version : How to pass and get unicode strings from external dll



mvelanka
4th March 2014, 20:55
I have a very old working dll.
It has a function that takes in a unicode string.
It does some processing.
Prepares another string and returns the prepared unicode string.

I have used this dll successfully in other projects. (Including in a firefox extension using js-ctype).

Now I want to use the same dll in my current QT project. (I am new to QT)

I have done some googling and have tried some ways; however could not successfully do what I wanted to do.

Here is my function in the working dll
==========================

DECLDIR TCHAR * process_string ( TCHAR * wList)
{
wstring outStr(L"{");
....
....
//does some processing and prepares outStr
outStr.append(L"}");
.....
...... etc....

TCHAR buff [10000] = L"";

wcscpy ( buff, L"");
wcscat ( buff, outStr.c_str());
return buff;
}

========================
here is my header file
========================

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

// Specify "C" linkage to get rid of C++ name mangeling
extern "C"
{
DECLDIR TCHAR * process_string ( TCHAR * wlist);
}

=================

I request you to guide me or give me some pointers so that I can use the above dll in my QT project

Thanks in advance.

ChrisW67
5th March 2014, 00:35
It strikes me that that function only works by accident rather than design. It is returning a pointer to a destroyed stack array.

Anyway, QString has functions QString fromUtf16()/QString::utf16() and QString::fromWCharArray()/QString::toWCharArray() for dealing with these strings.

Include your header (may require INCLUDEPATH variable), link your library into your project (LIBS variable) and use the QString functions.