Thread: MessageBox and express08

  1. #1
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80

    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:

    Code:
    MessageBox(NULL, (LPCTSTR)"Hello World", (LPCTSTR)"Title",MB_OK);
    infact im sure i didnt even need the LPCTSTR typecast before but memory may be foggy..

    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:

    Code:
    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?

    much appreciated,
    Ryan
    You can stop change as easily as u can drink the sea with a fork

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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.
    Code:
    MessageBox(NULL, TEXT("Hello World"), TEXT("Title"),MB_OK);
    T (in LPCTSTR) basically means that it could either be wide or normal.

    http://msdn2.microsoft.com/en-us/lib...53(VS.85).aspx
    Last edited by robwhit; 01-09-2008 at 02:37 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    "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:
    Code:
    char exampleString[]="i have a dislike for unicode";
    and it still needs the typcasts as it fails without them so i cant just write the text or variable as the param

    bah makes me want to reinstall 02
    You can stop change as easily as u can drink the sea with a fork

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The easiest thing to do is to stop using "MessageBox" and use "MessageBoxA" or "MessageBoxW" explicitly.

    gg

  6. #6
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    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 but ur right ofc that would sort it
    You can stop change as easily as u can drink the sea with a fork

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Also, if you remove "UNICODE" and "_UNICODE" pound-defines, then all API's should behave like the "A" version.

    gg

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Get used to UNICODE, it isn't going to go away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MicroFiend View Post
    another quick question tho is passing variables to the function as im still getting chinese if i pass it a char array say:
    Code:
    char exampleString[]="i have a dislike for unicode";
    do not use char array

    Code:
    TCHAR exampleString[]=TEXT("i have a dislike for unicode");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed