# LPCWSTR

## Recommended Posts

I have a small C++ app in which I am just trying to display a message box. The following is all of the code in my app. #include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int temp; temp = 0; MessageBox (NULL,temp , "Tutorial", 0); return 0; } When I try to run the app, I receive the following error: error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'int' to 'LPCWSTR' Can anyone help me with this error? Thank you.

##### Share on other sites
int MessageBox(          HWND hWnd,    LPCTSTR lpText,    LPCTSTR lpCaption,    UINT uType);

You're passing an integer (temp) but the function expects a string.

MessageBox(NULL,NULL,_T("Tutorial"), 0);

_T() is a macro that prefixes an "L" on the string literal when compiling with UNICODE defined. It will make the literal compatible with LPCWSTR. Include tchar.h.

##### Share on other sites
LPCWSTR is a wide null terminated string. So you must pass a string to it.

##### Share on other sites
Thanks LessBread. I was able to get a msgbox to display based on your suggestions. I now have the following code and it works correctly.

#include <windows.h>
#include <string>
#include <tchar.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
std::string temp;
temp = "Hello";

MessageBox(NULL,_T("Hello"),_T("Tutorial"),0);

return 0;
}

I am still wondering how to pass a string to the msgbox though. If it try the following line:

MessageBox(NULL,temp,_T("Tutorial"),0);

I still get the following error:

error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'std::string' to 'LPCWSTR'

##### Share on other sites
std::wstring pString(L"Dummy");MessageBox(NULL, pString.c_str(), NULL, NULL);

##### Share on other sites
Thanks Razor. That worked for passing a string.

I do have another general question though. LessBread mentioned something about "compiling with UNICODE defined". I am not sure what this means. I googled UNICODE and have an understanding of what it is. However, I do not think I need it. Is it possible for me to set up my program to compile without UNICODE?

##### Share on other sites
If you're using Visual Studio:

Project -> Properties -> Configuration Properties -> General -> Character Set -> "Not Set".

P.S.
You'll have to apply this to all of the configurations that you'll be using, for example, "Debug" and "Release", or you can do it once by chosing "All Configurations".

##### Share on other sites
In this context, UNICODE, is a preprocessor token.

Here's the guts of the _T macro to illustrate:

#ifdef	UNICODE#define	__T(x)	L ## x#else#define	__T(x)	x #endif#define	_T(x)	__T(x)

If UNICODE is defined, _T inserts L before the string literal. If UNICODE is not defined, the string literal is untouched.

You can define UNICODE before the includes, or through the project settings along the lines that raz0r decribed.

##### Share on other sites
Quote:
 Original post by CTEagleThanks LessBread. I was able to get a msgbox to display based on your suggestions. I now have the following code and it works correctly.#include #include #include int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ std::string temp; temp = "Hello"; MessageBox(NULL,_T("Hello"),_T("Tutorial"),0); return 0;} I am still wondering how to pass a string to the msgbox though. If it try the following line:MessageBox(NULL,temp,_T("Tutorial"),0);I still get the following error:error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'std::string' to 'LPCWSTR'

try this:
MessageBox(NULL,temp.c_str(),_T("Tutorial"),0);

##### Share on other sites
TEXT("Whatever") also seems to work for me:
MessageBox(hWnd, TEXT("HEY!"), TEXT("hey there!"), MB_OK);

##### Share on other sites
In the headers that I looked at, the TEXT macro is defined the same as the _T macro.

#define TEXT(x) __T(x)

That is, they both do the same thing.

##### Share on other sites
You can use #undef UNICODE before you include any files to disable UNICODE. For starters, I suppose this would be a good idea.

Alternatively, you can append an 'A' to any function that accepts only UNICODE. For example, you can use MessageBoxA() instead of MesageBox(), which would be replaced with MessageBoxW() when you compile if UNICODE is defined.

However, I'm sure there's a good reason that UNICODE is the default setting in projects. Personally, I never made programs for anyone but myself, so I never used it.

##### Share on other sites
Quote:
 Original post by v0dKAHowever, I'm sure there's a good reason that UNICODE is the default setting in projects.

Internationalization.

##### Share on other sites
Note for 2005 users about unicode... You can use the method of shutting it off by preprocessor commands or.... go into project properties
Configuration Properties->General: Under the project defaults set character set from use unicode to use multibyte :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628344
• Total Posts
2982184

• 9
• 24
• 9
• 9
• 13