在没有USES_CONVERSTION的情况下从const char*转换为LPTSTR

San*_*thi 6 c++ java-native-interface lptstr

我试图将const char*转换为LPTSTR.但我不想使用USES_CONVERSION来执行此操作.

以下是我用于使用USES_CONVERSION转换的代码.有没有办法转换使用sprintf或tcscpy等..?

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..
Run Code Online (Sandbox Code Playgroud)

Rez*_*imi 8

LPTSTR 有两种模式:

一个LPWSTR如果UNICODE被定义,一个LPSTR否则.

#ifdef UNICODE
    typedef LPWSTR LPTSTR;
#else
    typedef LPSTR LPTSTR;
#endif
Run Code Online (Sandbox Code Playgroud)

或者通过其他方式:

LPTSTR is wchar_t* or char* depending on _UNICODE
Run Code Online (Sandbox Code Playgroud)

如果你LPTSTR是非unicode:

根据MSDN全MS-DTYP IDL文档,LPSTR是一个typedefchar *:

typedef char* PSTR, *LPSTR;
Run Code Online (Sandbox Code Playgroud)

所以你可以尝试这个:

const char *ch = "some chars ...";
LPSTR lpstr = const_cast<LPSTR>(ch);
Run Code Online (Sandbox Code Playgroud)