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)
Win32 + LPCWSTR + Lots Of Squares :D
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:
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.
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.
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:
Again, haven't compiled this so no guarantees.
Hope this helps.
Instead ofchar ErrorTitle[12];sprintf_s(ErrorTitle, 12, "Error: %d", ErrorMessage.ErrorNumber);MessageBox(NULL, ErrorMessage.Message, (LPCWSTR)ErrorTitle, NULL);useWCHAR ErrorTitle[12];swprintf_s(ErrorTitle, 12, L"Error: %d", ErrorMessage.ErrorNumber);MessageBox(NULL, ErrorMessage.Message, ErrorTitle, NULL);//You don't have to typecast ErrorTitle anymoreInstead of(LPCWSTR)"Could Not Create Parent Window"useL"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.
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.
Answer is obvious now you told me. Too many late nights and starting to go code blind >.<
Thanks for the help :)
Thanks for the help :)
Quote:Original post by Ehrys
Too many late nights and starting to go code blind.
Is there any other way to code?
You could also use std::wstring... And actually use C++ instead of unsafe C s*printfs... Just my 43 cents ;-) I got a bit of a head cold today but I believe there is also std::wstringstream.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement