error C2664: “MessageBoxW”: 不能将参数 2 从“const char *”转换为“LPCWSTR”

本文介绍在VS2005环境下使用win32application编程时遇到的MessageBox参数类型转换错误C2664,并提供了解决方案。通过替换MessageBoxW为MessageBoxA,解决了参数类型不匹配的问题。

在vs2005 的win32 application中的编程语句:

MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);

出现如题目所示的错误:
error C2664: “MessageBoxW”: 不能将参数 2 从“const char *”转换为“LPCWSTR”

其中:#define MessageBox  MessageBoxW

解决方法:

MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);

 

 

 

生成开始于 17:50... 1>------ 已启动生成: 项目: 指令, 配置: Debug x64 ------ 1> 指令.cpp 1>D:\coding\c++run\指令\指令.cpp(142,63): error C2039: "GetRecipeCommandPrefix": 不是 "VersionAdapter" 的成员 1> D:\coding\c++run\指令\指令.cpp(36,7): 1> 参见“VersionAdapter”的声明 1>D:\coding\c++run\指令\指令.cpp(142,63): error C3861: “GetRecipeCommandPrefix”: 找不到标识符 1>D:\coding\c++run\指令\指令.cpp(146,98): error C2039: "GetRecipeCommandPrefix": 不是 "VersionAdapter" 的成员 1> D:\coding\c++run\指令\指令.cpp(36,7): 1> 参见“VersionAdapter”的声明 1>D:\coding\c++run\指令\指令.cpp(146,98): error C3861: “GetRecipeCommandPrefix”: 找不到标识符 1>D:\coding\c++run\指令\指令.cpp(170,24): error C2440: “=”: 无法从“const char *”转换为“LPCWSTR” 1> D:\coding\c++run\指令\指令.cpp(170,24): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1>D:\coding\c++run\指令\指令.cpp(175,17): error C2664: “HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)”: 无法参数 2 从“const char *”转换为“LPCWSTR” 1> D:\coding\c++run\指令\指令.cpp(177,9): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(4443,1): 1> 参见“CreateWindowExW”的声明 1> D:\coding\c++run\指令\指令.cpp(175,17): 1> 尝试匹配参数列表“(int, const char *, const char [21], long, int, int, int, int, int, int, HINSTANCE, int)”时 1>D:\coding\c++run\指令\指令.cpp(197,9): error C2664: “HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)”: 无法参数 2 从“const char [7]”转换为“LPCWSTR” 1> D:\coding\c++run\指令\指令.cpp(197,9): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(4443,1): 1> 参见“CreateWindowExW”的声明 1> D:\coding\c++run\指令\指令.cpp(197,9): 1> 尝试匹配参数列表“(long, const char [7], const char [9], long, int, int, int, int, HWND, HMENU, int, int)”时 1>D:\coding\c++run\指令\指令.cpp(198,9): error C2664: “HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)”: 无法参数 2 从“const char [7]”转换为“LPCWSTR” 1> D:\coding\c++run\指令\指令.cpp(198,9): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(4443,1): 1> 参见“CreateWindowExW”的声明 1> D:\coding\c++run\指令\指令.cpp(198,9): 1> 尝试匹配参数列表“(long, const char [7], const char [9], long, int, int, int, int, HWND, HMENU, int, int)”时 1>D:\coding\c++run\指令\指令.cpp(199,9): error C2664: “HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)”: 无法参数 2 从“const char [7]”转换为“LPCWSTR” 1> D:\coding\c++run\指令\指令.cpp(199,9): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(4443,1): 1> 参见“CreateWindowExW”的声明 1> D:\coding\c++run\指令\指令.cpp(199,9): 1> 尝试匹配参数列表“(long, const char [7], const char [11], long, int, int, int, int, HWND, HMENU, int, int)”时 1>D:\coding\c++run\指令\指令.cpp(203,13): error C2664: “int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)”: 无法参数 2 从“const _Elem *”转换为“LPCWSTR” 1>D:\coding\c++run\指令\指令.cpp(203,13): error C2664: with 1>D:\coding\c++run\指令\指令.cpp(203,13): error C2664: [ 1>D:\coding\c++run\指令\指令.cpp(203,13): error C2664: _Elem=char 1>D:\coding\c++run\指令\指令.cpp(203,13): error C2664: ] 1> D:\coding\c++run\指令\指令.cpp(203,60): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(9239,1): 1> 参见“MessageBoxW”的声明 1> D:\coding\c++run\指令\指令.cpp(203,13): 1> 尝试匹配参数列表“(HWND, const _Elem *, const char [18], long)”时 1> with 1> [ 1> _Elem=char 1> ] 1>D:\coding\c++run\指令\指令.cpp(204,13): error C2664: “int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)”: 无法参数 2 从“const _Elem *”转换为“LPCWSTR” 1>D:\coding\c++run\指令\指令.cpp(204,13): error C2664: with 1>D:\coding\c++run\指令\指令.cpp(204,13): error C2664: [ 1>D:\coding\c++run\指令\指令.cpp(204,13): error C2664: _Elem=char 1>D:\coding\c++run\指令\指令.cpp(204,13): error C2664: ] 1> D:\coding\c++run\指令\指令.cpp(204,69): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(9239,1): 1> 参见“MessageBoxW”的声明 1> D:\coding\c++run\指令\指令.cpp(204,13): 1> 尝试匹配参数列表“(HWND, const _Elem *, const char [18], long)”时 1> with 1> [ 1> _Elem=char 1> ] 1>D:\coding\c++run\指令\指令.cpp(207,13): error C2664: “int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)”: 无法参数 2 从“const _Elem *”转换为“LPCWSTR” 1>D:\coding\c++run\指令\指令.cpp(207,13): error C2664: with 1>D:\coding\c++run\指令\指令.cpp(207,13): error C2664: [ 1>D:\coding\c++run\指令\指令.cpp(207,13): error C2664: _Elem=char 1>D:\coding\c++run\指令\指令.cpp(207,13): error C2664: ] 1> D:\coding\c++run\指令\指令.cpp(207,58): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(9239,1): 1> 参见“MessageBoxW”的声明 1> D:\coding\c++run\指令\指令.cpp(207,13): 1> 尝试匹配参数列表“(HWND, const _Elem *, const char [18], long)”时 1> with 1> [ 1> _Elem=char 1> ] 1>D:\coding\c++run\指令\指令.cpp(208,13): error C2664: “int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)”: 无法参数 2 从“const _Elem *”转换为“LPCWSTR” 1>D:\coding\c++run\指令\指令.cpp(208,13): error C2664: with 1>D:\coding\c++run\指令\指令.cpp(208,13): error C2664: [ 1>D:\coding\c++run\指令\指令.cpp(208,13): error C2664: _Elem=char 1>D:\coding\c++run\指令\指令.cpp(208,13): error C2664: ] 1> D:\coding\c++run\指令\指令.cpp(208,67): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(9239,1): 1> 参见“MessageBoxW”的声明 1> D:\coding\c++run\指令\指令.cpp(208,13): 1> 尝试匹配参数列表“(HWND, const _Elem *, const char [18], long)”时 1> with 1> [ 1> _Elem=char 1> ] 1>D:\coding\c++run\指令\指令.cpp(211,13): error C2664: “int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)”: 无法参数 2 从“const _Elem *”转换为“LPCWSTR” 1>D:\coding\c++run\指令\指令.cpp(211,13): error C2664: with 1>D:\coding\c++run\指令\指令.cpp(211,13): error C2664: [ 1>D:\coding\c++run\指令\指令.cpp(211,13): error C2664: _Elem=char 1>D:\coding\c++run\指令\指令.cpp(211,13): error C2664: ] 1> D:\coding\c++run\指令\指令.cpp(211,60): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(9239,1): 1> 参见“MessageBoxW”的声明 1> D:\coding\c++run\指令\指令.cpp(211,13): 1> 尝试匹配参数列表“(HWND, const _Elem *, const char [20], long)”时 1> with 1> [ 1> _Elem=char 1> ] 1>D:\coding\c++run\指令\指令.cpp(212,13): error C2664: “int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)”: 无法参数 2 从“const _Elem *”转换为“LPCWSTR” 1>D:\coding\c++run\指令\指令.cpp(212,13): error C2664: with 1>D:\coding\c++run\指令\指令.cpp(212,13): error C2664: [ 1>D:\coding\c++run\指令\指令.cpp(212,13): error C2664: _Elem=char 1>D:\coding\c++run\指令\指令.cpp(212,13): error C2664: ] 1> D:\coding\c++run\指令\指令.cpp(212,69): 1> 指向的类型不相关; 转换需要 reinterpret_cast、C 样式强制转换或带圆括号的函数样式强制转换 1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winuser.h(9239,1): 1> 参见“MessageBoxW”的声明 1> D:\coding\c++run\指令\指令.cpp(212,13): 1> 尝试匹配参数列表“(HWND, const _Elem *, const char [20], long)”时 1> with 1> [ 1> _Elem=char 1> ] ========== 生成: 0 成功,1 失败,0 最新,0 已跳过 ========== ========== 生成 于 17:50 完成,耗时 03.863 秒 ==========
最新发布
11-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值