views:

9158

answers:

6

This seems like a pretty softball question, but I always have a hard time looking up this function because there seem there are so many variations regarding the referencing of char and tchar.

+4  A: 

TCHAR is a Microsoft-specific typedef for either char or wchar_t (a wide character).

Conversion to char depends on which of these it actually is. If TCHAR is actually a char, then you can do a simple cast, but if it is truly a wchar_t, you'll need a routine to convert between character sets. See the function MultiByteToWideChar()

Chris Johnson
You bring up an excellent point. Although in this particular situation I think the TChar is a wide character I'll only need to do the conversion if it isn't. which I gotta check somehow.
CrashCodes
+3  A: 

MultiByteToWideChar but also see "A few of the gotchas of MultiByteToWideChar".

jeffm
What happens if TCHAR is CHAR?
Martin York
I read the question as stipulating that a conversion was necessary, but point taken.
jeffm
+2  A: 

There are a few answers in this post as well, especially if you're looking for a cross-platform solution:

http://stackoverflow.com/questions/148403/utf8-tofrom-wide-char-conversion-in-stl

Mark Ransom
How can it be crosss platform there is no TCHAR anyware else. Its windows specific.
Martin York
+6  A: 

The simplest way is to use the conversion macros:

  • CW2A
  • CA2W
  • etc...

MSDN

RichS
we should not use these conversions, because if we call these in recursion, it won't release the memory
Vinay
@Vinay Create a helper function that calls one of these, then copies the result into the heap or into another buffer. Once the helper function returns, the stack memory is released.
bdonlan
+1  A: 

Although in this particular situation I think the TChar is a wide character I'll only need to do the conversion if it isn't. which I gotta check somehow.

if (sizeof(TCHAR) != sizeof(wchar_t))
{  .... }

The cool thing about that is both sizes of the equals are constants, which means that the compiler will handle (and remove) the if(), and if they are equal, remove everything inside the braces

James Curran
A: 

Here is the CPP code that duplicates _TCHAR * argv[] to char * argn[].

http://www.wincli.com/?p=72

If you adopting old code to Windows, simple use define mentioned in the code as optional.

Dmitry Obukhov