Sign in to follow this  

Win32 + LPCWSTR + Lots Of Squares :D

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

The topic title pretty much sums it up. When I print out anything that uses "char", "std::string" or even "LPCWSTR" all that gets printed through "MessageBox()" is a lot of square blocks [][][][][]4[][]AA[][] etc etc. The only way it will print out correctly is if I specifically hardcode the message in the function call. What I'm currently doing is:
struct ErrorMsg
{
   int ErrorNumber;
   LPCWSTR Message;
};

...

if(!Engine.Startup(&ErrorMessage))
	{
		char ErrorTitle[12];
		sprintf_s(ErrorTitle, 12, "Error: %d", ErrorMessage.ErrorNumber);
		MessageBox(NULL, ErrorMessage.Message, (LPCWSTR)ErrorTitle, NULL);
		return(ErrorMessage.ErrorNumber);
	}

Engine.Startup:

bool CEngine::Startup(ErrorMsg* ErrorMessage)
{
	if(m_WindowManager.NewWindow(800, 600, true))
		return true;
	else
	{
		(*ErrorMessage).ErrorNumber = 1;
		(*ErrorMessage).Message = (LPCWSTR)"Could Not Create Parent Window";
		return false;
	}
}

(I have forced the WindowManager to return false)


With this I get the output I previously mentioned. I've also tried char* which provided the same results. I also tried passing ErrorMessage by Value which caused it to output nothing whatsoever. Bit confused as to what's going on. Also to note, if I use sprintf_s(ErrorTitle, 12, "Error: 1234", ErrorMessage.ErrorNumber); <--- Notice I'm not including the actual error number in the buffer. Then I get the same output as before.

Share this post


Link to post
Share on other sites
You are typecasting a char* to a LPCWSTR. char's are 1 byte, WCHAR's are 2 bytes. Most anything that has a 'W' in it requires wide chars.


Instead of
char ErrorTitle[12];
sprintf_s(ErrorTitle, 12, "Error: %d", ErrorMessage.ErrorNumber);
MessageBox(NULL, ErrorMessage.Message, (LPCWSTR)ErrorTitle, NULL);

use

WCHAR ErrorTitle[12];
swprintf_s(ErrorTitle, 12, L"Error: %d", ErrorMessage.ErrorNumber);
MessageBox(NULL, ErrorMessage.Message, ErrorTitle, NULL);
//You don't have to typecast ErrorTitle anymore

Instead of
(LPCWSTR)"Could Not Create Parent Window"

use

L"Could Not Create Parent Window"


I haven't compiled any of this, but I think you'll get the picture. If you want to convert from char to WCHAR, use MultiByteToWideChar. Something like:

char CharStuff[] = "Some stuff in chars.";
WCHAR wCharStuff[128];
MultiByteToWideChar(CP_ACP, CharStuff, -1, wCharStuff, 128);


Again, haven't compiled this so no guarantees.
Hope this helps.

Share this post


Link to post
Share on other sites

WCHAR ErrorTitle[12];
swprintf_s(ErrorTitle, 12, L"Error: %d", ErrorMessage.ErrorNumber);
MessageBoxW(NULL, ErrorMessage.Message, ErrorTitle, NULL);


If you're going to go with hardcoded wchar_t's, use MessageBoxW too.

Share this post


Link to post
Share on other sites

This topic is 3852 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this