Results 1 to 5 of 5

Thread: What is the _T ??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    What is the _T ??

    Hi,

    I was just wondering whether someone can explain what the _T relates to in the following code. Haven't been able to find out anywhere.

    Code:
    CString logMsg;
    
    	if ( FAILED(hr) )
    	{
    	logMsg.Format(_T("Error determining current User ID (GetOwnUserId failed with error 0x%x)"), hr);
            retUserID = _T("Unavailable");
            return retUserID;
    	}
    	else;
    Any help would be much appreciated.
    Thanks,
    Steve

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    From what I can glean from various literature, I think it is either a macro or a function that converts the string to a wide character format (unicode or some such).

    For instance some console programs use
    Code:
    int _tmain()
    {
    ...
    }
    as their entry point. It seems to have something to do with Unicode machinations and portability to other platforms.

    But I could be wrong .
    I don't live here any more.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You're close. The t is a common abbreviation for generic characters (no idea why it's a t).

    The whole thing is mainly a Windows issue. Win9x supports only ANSI, WinCE only UNICODE and WinNT uses UNICODE internally but provides ANSI support.
    That support means that every ANSI string passed into the API gets converted to UNICODE and every string passed out back to ANSI. This takes some time, so ideally you'd want your apps to be compiled as UNICODE for CE and NT and as ANSI for 9x.

    To make this possible, every WinAPI function that uses strings is available in two versions on NT and only one of them on the other systems. E.g. CreateWindow: there is a CreateWindowA that takes an ANSI string and a CreateWindowW that takes a UNICODE (or Wide) string.

    CreateWindow itself is a macro, an alias. Depending on the UNICODE symbol being defined or not, it resolves to CreateWindowA or CreateWindowW.

    There are therefore two types of characters: narrow ANSI characters and wide UNICODE characters. ANSI characters use the datatype "char" with these aliases (provided windows.h is included):
    CHAR
    and these pointer aliases:
    char *: LPSTR, PSTR
    const char *: LPCSTR, PCSTR

    UNICODE characters use the datatype "unsigned short" with these aliases:
    wchar_t, WCHAR
    and these pointer aliases:
    unsigned short *: LPWSTR, PWSTR
    const unsigned short *: LPCWSTR, PCWSTR
    In C++, wchar_t is supposed to be a built-in type and the real type of UNICODE characters. However this often needs to be enabled with a compiler switch, where it is supported at all. I will from now on refer to wchar_t nonetheless.

    How to be generic with types? The answer is a typedef, dependent again on UNICODE.
    TCHAR, _TCHAR, tchar_t.
    This typedef resolves to char when UNICODE is not defined, wchar_t when it is. tchar_t is not readily available, I use it in my own files.
    TCHAR is defined in windows.h. _TCHAR is defined in tchar.h, a MS-specific header that also defines the _T and _TEXT macros (windows.h defines the TEXT macro with the same functionality) as well as the _tmain, _tWinMain, _tstrcmp, etc. macros. The _ means that they are not CRT functions even if they look like it. They are macros that resolve to either the ANSI or the UNICODE versions of the CRT functions.
    tchar.h uses the _UNICODE symbol, not UNICODE. This means that you should always define BOTH UNICODE and _UNICODE when compiling for unicode.

    Finally there's the issue of character and string literals. 's' is a character literal and "asdlk" is a string literal. Which character set should they be?
    Simple: they are ANSI. To get UNICODE, you must prepend the literal with a capital L (for Long I think).
    L's' and L"asdlk" are UNICODE literals.

    _T, _TEXT and TEXT are macros that prepend a L to the argument when UNICODE is defined but don't when it is not.
    The definition is
    Code:
    #ifdef _UNICODE
    #  define _T(x) L##x
    #else
    #  define _T(x) x
    #endif
    The ## is a special macro operator that can be used to concatenate arguments with macro literals or other arguments.

    Of the three macros, _T is most commonly used because it is the shortest. That may be a bad idea though, as literal strings in any app, but particularly GUI apps, are generally discouraged. In those situations where they are useful they usually need not be generic. Strings that appear in user interaction should be stored in string table resources or other resource files.

    On final word on tchar_t. It is used in my header, tcpp, which is analog to tchar.h in that it declares generic versions of often-used parts of the standard library, like tcout and tstring.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    _T is a macro that expands to nothing in normal builds, and to L in unicode builds (if UNICODE is defined)

    It allows you to make your code portable between those two types of builds

    Code:
    TCHAR* a = _T("string");
    // same as
    char* a = "string";
    
    #define UNICODE
    TCHAR* b = _T("string");
    // same as
    wchar_t* b = L"string";

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thanks

    Thanks again for all the help.

    Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width