Visual StudioE0167

x huang 20 Reputation points
2026-07-15T05:59:44.25+00:00

打代码时出现‘"const char *" 类型的实参与 "LPCWSTR" (aka "const WCHAR *") 类型的形参不兼容’,👇我的代码出错段>


#include<Windows.h>
using namespace std;
void wuxiantanchuang(){
    while (true) {
		MessageBox(NULL, "error", "error!", MB_OK);
    }
}

Windows for business | Windows 365 Business
0 comments No comments

Answer accepted by question author
Chen Tran 13,030 Reputation points Independent Advisor
2026-07-15T06:49:41.7933333+00:00

你好 Huang,

根據錯誤描述,嗯! MessageBox 實際上是一個巨集。啟用 Unicode 後,它會解析為 MessageBoxW(「寬」版本),該版本需要類型為 const wchar_t*(或 LPCWSTR)的寬字元字串。但是,你的程式碼傳遞的是標準的 ANSI/窄字串(“error” 和 “error!”),它們的型別是 const char*。

由於你的專案已經配置為支援 Unicode,建議的做法是將字串字面量標記為「寬」字串,方法是在字串前加上 L,例如 MessageBox(NULL, L"error", L"error!", MB_OK),而不是 MessageBox(NULL, "error", "error!", MB_OK)。

如果你仍然想要使用標準的窄字串 (char*),你可以完全繞過 MessageBox 宏,直接呼叫 ANSI 版本的函數 MessageBoxA,例如 MessageBoxA(NULL, "error", "error!", MB_OK)。而不是 MessageBox(NULL, "error", "error!", MB_OK);

希望以上資訊對你有幫助!如果解決了你的問題,請點擊“接受答案”,這樣我就知道它確實解決了你的問題。

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Newest

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.