MessageBox and express08
Not having messed with the win32 api for ages i decided i wanted to, so i start by finding out microsoft now give away a free 'express' version of vc so i decided what the hell and junked my vc.net 02 edition, now asside from missing every header i used i find im now having trouble writing a simple test app,
i used to be able to use the following function fine:
infact im sure i didnt even need the LPCTSTR typecast before but memory may be foggy..
MessageBox(NULL, (LPCTSTR)"Hello World", (LPCTSTR)"Title",MB_OK);
only to find i was introduced to a message box written in what seemed to be chinese.
i figured their might of been some changes due to unicode and such so i did a brief sweep of the msdn lib, finding that i now need to use the LPCWSTR and the char L infront of any string i want to directly pass so it now looks like:
MessageBox(NULL, (LPCWSTR)L"Hello World", (LPCWSTR)L"Title",MB_OK);
Im guessing the W stands for wide and have no idea what the L stands for i guess literal as in string literal, no idea and trauling the net for a reasonable answer isnt my idea of fun, could anyone please enlighten this old coder as to the reason behind this change and what they stand for?
Well, there are two versions, MessageBoxA, and MessageBoxW. One is for ANSI (or maybe it's ASCII?), the other is for Unicode. Depending on whether you have UNICODE or something defined, MessageBox will turn into MessageBoxA or MessageBoxW.
So use _T or TEXT to cast your ANSI string to the right type.
T (in LPCTSTR) basically means that it could either be wide or normal.
MessageBox(NULL, TEXT("Hello World"), TEXT("Title"),MB_OK);
You shouldn't use casts before the text. Try first without a cast, THEN try with a cast, if you know what it means to do so.
The thing is, default is unicode and you forced your typical multi-byte ansi text to a unicode pointer, which displayed all that chinese ;)
So use _T, TEXT for text or no cast at all if you're working with pure text and either multi-byte OR unicode (not going to switch them).
"MessageBox(NULL, TEXT("Hello World"), TEXT("Title"),MB_OK);" that worked nicely, ive never found the need to typcast or use the TEXT function before thats why this got me slightly irritated, it is using W rather than the MBA function so im guessing Elysia is right saying it defaults to this
another quick question tho is passing variables to the function as im still getting chinese if i pass it a char array say:
and it still needs the typcasts as it fails without them so i cant just write the text or variable as the param
char exampleString="i have a dislike for unicode";
bah makes me want to reinstall 02 :p
The easiest thing to do is to stop using "MessageBox" and use "MessageBoxA" or "MessageBoxW" explicitly.
tbh i barely use it i usualy design my own form for such aler tinformation it was merely a test, one which irritated me to no end :p but ur right ofc that would sort it
Also, if you remove "UNICODE" and "_UNICODE" pound-defines, then all API's should behave like the "A" version.
Rather change the setting to use multi-byte instead of unicode. The only problem why you have this is because you use unicode. It's default by now, so make sure to switch back to multi-byte if you don't want unicode.
To use unicode strings, you need to add L before; no such thing for multi-byte, so just change project options. It's under General.
Get used to UNICODE, it isn't going to go away.
do not use char array
Originally Posted by MicroFiend
TCHAR exampleString=TEXT("i have a dislike for unicode");