[英]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.