In modern Windows programming, it's OK to just ignore TCHAR
and instead use wchar_t
(WCHAR
) and Unicode UTF-16.
(TCHAR
is a model of the past, when you wanted to have a single code base, and produce both ANSI/MBCS and Unicode builds changing some preprocessor switches like _UNICODE
and UNICODE
.)
In any case, you should use C++ and convenient string classes to simplify your code. You can use ATL::CString
(which corresponds to CStringW
in Unicode builds, which are the default since VS2005), or STL's std::wstring
.
Using CString
, you can do:
CString str1 = L"Some value";
CString str2 = L"Another value";
CString cs;
cs.Format(L"The paths are %s and %s", str1.GetString(), str2.GetString());
CString
also provides proper overloads of operator+
to concatenate strings (so you don't have to calculate the total length of the resulting string, dynamically allocate a buffer for the destination string or check existing buffer size, call wcscpy
, wcscat
, don't forget to release the buffer, etc.)
And you can simply pass instances of CString
to Win32 APIs expecting const wchar_t*
(LPCWSTR/PCWSTR
) parameters, since CString
offers an implicit conversion operator to const wchar_t*
.