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.
source share