num*_*l25 2 c c++ visual-studio-2008 visual-studio
我正在读一本书,它告诉我打开一个空的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);
}
Run Code Online (Sandbox Code Playgroud)
并运行它.但是我收到以下错误:
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 ==========
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
在这种情况下,您需要使用宽字符串,因为您正在编译unicode.尝试使用L为所有字符串常量添加前缀
MessageBox(
NULL,
L"Motoko kusangai has hacked your system!",
L"Public Security Section 9",
MB_OK | MB_ICONEXCLAMATION);
Run Code Online (Sandbox Code Playgroud)