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

3

6

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.

Usherette answered 19/3, 2012 at 12:58 Comment(0)
G
4

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)

Genie answered 19/3, 2012 at 13:5 Comment(1)
This seems like a working solution but i still have one error in this function, here is the prototype int getKeyboardLayoutFile(char* layoutFile, DWORD bufferSize) , and here is the function call that gives error >> if(RegQueryValueExA(hKey, "Layout File", NULL, &varType, layoutFile, &bufferSize) != ERROR_SUCCESS), and the error is " error: invalid conversion from 'char*' to 'BYTE*'", "error: initializing argument 5 of 'LONG RegQueryValueExA(HKEY__, const CHAR, DWORD*, DWORD*, BYTE*, DWORD*)'"Usherette
R
2

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.

Robison answered 19/3, 2012 at 13:6 Comment(0)
G
0

Change over from char to WCHAR and then to solve your swprintf problem just do this

#define   swprintf   _snwprintf

On Windows, the prototype of swprintf is

int swprintf( wchar_t *buffer,const wchar_t *format [,argument] ... );

But the ISO C Standard requires the following prototype for swprintf

int swprintf (wchar_t *, size_t, const wchar_t *, ...);

For this very reason, on Windows, _snwprintf is provided.

Read this for more details

http://msdn.microsoft.com/en-us/library/ybk95axf(v=vs.71).aspx

Gourley answered 19/3, 2012 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.