Public Group

# Win32 - Message Box Problem

This topic is 3909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, So I'm learning to do windows programming and my program won't compile because of a strange error I keep getting. I will show you the error and my code. Code:
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpCmdLine ,int nCmdShow)
{
MessageBox( NULL , "Hi!", "Win32 - Creating a Message Box" , MB_OK );

return 0;
}


Error:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [13]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


The strange thing is, I don't have a function being called under the name 'MessageBoxW'. Does anyone know whats wrong with this?

##### Share on other sites
Important Note: I haven't ever really used the win32 API.

You seem to be building in wide character mode. Many win32 "functions" are just macros, and depending on the build mode they map to functionNameA (ASCII I presume) and functionNameW( for wide characters).

To compile your code in the same mode, you may be able to use a "TEXT" macro. It will put a "L" in front of strings in unicode build mode.

Alternatively, change to ascii build mode.

##### Share on other sites
There are several things you should know about the Win32 API:

1) It is designed to work with both wide character (multibyte) strings as well as narrow character (single byte) strings. It does this by having two of most functions: an 'A' variant and a 'W' variant. For example, there are two message box functions, namely, MessageBoxA() which works with narrow character strings and MessageBoxW() which works with wide character strings.

2) To control which type of string is used the Win32 API provides the symbol UNICODE. When UNICODE is defined the functions like MessageBox() are #defined as MessageBoxW(). When UNICODE is not defined functions like MessageBox() are #defined as MessageBoxA().

3) Starting with MSVC++ 2005 UNICODE is defined by default.

4) This complicates life when working with string literals and pointers to strings so the Win32 API also defines several string datatypes

CHAR: a narrow character
PSTR, PCSTR, LPSTR, LPCSTR: A pointer to an array of narrow characters

WCHAR: a wide character
PWSTR, PCWSTR, LPWSTR, LPCWSTR: A pointer to an array of wide characters

TCHAR: CHAR when UNICODE is not defined, WCHAR when UNICODE is defined
PTSTR, PCTSTR, LPTSTR, LPCTSTR: Analogous to the above

Prefer to use TCHAR in your dealings with Win32. If you're going to use std::string I would use something like this

typedef basic_string<TCHAR> tstring;

and then use tstring rather than string.

5) When working with string literals, wrap them in the TEXT macro. TEXT will make the string literal a wide string when UNICODE is defined and a narrow string when UNICODE is not defined.

This is what can solve your problem. Rewrite your code to be

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpCmdLine ,int nCmdShow){    MessageBox( NULL , TEXT("Hi!"), TEXT("Win32 - Creating a Message Box") , MB_OK );    return 0;}

##### Share on other sites
Quote:
 Original post by rip-offAlternatively, change to ascii build mode.
To expand on this a little - Assuming you're using Visual Studio 2005 or 2003, go to your project settings (Alt+F7), COnfiguration Properties -> General -> Character Set and change it to "Use Multi-Byte Character Set".

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631429
• Total Posts
3000033
×