visual-studio :错误 C2664:“MessageBoxW”:无法从“const char [40]”转换参数 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2312802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:34:45  来源:igfitidea点击:

: error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [40]'

c++cvisual-studiovisual-studio-2008

提问by numerical25

I am reading a book and It told me to open a empty WIN32 project. I created source file called main.cpp and put it in the source folder (This is the only file I have in my project). In that file put the following code:

我正在读一本书,它告诉我打开一个空的 WIN32 项目。我创建了名为 main.cpp 的源文件并将其放在源文件夹中(这是我项目中唯一的文件)。在该文件中放入以下代码:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                  LPSTR lpCmdLine, int nShowCmd)
{
    MessageBox(NULL, "Motoko kusangai has hacked your system!", "Public Security Section 9", MB_OK | MB_ICONEXCLAMATION);
}

And run it. But I get the following error:

并运行它。但我收到以下错误:

1>c:\users\numerical25\documents\visual studio 2008\projects\begin\begin\main.cpp(6) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [40]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\Users\numerical25\Documents\Visual Studio 2008\Projects\Begin\Begin\Debug\BuildLog.htm"
1>Begin - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What am I doing wrong?

我究竟做错了什么?

回答by JaredPar

You need to use a wide string in this case because you are compiling for unicode. Try prefixing all of your string constants with L.

在这种情况下,您需要使用宽字符串,因为您正在为 unicode 进行编译。尝试在所有字符串常量前加上 L。

MessageBox(
  NULL, 
  L"Motoko kusangai has hacked your system!", 
  L"Public Security Section 9", 
  MB_OK | MB_ICONEXCLAMATION);

回答by Andrew O'Reilly

Petzold's classic Programming Windows starts off with a great chapter on Unicode that I'd recommend reading. If you are going to be doing any Win32 UI work I'd get a copy of his book. Given how out of favor Win32 is these days you can pick up used copies of the most recent 5th edition for less than $20. Unlike most technical authors Charles has a very conversational style and uses strong story telling techniques to make his books very readable despite their length (his Programming Windows with C# was similarly good).

Petzold 的经典 Windows 编程以我推荐阅读的关于 Unicode 的精彩章节开始。如果您打算从事任何 Win32 UI 工作,我会得到他的书的副本。鉴于如今 Win32 的失宠程度,您可以以不到 20 美元的价格购买最新的第 5 版的二手副本。与大多数技术作者不同,Charles 具有非常健谈的风格,并使用强大的讲故事技巧使他的书非常可读,尽管它们很长(他的用 C# 编程 Windows 同样不错)。

It's good practice these days to use unicode strings but if you really don't want them you can go into the project propertiesin VS and change the Character Set to "Use Multi-Byte Character Set", which will essentially get you the regular 8 bit ASCII you are probably used to.

现在使用 unicode 字符串是一种很好的做法,但如果你真的不想要它们,你可以进入VS 中的项目属性并将字符集更改为“使用多字节字符集”,这基本上会让你得到常规的 8您可能已经习惯了位 ASCII。

回答by Michal Ciechan

try

尝试

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                  LPSTR lpCmdLine, int nShowCmd)
{
    MessageBox(NULL, _T("Motoko kusangai has hacked your system!"), _T("Public Security Section 9"), MB_OK | MB_ICONEXCLAMATION);
}

Each time you get this error for some static text, enclose the static text within _T() tags.

每次在某些静态文本中出现此错误时,请将静态文本括在 _T() 标签中。

Edit: MS Link

编辑:MS链接

回答by dxmxax

I had the similar error and changing to "Use Multi-Byte Character Set" worked for me. I am using Visual Studio 2012.

我遇到了类似的错误,更改为“使用多字节字符集”对我有用。我正在使用 Visual Studio 2012。