TCHAR - What is it, and how do I not use it?

This is a discussion on TCHAR - What is it, and how do I not use it? within the Windows Programming forums, part of the Platform Specific Boards category; I've tried googling it, and I've gathered that there's some compiler flag in MSVS that automatically makes me 'use unicode' ...

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478

    TCHAR - What is it, and how do I not use it?

    I've tried googling it, and I've gathered that there's some compiler flag in MSVS that automatically makes me 'use unicode' - is this correct?

    Anyway, if I try code from TheForger's site, for example, it will go nuts, as I'm trying to pass normal character arrays to Unicode versions of the API functions. Correct me if I'm wrong.

    This also means that ever string literal I use needs to be enclosed with the _T() macro to convert the literal to UNICODE.

    How do I not use UNICODE? It is really not needed when I'm trying to get the hang of GUI programming, and if I were to publish my code on some of the boards I frequent, using the tchar.h header and whatnot, it wouldn't be extremely portable either.

    All hints and suggestions will be greatly appreciated.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Oh, never mind, I fixed it.

    Under project options in MSVS, I found an option to change the character set. I just changed it to "Not Set", and saw that the compiler flag "/D "_UNICODE" had been removed.

    Modified my source file so it didn't use the _T() macro and then tried compiling.

    Voila.

    Sorry for posting before researching properly.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    No, you're not doing it quite right. You should use multi-byte if not unicode typically, but the real thing is that TCHAR is defined to either CHAR or wchar_t depending on whether you use unicode or not.
    So therefore, if you use TCHAR, you should also use the _T macro. It's good practice to use TCHAR since it will not harm your project in any way and allow you to switch between multi-byte/unicode as you want without any code changes. And using _T on all string liters takes getting used to...

    IF you don't want unicode at all, or just want unicode, then you should remove all TCHAR and replace with CHAR or wchar_t as appropriate.
    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
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Elysia View Post
    No, you're not doing it quite right. You should use multi-byte if not unicode typically, but the real thing is that TCHAR is defined to either CHAR or wchar_t depending on whether you use unicode or not.
    So therefore, if you use TCHAR, you should also use the _T macro. It's good practice to use TCHAR since it will not harm your project in any way and allow you to switch between multi-byte/unicode as you want without any code changes. And using _T on all string liters takes getting used to...

    IF you don't want unicode at all, or just want unicode, then you should remove all TCHAR and replace with CHAR or wchar_t as appropriate.
    Well, the problem was that MSVS wouldn't convert between char* or char[] to TCHAR, meaning I had to use the _T macro, meaning that, were I to share the code with some of my friends, they would most likely not be able to compile it, since most, if not all, of them don't have MSVS.

    Also, you have to keep in mind that I'm not actually 'developing' anything. I'm just coding for my own pleasure, perhaps creating something useful now and then.

    Thanks for the tips though.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    But it's also very easy to define _T as something, you know... You should keep that in mind too!
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21