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.

有帮助吗?

解决方案

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)

其他提示

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.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top