0

When I take something as simple as this:

char text1[] = "hello world";
MessageBox(NULL, text1, NULL, NULL);

I get this error:

Error   1   error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [12]' to 'LPCWSTR'   
8
  • 1
    char text1[] = ... is what you are looking for. Commented Aug 28, 2013 at 18:06
  • Well, wchar_t text1[] (or std::wstring). Commented Aug 28, 2013 at 18:08
  • In C++ you would use std::string. Commented Aug 28, 2013 at 18:09
  • What almost no one here is mentioning is that the wide versions should be used when dealing with the winapi. Commented Aug 28, 2013 at 18:10
  • error C2039: 'string' : is not a member of 'std' Commented Aug 28, 2013 at 18:12

4 Answers 4

4

You have two basic problems. First, a char can only hold one character, not a string of characters. Second, you have a "narrow" character string literal, but you're (apparently) using a Unicode build of your application, in which MessageBox expects to receive a wide character string. You want either:

wchar_t text1[] = L"hello world";

or:

wchar_t const *text1 = L"hello world";

or (most often):

std::wstring text1(L"hello world");

...but note that an std::wstring can't be passed directly to Messagebox. You'd need to either pass text1.c_str() when you call MessageBox, or else write a small wrapper for MessageBox that accepted a (reference to) a std::wstring, something like:

void message_box(std::wstring const &msg) {
     MessageBox(NULL, msg.c_str(), NULL, MB_OK);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@user2726531: Sounds like you need to add #include <string>.
But now a new error occured: error C2601: 'MessageBoxW' : local function definitions are illegal
@user2726531: Ah, I messed up -- since MessasgeBox is a macro, it's replacing the name of the MessageBox function above with MessasgeBoxW. Just rename it to something else.
0

A string literal in C/C++ is not a char but a collection of char values. The idiomatic way of declaring this is

const char* text1 = "hello world";

2 Comments

error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
@user2726531 LPCWSTR is referring to a unicode string while char* or LPCSTR refers to an ANSI string. If you need a unicode string use LPCWSTR and change the declaration to L"hello world"
0

char is a single character, not a String.

You need Unicode, you can use TCHAR;

TCHAR[] text = _T("Hello World.");
MessageBox(NULL, text, NULL, NULL);

2 Comments

error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [12]' to 'LPCWSTR'
@user2726531 edited the answer. You just need Unicode character.
0

char only hold one character, not an array of characters.

So just use a pointer to constant string of Unicode characters.

LPCWSTR text1 = L"hello world";

1 Comment

first option works. Second option does not: Error error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.