简体   繁体   English

:错误C2664:'MessageBoxW':无法从'const char [40]'转换参数2

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

I am reading a book and It told me to open a empty WIN32 project. 我正在读一本书,它告诉我打开一个空的WIN32项目。 I created source file called main.cpp and put it in the source folder (This is the only file I have in my project). 我创建了名为main.cpp的源文件并将其放在源文件夹中(这是我项目中唯一的文件)。 In that file put the following code: 在该文件中输入以下代码:

#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? 我究竟做错了什么?

You need to use a wide string in this case because you are compiling for unicode. 在这种情况下,您需要使用宽字符串,因为您正在编译unicode。 Try prefixing all of your string constants with L. 尝试使用L为所有字符串常量添加前缀

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

Petzold's classic Programming Windows starts off with a great chapter on Unicode that I'd recommend reading. Petzold的经典编程Windows开始时有一个关于Unicode的伟大章节,我建议你阅读。 If you are going to be doing any Win32 UI work I'd get a copy of his book. 如果您要进行任何Win32 UI工作,我会得到他的书的副本。 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. 鉴于Win32如今不受欢迎,您可以以不到20美元的价格购买最新第5版的旧版本。 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). 与大多数技术作家不同,查尔斯有一种非常会话式的风格,并且使用强大的故事讲述技巧使他的书尽管长度很长(他的编程Windows与C#同样好)。

It's good practice these days to use unicode strings but if you really don't want them you can go into the project properties in 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。

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 Link

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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 C2664:MessageBoxW 无法将参数 2 从“const char”转换为“LPCWSTR” - Error C2664: MessageBoxW cannot convert argument 2 from 'const char ' to 'LPCWSTR' 错误C2664:&#39;strcmp&#39;:无法将参数2从&#39;char&#39;转换为&#39;const char *&#39; - error C2664: 'strcmp' : cannot convert parameter 2 from 'char' to 'const char *' 错误 C2664:“wcscmp”:无法将参数 1 从“CHAR [260]”转换为“const wchar_t *” - error C2664: 'wcscmp' : cannot convert parameter 1 from 'CHAR [260]' to 'const wchar_t *' 错误C2664:“发送”:无法将参数2从“ ServerGreeting”转换为“ const char *” - error C2664: 'send' : cannot convert parameter 2 from 'ServerGreeting' to 'const char *' 错误C2664:“ CComboBox :: InsertString”:无法将参数2从“ const char [4]”转换为“ LPCTSTR” - error C2664: 'CComboBox::InsertString' : cannot convert parameter 2 from 'const char [4]' to 'LPCTSTR' 错误C2664:&#39;strcpy&#39;:无法将参数2从&#39;const wchar_t [9]&#39;转换为&#39;const char *&#39;。如何解决此错误? - Error C2664: 'strcpy' : cannot convert parameter 2 from 'const wchar_t [9]' to 'const char *'.How to solve this error? C2664无法将参数1从&#39;const char *&#39;转换为&#39;Node *&#39; - C2664 cannot convert argument 1 from 'const char *' to 'Node*' 错误 C2664:“int printf(const char *const,…)”:无法将参数 2 从“void”转换为“…” - error C2664: 'int printf(const char *const ,…)': cannot convert argument 2 from 'void' to '…' 错误 C2664:无法将参数 1 从“const char [14]”转换为“char” - error C2664: cannot convert argument 1 from 'const char [14]' to 'char' 错误C2664:“策略::中间阶段”:无法将参数2从“字符*”转换为“字符* []” - Error C2664: 'Strategy::Interphase' : cannot convert parameter 2 from 'char *' to 'char *[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM