Converting from const char * to LPTSTR without USES_CONVERSTION

Santron Manibharathi picture Santron Manibharathi · Feb 4, 2013 · Viewed 18.3k times · Source

I am trying to convert const char * to LPTSTR. But i do not want to use USES_CONVERSION to perform that.

The following is the code i used to convert using USES_CONVERSION. Is there a way to convert using sprintf or tcscpy, etc..?

USES_CONVERSION;
jstring JavaStringVal = (some value passed from other function);
const char *constCharStr = env->GetStringUTFChars(JavaStringVal, 0);    
LPTSTR lpwstrVal = CA2T(constCharStr); //I do not want to use the function CA2T..

Answer

Reza Ebrahimi picture Reza Ebrahimi · Feb 4, 2013

LPTSTR has two modes:

An LPWSTR if UNICODE is defined, an LPSTR otherwise.

#ifdef UNICODE
    typedef LPWSTR LPTSTR;
#else
    typedef LPSTR LPTSTR;
#endif

or by the other way:

LPTSTR is wchar_t* or char* depending on _UNICODE

if your LPTSTR is non-unicode:

according to MSDN Full MS-DTYP IDL documentation, LPSTR is a typedef of char *:

typedef char* PSTR, *LPSTR;

so you can try this:

const char *ch = "some chars ...";
LPSTR lpstr = const_cast<LPSTR>(ch);