简体   繁体   English

无法将char *转换为WCHAR * [qt / c ++]

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

im developin QT application, and i need to include pure C code. 我开发QT应用程序,我需要包括纯C代码。 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. 当我在code :: blocks中编译这段代码时,它是成功的,也许是一个警告,但是当我尝试在QT创建器中编译它时,我得到了这4个错误。

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函数,所以我只能将类型从char更改为WCHAR,因为它不会编译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 首先我尝试将更改类型从char更改为WCHAR,但后来snprintf没有工作,所以我尝试使用swprinf,但没有成功,因为奇怪的是它没有找到这个功能

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. 如何在c ++环境中编译纯C代码而没有任何错误......或者如何进行正确的类型转换。

You are compiling in Unicode mode. 您正在以Unicode模式进行编译。 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 (ie GetSystemDirectoryA ) 发生的问题是那些Windows API函数是检查你是否正在构建Unicode的宏,然后调用函数的W或A版本(在你的代码中, GetSystemDirectory实际上是调用GetSystemDirectoryW 。所以,你可以将编译更改为多字节字符串....或者您可以显式更改您的api调用以调用A版本(即GetSystemDirectoryA

You are compiling your project with the UNICODE or _UNICODE define. 您正在使用UNICODE或_UNICODE定义编译项目。 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. 要删除定义,您可能需要禁用整个项目的unicode支持。

Change over from char to WCHAR and then to solve your swprintf problem just do this char切换到WCHAR然后解决你的swprintf问题就这样做

#define   swprintf   _snwprintf

On Windows, the prototype of swprintf is 在Windows上, swprintf的原型是

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

But the ISO C Standard requires the following prototype for swprintf 但ISO C标准要求使用以下swprintf原型

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

For this very reason, on Windows, _snwprintf is provided. 出于这个原因,在Windows上,提供了_snwprintf

Read this for more details 阅读本文了解更多详情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 wchar在c ++中使用char - wchar to char in c++ C ++错误(将wstring转换为字符串):无法将参数1从std :: wchar_t转换为std :: char - C++ error(converting wstring to string): cannot convert argument 1 from std::wchar_t to std::char C++ 将字符串(或 char*)转换为 wstring(或 wchar_t*) - C++ Convert string (or char*) to wstring (or wchar_t*) C ++ Boost错误:初始化时无法将const值类型*(aka const wchar_t *)转换为const char * - C++ boost error: cannot convert const value type* (aka const wchar_t*) to const char* in initialization 无法从'const char [3]'转换为'char *'x100000(Qt Creator C ++ Windows 32) - cannot convert from 'const char [3]' to 'char *' x100000 (Qt Creator C++ Windows 32) C ++-无法从'char []'转换为'char []' - C++ - Cannot convert from 'char []' to 'char[]' 在参数传递中无法将'const char *'转换为'WCHAR *' - Cannot convert 'const char*' to 'WCHAR*' in argument passing C ++正则表达式与char和wchar_t? - C++ regex with char and wchar_t? 为什么C / C ++自动将char / wchar_t / short / bool / enum类型转换为int? - Why does C/C++ automatically convert char/wchar_t/short/bool/enum types to int? 如何在不使用mbstowcs的情况下将ASCII char *转换为C ++中的wchar_t *? - How to convert ASCII char* to wchar_t* in C++ without using mbstowcs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM