cannot convert #39;const char*#39; to #39;LPCWSTR {aka const wchar_t*}#39;(无法将“const char*转换为“LPCWSTR {aka const wchar_t*})
问题描述
我在我的 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 typeconst char*
CreateFileW
, which takes a file path of typeconst 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.
您的选择是:
- 将函数参数更改为 UTF-16 (
const wchar_t*
) 字符串. - 保留
char*
参数,但让您的函数使用 MultiByteToWideChar. - 显式调用
CreateFileA
而不是CreateFile
. - 在不使用
UNICODE
的情况下编译您的程序,以便宏默认扩展为A
版本. - 绑架一位著名的 Microsoft 开发人员并强迫他阅读UTF-8 Everywhere,直到他同意让 Windows 完全支持 UTF-8 作为ANSI"代码页,从而使各地的 Windows 开发人员摆脱这种宽字符的束缚.
- Change the function parameter to a UTF-16 (
const wchar_t*
) string. - Keep the
char*
parameter, but have your function explicitly convert it to a UTF-16 string with a function like MultiByteToWideChar. - Explicitly call
CreateFileA
instead ofCreateFile
. - Compile your program without
UNICODE
, so that the macros expand to theA
versions by default. - Kidnap a prominent Microsoft developer and force him to read UTF-8 Everywhere until he agrees to have Windows fully support UTF-8 as an "ANSI" code page, thus freeing Windows developers everywhere from this wide-character stuff.
我不知道是否涉及绑架,但 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_t*}"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法将“const char*"转换为“LPCWSTR {aka const w


基础教程推荐
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01