weird error

I'm getting this weird error in my code.

Error Message:
c:\documents and settings\owner\my documents\visual studio 2008\projects\helloworld win\helloworld win\main.cpp(10) : error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'const wchar_t [13]' to 'LPCSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>    // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    // create a "Hello World" message box using MessageBox()
    MessageBox(NULL,
               L"Hello World!",
               L"Just another Hello World program!",
               MB_ICONEXCLAMATION | MB_OK);

    // return 0 to Windows
    return 0;
}
It worked for me...

I cut and paste this code into a fresh, empty Win32 project and it popped up the little hello world box as expected.
Well it doesn't work for me.
try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>    // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    // create a "Hello World" message box using MessageBox()
    MessageBox(NULL,
               "Hello World!",
               "Just another Hello World program!",
               MB_ICONEXCLAMATION | MB_OK);

    // return 0 to Windows
    return 0;
}
Last edited on
worked, thx.

Damn tutorial made an error in the code.
Slaps the site.
closed account (z05DSL3A)
Gamer101 wrote:
Damn tutorial made an error in the code.
Slaps the site.

It is all down to whether _UNICODE is defined in the project or not.
In your project it is not defined so L"Hello World!", caused a problem. Matt, on the other hand, has it defined so did not have the problem.

I would suggest that you (always) use the _T("...") or _TEXT("...") macros (they do the same thing) for string literals. It will make sure 'L' is used if needed. You may need to #include <tchar.h> .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>    // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    // create a "Hello World" message box using MessageBox()
    MessageBox(NULL,
               _TEXT("Hello World!"),
               _TEXT("Just another Hello World program!"),
               MB_ICONEXCLAMATION | MB_OK);

    // return 0 to Windows
    return 0;
}


Doing so causes the string to be interpreted as composed of wide characters if the _UNICODE identifier is defined and as 8-bit characters if not.
Last edited on
Topic archived. No new replies allowed.