无法将'const char *'转换为'LPCWSTR {aka const wchar

编程入门 行业动态 更新时间:2024-10-23 22:32:19
本文介绍了无法将'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 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.

您的选项是:

  • 将函数参数更改为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 of CreateFile.
  • Compile your program without UNICODE, so that the macros expand to the A 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

    本文发布于:2023-10-07 14:31:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1469640.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:转换为   char   const   aka   LPCWSTR

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!