Jump to content
  • Advertisement
Sign in to follow this  
CTEagle

LPCWSTR

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

int MessageBox(
HWND hWnd,
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType
);


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

Try this instead:

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 this post


Link to post
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 this post


Link to post
Share on other sites
How about:

std::wstring pString(L"Dummy");

MessageBox(NULL, pString.c_str(), NULL, NULL);




Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by CTEagle
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'

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

Share this post


Link to post
Share on other sites
TEXT("Whatever") also seems to work for me:

MessageBox(hWnd, TEXT("HEY!"), TEXT("hey there!"), MB_OK);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!