VS2008中關於“MessageBoxW”: 不能將引數 2 從“char [20]”轉換為“LPCWSTR”的錯誤解決
問題:
1>d:\users\yangtse\documents\visual studio 2008\projects\winmain\winmain\54.cpp(27) : error C2440: “=”: 無法從“const char [14]”轉換為“LPCWSTR”
1> 與指向的型別無關;轉換要求 reinterpret_cast、C 樣式轉換或函式樣式轉換
1>d:\users\yangtse\documents\visual studio 2008\projects\winmain\winmain\54.cpp(35) : error C2664: “CreateWindowExW”: 不能將引數 2 從“const char [14]”轉換為“LPCWSTR”
1> 與指向的型別無關;轉換要求 reinterpret_cast、C 樣式轉換或函式樣式轉換
1>d:\users\yangtse\documents\visual studio 2008\projects\winmain\winmain\54.cpp(65) : error C2664: “MessageBoxW”: 不能將引數 2 從“char [20]”轉換為“LPCWSTR”
1> 與指向的型別無關;轉換要求 reinterpret_cast、C 樣式轉換或函式樣式轉換
解決:
MessageBox(hwnd,szChar,"char",0);
MessageBox 在2008中定義為 MessageBoxW W指的是寬位元組(也叫UNICODE),有3種方法可解決
①用函式MessageBoxA
②在內容前加上TEXT(對變數無效),如MessageBox(hwnd,szChar,TEXT("char"),0);
③在專案屬性->常規中,把Uicode改成多字元段。
PS:在2008中,很多函式的返回值都是寬位元組的,所以不一定要用MessageBoxA
在MSDN上可以查到用寬位元組的函式和同樣功能普通函式的名稱。
在6.0中沒用寬位元組
轉載自:http://apps.hi.baidu.com/share/detail/16474815