Error With Message Box

When I use VS 2012, I get this error to display a message box:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [31]' to 'LPCWSTR'
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include <windows.h>
//#include <windowsx.h>

int WINAPI WinMain(HINSTANCE hIn , HINSTANCE hPrevInce, 
	LPSTR lpe, int nShowd)
{
    MessageBox(NULL, "hayate has hacked your system!",
        "Public Security Section 9", MB_OK | MB_ICONEXCLAMATION);

    return 0;

}

Have some guys fix this error. Please help me!
Use T("Text...") instead of "Text" there.
See this thread for a discussion on different ways of resolving this issue:

http://cboard.cprogramming.com/windows-programming/82697-messagebox-visual-cplusplus.html

There are pros and cons of each way, so you'll have to decide for yourself which way you'd like to go. Essentially you need to tell the compiler that "hayate has hacked your system!" and "Public Security Section 9" are strings, using a prefix such as TEXT, L or _T, depending on your UNICODE needs. So, your function call would become something like:

1
2
MessageBox(NULL, TEXT("hayate has hacked your system!"),
        TEXT("Public Security Section 9"), MB_OK | MB_ICONEXCLAMATION);


Hope that helps.
You compile your project as Unicode, but you give MessageBox a non-Unicode string. You could use the _T() macro like this:

1
2
 MessageBox(NULL, _T("hayate has hacked your system!"),
        _T("Public Security Section 9"), MB_OK | MB_ICONEXCLAMATION);


If _UNICODE is defined, the _T() macro converts it's content from "my string" to L"my string", otherwise it does not. See:

http://msdn.microsoft.com/en-us/library/dybsewaf%28v=vs.80%29.aspx
Last edited on
Topic archived. No new replies allowed.