Convert from const char * to LPTSTR without USES_CONVERSTION

I am trying to convert const char * to LPTSTR. But I do not want to use USES_CONVERSION to accomplish this.

Below is the code that 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.. 
+4
source share
2 answers

LPTSTR has two modes:

An LPWSTR , if UNICODE specified, LPSTR otherwise.

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

or in another way:

 LPTSTR is wchar_t* or char* depending on _UNICODE 

if your LPTSTR not unicode:

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

 typedef char* PSTR, *LPSTR; 

so you can try the following:

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

USES_CONVERSION and related macros are the easiest way to do this. Why not use them? But you can always just check if UNICODE or _UNICODE macros are defined. If none of these are defined, no conversion is required. If one of them is defined, you can use MultiByteToWideChar to perform the conversion.

This is actually a stupid thing. JNIEnv alreay has a way to get characters as Unicode: JNIEnv :: GetStringChars. So just check the UNICODE and _UNICODE macros to find out which method to use:

 #if defined(UNICODE) || defined(_UNICODE) LPTSTR lpszVal = env->GetStringChars(JavaStringVal, 0); #else LPTSTR lpszVal = env->GetStringUTFChars(JavaStringVal, 0); #endif 

In fact, if you don't want to pass a string to a method that expects LPTSTR, you should just use only the Unicode version. Java strings are stored as Unicode internally, so you won’t get conversion overhead, and Unicode strings are best overall.

0
source

All Articles