我在 C++ 代码中遇到了一个我无法理解的错误.剥离的代码位在这里:
I'm getting an error in my C++ code that I can't quite make sense of. The stripped down code bits are here:
RS232Handle=OpenRS232("COM1", 9600); HANDLE OpenRS232(const char* ComName, DWORD BaudRate) { ComHandle=CreateFile(ComName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); }我收到以下错误:
error: cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'void* CreateFileW(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE)' ComHandle=CreateFile(ComName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);代码取自 VS 代码,我现在正在使用 Qt creator.
The code was taken from VS code and I am now using Qt creator.
我该如何解决这个问题?谢谢!
How can I fix this issue? Thanks!
推荐答案Windows CreateFile 函数实际上是一个宏,扩展为以下之一:
The Windows CreateFile function is actually a macro that expands to one of:
- CreateFileA,采用 const char* 类型的文件路径
- CreateFileW,采用 const wchar_t* 类型的文件路径.
- CreateFileA, which takes a file path of type const char*
- CreateFileW, which takes a file path of type const wchar_t*.
(Windows API 中大多数接受字符串的函数也是如此.)
(The same is true for most of the functions in the Windows API that take a string.)
您正在声明参数 const char* ComName,但显然在编译时定义了 UNICODE,因此它调用了 W 版本的功能.没有从 const wchar_t* 到 const char* 的自动转换,因此出现错误.
You're declaring the parameter const char* ComName, but apparently compiling with UNICODE defined, so it's calling the W version of the function. There's no automatic conversion from const wchar_t* to const char*, hence the error.
您的选择是:
我不知道是否涉及绑架,但 Windows 10 1903 终于添加了支持 用于 UTF-8 作为 ANSI 代码页.
I don't know if a kidnapping was involved, but Windows 10 1903 finally added support for UTF-8 as an ANSI code page.
更多推荐
无法将 'const char*' 转换为 'LPCWSTR {aka const wchar
发布评论