Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Why is this not working?

toddlerasimtoddlerasim Member Posts: 93
Why is the TextOut() printing gibberish in VS2008 while it works perfectly well in VC++6.

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here...
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,200,200);
MoveToEx(hdc,300,0,NULL);
LineTo(hdc,50,300);
TextOut(hdc,20,20,(LPCTSTR)"text1",6);
ValidateRect(hWnd,NULL);
EndPaint(hWnd, &ps);
break;

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Why is the TextOut() printing gibberish in VS2008 while it works
    : perfectly well in VC++6.
    :

    In VC++6 TCHAR is by default char. In VS2008 however, it defaults to unicode (wchar_t). Normally, you'd get a compiler error, but you have forcibly cast the string literal (which is always of type char[]) to a wchar_t*, and thus the compiler does not object.

    Remove the cast, and prepend L to the string literal as such: L"This string uses wchar_t"

    Alternatively, if you want it to be portable, you could use the _T or TEXT macro: TEXT("This string will be char or wchar_t, depending on unicode setting")

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • toddlerasimtoddlerasim Member Posts: 93
    : : Why is the TextOut() printing gibberish in VS2008 while it works
    : : perfectly well in VC++6.
    : :
    :
    : In VC++6 TCHAR is by default char. In VS2008 however, it defaults to
    : unicode (wchar_t). Normally, you'd get a compiler error, but you
    : have forcibly cast the string literal (which is always of type
    : char[]) to a wchar_t*, and thus the compiler does not object.
    :
    : Remove the cast, and prepend L to the string literal as such: L"This
    : string uses wchar_t"
    :
    : Alternatively, if you want it to be portable, you could use the _T
    : or TEXT macro: TEXT("This string will be char or wchar_t, depending
    : on unicode setting")
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    :
    :Well it may be blurry to you but you certainly made it crystal clear to me. Both _T and TEXT worked like a charm. Many thanks. But how does one know when to use TextOut() or _T / TEXT()?
  • LundinLundin Member Posts: 3,711
    : But how does one know when to use TextOut() or _T / TEXT()?

    Those are different things. TextOut() is a function for printing, TEXT() is a macro to make your strings portable.

    If you want to write portable code, you should always use the TEXT() macro as soon as you write a string literal "" in your code. The API guru Charles Petzold always does this, for example.

    Problems like this appear in the Win API because it was written with Unicode support before the C language had it. In C99, a char can only be 8 bytes and a wchar_t can only be 16 bytes. "Classic" C doesn't make this distinction, and says that char could possibly be larger than 8 bytes.
Sign In or Register to comment.