Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How/Where to use _T() a.k.a. TEXT() macros?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I've read that the _T("...") or TEXT("...") macros basically expand to L"..." when UNICODE is defined and "..." when it isn't.
For literal strings like that, it makes sense, but what about pointers? Is there a way to do the same kind of thing with variables?
If I do this:
Code:
char szData[256];
strcpy( szData, "Hello World" );
std::wstring wStr( _T( szData ) );  // C2065: 'LszData' : undeclared identifier
I get an error since it just puts an L infront of the variable name.
 
Use TCHARs. The problem is that there isn't an equivalent for std::string so you'll have to make up your own. Have a look at tchar.h for more info
Code:
#include <tchar.h>
#include <string>
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
TCHAR szData[256];
_tcscpy( szData, _T("Hello World") );
std::tstring wStr( _T( szData ) );
 
xwb said:
Code:
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
Yeah, that's basically the only thing I could think of to put a bandage on this problem...
It's a hell of a mess though, since I'm trying to create an interface between two badly designed components; one which uses UNICODE and passes CString objects (across a DLL boundary none the less) and another that is only non-UNICODE and not const-correct.
[noevil]
 
I don't think that solves your problem, though. It sounds like you still have to convert from non-Unicode to Unicode. There are functions to do that, but I don't remember the name(s).
 
Yup, those are the ones.
I used them in my functions which basically look like this:
Code:
Tstring ASCII_ToWide( const char* );
Tstring ASCII_ToWide( const CString& );
Tstring WideToASCII( const wchar_t* );
Tstring WideToASCII( const CString& );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top