Cannot convert parameter from 'const char[20]' to 'LPCWSTR'
Asked Answered
E

3

5

When compiling this code:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInsance,HINSTANCE hPrevInstance,PSTR cmdLine,int showCmd){
    MessageBox(0,"First Win32 Program","Window Tittle",MB_OK);
    }

I get the compiler error:

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

What am I doing wrong?

Earthlight answered 30/3, 2011 at 2:58 Comment(1)
I hesitate to use my C++ dup-hammer on this but here's the oldest question on this I could find: https://mcmap.net/q/1265779/-cast-to-lpcwstrInflation
R
6

You have UNICODE defined, so MessageBox is expecting a wide string.

Rescissory answered 30/3, 2011 at 3:0 Comment(0)
K
16

By default, UNICODE is defined in Visual Studio 2010. Either call MessageBoxA() instead of using the MessageBox() define, or pass a wide string literal (L"First Win32 Program" and L"Window Title"). Or, if you really care about being able to build without UNICODE defined, use the TEXT() macro around your string literals: TEXT("First Win32 Program") (this is the same as L"First Win32 Program" when UNICODE is defined, but it will be "First Win32 Program" when UNICODE is not defined).

For more information about UNICODE and how it affects what APIs are called, see http://msdn.microsoft.com/en-us/goglobal/bb688113.aspx. Specifically, the sections below "Creating Win32 Unicode Applications".

On a side note: All Windows operating systems supported by Microsoft today are Unicode native. I would recommend to always favor the "wide" API. In this case, MessageBoxW(), which is what MessageBox() is defined to be when UNICODE is set. The days of using the TEXT() macro and compiling without UNICODE defined should be behind us.

Kayser answered 30/3, 2011 at 3:2 Comment(3)
Hmm, I always use the _T macro anyway. I'm not sure what the downside is, or why you'd prefer to use L, even if the non-Unicode days are behind us.Bonefish
There's no real downside except for what some might find to be clutter and the fact that if you forget to use it, you'll get compilation errors when UNICODE is defined or vice versa. Personally, I opt for the explicitness of calling the wide APIs and just using wide data types, which really just makes the UNICODE define useless.Kayser
@Cody - One reason for using L instead of _T is that L is standard C++, and that we only target Windows versions with UNICODE defined. Unless you still do a lot of Windows 95, of course. :-)Fatuitous
R
6

You have UNICODE defined, so MessageBox is expecting a wide string.

Rescissory answered 30/3, 2011 at 3:0 Comment(0)
B
2

MessageBoxW() expects wide chars... you can make little bit changes in your code and than your code will be perfectly running.

Solution One :- MessageBox(0,L"First Win32 Program",L"Window Tittle",MB_OK);

Solution two. Use MessageBoxA() instead of MessageBox(). MessageBoxA() will take narrow chars in ANSI character set.

Breland answered 9/5, 2016 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.