Jump to content
  • Advertisement
Sign in to follow this  
VanKurt

VS2005 Beta2 freakin me out - MessageBox & LPCWSTR ??

This topic is 4654 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

Hi there! Today I started working with VisualStudio2005 Beta 2. As an experienced game programmer I decided to do a simple hello-world-app to see what the new IDE is like ;-) I couldn't believe it, when even the simplest thing didn't work: displaying a message box! With VC03 it was as easy as "MessageBox NULL, "Hello", "Wurx", MB_OK);". But here NOTHING works :-( Here's my code:
#include <windows.h>
#include <string>
#include <iostream>

using namespace std;

void main( void )
{
	string text1("Body");
	string capt1("Caption");

	char* text2("Body");
	char* capt2("Caption");

	// WHY DOES THIS NOT WORK???
	//MessageBox( NULL, "Text1", "Caption1", MB_OK );

	// These don't compile
	//MessageBox( NULL, &text1, &capt1, MB_OK );
	//MessageBox( NULL, text1, capt1, MB_OK );
	//MessageBox( NULL, text1.c_str(), capt1.c_str(), MB_OK );
	//MessageBox( NULL, (LPCWSTR)text1.c_str, (LPCWSTR)capt1.c_str, MB_OK );

	// These don't compile, too
	//MessageBox( NULL, text2, capt2, MB_OK );
	//MessageBox( NULL, &text2, capt2, MB_OK );

	// Compile, but box is filled with crazy chars
	//MessageBox( NULL, (LPCWSTR)text2, (LPCWSTR)capt2, MB_OK );
	//MessageBox( NULL, (LPCWSTR)text1.c_str(), (LPCWSTR)capt1.c_str(), MB_OK );
}//end main


As you can see I tried all possible combinations (even this strange string stuff I don't really know...). :-) Here's the compiler error I get all the time:
Quote:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [6]' to 'LPCWSTR'
What's that supposed to mean? Please, help me [embarrass]! Thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
EDIT: Oppss you did cast.

LPCWSTR is unicode. Yours is just char. 1 byte (char) vs 2 byte (unicode)

Share this post


Link to post
Share on other sites
A much safer alternative is to use:

MessageBox(NULL, CString("abc"), CString("xyz"), MB_OK);


The CString class will handle all the work. in this case, it'll turn the chars to wchar_ts.

to print variables, add them to an std::string using a string stream and boost::lexical_cast, then:

MessageBox(NULL, CString(MyStdString.c_str()), CString("title"), MB_OK);

Share this post


Link to post
Share on other sites
you can also use the TEXT macro like I do:
The TEXT macro identifies a string as Unicode when the UNICODE is defined during compilation. Otherwise, it identifies a string as an ANSI string.

your code would look something like this:
MessageBox( NULL, TEXT("Text1"), TEXT("Caption1"), MB_OK );

Share this post


Link to post
Share on other sites
Mhh, TEXT only works for "blabla" strings, but how do I handle variables?
I also had a problem using CString...what headers do need to use this class? Is this somehow MFC specific (I'm not using this) ?? :-)
Thanks so far!

Share this post


Link to post
Share on other sites
OR, you can go into project settings , and change it from UNICODE to none (auto-detect i believe)

EDIT:

people love making things more difficult then they really are. unicode is terrific, and you SHOULD use it, but you DONT HAVE TO.

Share this post


Link to post
Share on other sites
Welcome to the world of Unicode. Read chapter 2 of Charles Petzold's "Programming Applications in Win32" for the full story on all of this. Short answer: use TCHAR instead of char for strings. TCHAR is a macro that expands out to wchar_t if _UNICODE is #define'd and char otherwise. Most functions that take strings have A appended to their name for the narrow versions, and W for the wide character versions. Every function you call is #define'd to one of these depending on whether _UNICODE is #define'd.

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!