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

12,967

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
12,967
numerical25
Author by

numerical25

Updated on June 05, 2022

Comments

  • numerical25
    numerical25 almost 2 years

    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?