Qstring for LPCWSTR

LPCWSTR path; void WinApiLibrary::StartProcess(QString name) { path = name.utf16(); CreateProcess(path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); } 

C: \ kursovaya \ smc \ winapilibrary.cpp: 21: error: invalid conversion from 'const ushort * {aka const short unsigned int *}' to 'LPCWSTR {aka const wchar_t *}' [-fpermissive] path = name.utf16 ();

This code worked in Qt 4.8, but now I have Qt 5.2, and this code does not work. What happened to this guy?

+8
qt
source share
3 answers

QString::utf16() returns const ushort* , which is different from const wchar_t* , so you have a compilation error.

You are probably compiling with /Zc:wchar_t . If you change it to /Zc:wchar_t- , it should work, since in this case the type wchar_t becomes typedef to a 16-bit integer.

In Visual Studio: Project Properties โ†’ Configuration Properties โ†’ C / C ++ โ†’ Treat WChar_t as an Inline Type โ†’ None.

Or just add reinterpret_cast<LPCWSTR> .

+4
source share

I had the same problem (I am using Qt 5.3), here is how I fixed it:

 QString strVariable1; LPCWSTR strVariable2 = (const wchar_t*) strVariable1.utf16(); 
+9
source share

I am using Qt 5.2 and I had the same problem. Here is how I fixed it:

 QString sPath = "C:\\Program File\\MyProg"; wchar_t wcPath[1024]; int iLen = sPath.toWCharArray(wcPath); 
0
source share

All Articles