久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

無法將“const char*"轉換為“LPCWSTR {aka const w

cannot convert #39;const char*#39; to #39;LPCWSTR {aka const wchar_t*}#39;(無法將“const char*轉換為“LPCWSTR {aka const wchar_t*})
本文介紹了無法將“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.

您的選擇是:

  1. 將函數參數更改為 UTF-16 (const wchar_t*) 字符串.
  2. 保留 char* 參數,但讓您的函數使用 MultiByteToWideChar.
  3. 顯式調用 CreateFileA 而不是 CreateFile.
  4. 在不使用 UNICODE 的情況下編譯您的程序,以便宏默認擴展為 A 版本.
  5. 綁架一位著名的 Microsoft 開發人員并強迫他閱讀UTF-8 Everywhere,直到他同意讓 Windows 完全支持 UTF-8 作為ANSI"代碼頁,從而使各地的 Windows 開發人員擺脫這種寬字符的束縛.
  1. Change the function parameter to a UTF-16 (const wchar_t*) string.
  2. Keep the char* parameter, but have your function explicitly convert it to a UTF-16 string with a function like MultiByteToWideChar.
  3. Explicitly call CreateFileA instead of CreateFile.
  4. Compile your program without UNICODE, so that the macros expand to the A versions by default.
  5. 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*}"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 黄网免费 | av看片网| 中文字幕在线一区 | 精品久久久久久久久久久久久久 | 91社区在线观看高清 | 国产成人精品一区二区三区 | 在线观看视频福利 | 中文字幕视频一区 | 黄色日批视频 | 偷拍自拍网| 成年人在线观看 | 国产亚洲一区二区三区在线观看 | av入口 | 韩国av影院 | 毛片一级片 | 国内久久 | 亚洲区一区二 | 日韩精品一区二区三区中文字幕 | 九九热在线视频观看这里只有精品 | 国产精品视频在线观看 | 久久夜夜 | 中文字幕av在线播放 | 亚洲高清视频在线观看 | 成人h动漫亚洲一区二区 | 一级毛片观看 | 日本中文字幕在线观看 | a级黄色片视频 | 亚州影院 | 天天看天天操 | 亚洲天堂av在线 | 手机av免费在线 | 亚洲欧美综合精品久久成人 | 高清亚洲 | 日本在线精品视频 | 欧美性久久久 | 欧美日韩国产精品一区 | 国产一级特黄视频 | 91麻豆精品国产91久久久资源速度 | av在线天堂网 | 成人a视频片观看免费 | h在线播放 |