Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert char* to WCHAR* [qt/c++]

Tags:

c++

c

qt

wchar

im developin QT application, and i need to include pure C code. When i compile this code in code::blocks it was successful, maybe one warning, but when i try to compile it in QT creator, i get these 4 errors.

cannot convert 'char*' to 'WCHAR*' for argument '1' to 'UINT GetSystemDirectoryW(WCHAR*, UINT)'
cannot convert 'char*' to 'const WCHAR*' for argument '1' to 'HINSTANCE__* LoadLibraryW(const WCHAR*)'
 cannot convert 'char*' to 'WCHAR*' for argument '1' to 'BOOL 
 cannot convert 'const char*' to 'const WCHAR*' for argument '2' to 'LONG RegQueryValueExW(HKEY__*, const WCHAR*, DWORD*, DWORD*, BYTE*, DWORD*)'

and the code is here>

char systemDirectory[MAX_PATH]; 
GetSystemDirectory(systemDirectory, MAX_PATH); //first error
char kbdLayoutFilePath[MAX_PATH];
kbdLibrary = LoadLibrary(kbdLayoutFilePath); //second error
char kbdName[KL_NAMELENGTH];
GetKeyboardLayoutName(kbdName); //third error
if(RegQueryValueEx(hKey, "Layout File", NULL, &varType, layoutFile, &bufferSize) != ERROR_SUCCESS) //fourth error

i also use snprintf function, so i cant just change the type from char to WCHAR, because then it wont compile the snprintf

snprintf(kbdKeyPath, 51 + KL_NAMELENGTH,
"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", kbdName);

So do you have any ideas how to fix it ? first i tried change type from char to WCHAR, but then the snprintf didnt work, so i tried to use swprinf, but with no success, since strangely it didnt find this function

int swprintf(wchar_t *wcs, size_t maxlen,
             const wchar_t *format, ...);

but just this

int swprintf(wchar_t *wcs,
                 const wchar_t *format, ...);

so what are my option ? How to compile pure C code in c++ environment without any errors... or how to make the right type conversion.

like image 652
Anton Giertli Avatar asked Mar 19 '12 12:03

Anton Giertli


2 Answers

You are compiling in Unicode mode. You could set your compile to multi-byte strings. The problem that is happening is those windows API functions are macros that check whether you are building Unicode or not and then call either the W or A version of the function (in your code there, the GetSystemDirectory is actually calling GetSystemDirectoryW. So, you can either change your compile to multi-byte strings....or you could explicitly change your api calls to call the A version (i.e. GetSystemDirectoryA)

like image 118
pstrjds Avatar answered Oct 22 '22 06:10

pstrjds


You are compiling your project with the UNICODE or _UNICODE define. Check your project settings and remove the define if necessary. To remove the define, you might need to disable unicode support for the whole project.

like image 42
Karel Petranek Avatar answered Oct 22 '22 06:10

Karel Petranek