I am using JNI to call the native C++ layer.
java layer
int res= recog(audioFilePath, grammarFilePath, contextID, subContextID);
C++ layer
JNIEXPORT void JNICALL Java_com_uniphore_voice_recogniser_NuanceOfflineRecogniser_recog(JNIEnv *jenv, jobject jobj, jstring jaudioFilePath,
jstring jgrammarFilePath, <br/>
jstring jcontextID,
jstring jsubContext)
{
const char* _audioFilePath = (char*)jenv->GetStringChars(jaudioFilePath, JNI_FALSE);
const char* _grammarFilePath = (char*)jenv->GetStringChars(jgrammarFilePath, JNI_FALSE);
const char* _contextId = (char*)jenv->GetStringChars(jcontextID, JNI_FALSE);
const char* _subContextId = (char*)jenv->GetStringChars(jsubContext, JNI_FALSE);
std::wcout << "audio file path: " << _audioFilePath <<" "<< std::strlen(_audioFilePath) <<std::endl
<< "grammar file path: "<< _grammarFilePath <<" "<<std::strlen(_grammarFilePath) << std::endl
<< "contextId: " << _contextId << std::endl
<< "subContextId: " << _subContextId << std::endl << std::endl;
I can see at java layer values is properly passed to the lower level but in c++ layer while printing that value in C++ layer I can see it is printing only first character of whole string.
suppose if audioFilePath I am passing like "c:\\test.wav" I am getting print in c++ layer only like c
I am trying in visual studio 2013 and project character support I selected as Unicode support.
I am new to c++ environment, please help to get the reason for this one.
According to JNI docs GetStringChars
returns the unicode characters for the given string in a jchar *
which is an unsigned short *
. You cast it to a char *
. When you use cout
with a char *
it expects a string in ASCII format with a null-terminator. You pass it a pointer to a string in unicode format, which has every other character 0
for plain ASCII characters. Hence why you only print the first character in the string.
GetStringChars is not returning a pointer to single byte characters, but two byte, unicode characters
const jchar * GetStringChars(JNIEnv *env, jstring string,
jboolean *isCopy);
Returns a pointer to the array of Unicode characters of the string.
Instead, try
GetStringUTFChars
This will be null terminated also.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.