numerical25
numerical25

Reputation: 10790

: 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. 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:

#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?

Upvotes: 2

Views: 13203

Answers (4)

dxmxax
dxmxax

Reputation: 1

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

Upvotes: 0

Andrew O&#39;Reilly
Andrew O&#39;Reilly

Reputation: 1643

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).

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.

Upvotes: 4

JaredPar
JaredPar

Reputation: 755587

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.

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

Upvotes: 9

Michal Ciechan
Michal Ciechan

Reputation: 13898

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.

Edit: MS Link

Upvotes: 2

Related Questions