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

im developing a QT application, and I need to include clean C code. When I compile this code in code :: blocks, it was successful, perhaps with one warning, but when I try to compile it in the 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 the snprintf function, so I cannot just change the type from char to WCHAR, because then it will not compile snprintf

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

Do you have any idea how to fix this? At first I tried to change the type from char to WCHAR, but then snprintf did not work, so I tried to use swprinf, but without success, as it is strange he did not find this function

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

but only that

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

So what is my option? How to compile pure C code in a C ++ environment without any errors ... or how to make the correct type conversion.

+4
source share
3 answers

You compile in Unicode mode. You can configure compilation to multibyte strings. The problem is that the window API functions are macros that check whether you build Unicode or not, and then call either W or the version of the function (in your code there, GetSystemDirectory actually calls GetSystemDirectoryW . That way you can either change compilation to multibyte strings .... or you can explicitly change your api calls to call version A (i.e. GetSystemDirectoryA )

+4
source

You are compiling your project with a UNICODE or _UNICODE definition. Check the project settings and delete the definition if necessary. To remove a definition, you may need to disable Unicode support for the entire project.

+2
source

Go 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 for the ISO C standard, the following prototype for swprintf

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

For this reason, _snwprintf provided on Windows.

Read this for more details.

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

0
source

Source: https://habr.com/ru/post/1402227/


All Articles