简体   繁体   中英

JNI Returning value from a DWord

I have a function with this signature from the header file

SIMAPI_DECL DWORD WINAPI SimReadDwordBuffer (DWORD* pBuffer, 
                                             DWORD dwDwordsToRead, 
                                             DWORD* pdwDwordsRead, 
                                             DWORD dwBlockDwords, 
                                             DWORD dwNoWait);

With the following native call defined

protected native int  SimReadDwordBuffer (int[] pBuffer, 
                                          int dwDwordsToRead, 
                                          int pdwDwordsRead, 
                                          int dwBlockDwords, 
                                          int dwNoWait);

I use javah.exe to create the jni header and it looks like this

protected native int  SimReadDwordBuffer (int[] pBuffer, 
                                          int dwDwordsToRead, 
                                          int pdwDwordsRead, 
                                          int dwBlockDwords, 
                                          int dwNoWait);

And the implementation is this

JNIEXPORT jint JNICALL Java_com_sig_ccm_CcmBase_SimReadDwordBuffer
  (JNIEnv *env, jobject obj, jintArray pBuffer, jint dwWordsToRead, 
                             jint pdwWordsRead, 
                             jint dwBlockWords, jint dwNoWait){
jint *body = env->GetIntArrayElements(pBuffer, 0);
//DWORD foo = 0;
jint value = SimReadDwordBuffer((unsigned long int *)body,
                         dwWordsToRead,
                                     //&foo, 
                         (unsigned long int *)&pdwWordsRead,
                         dwBlockWords,
                         dwNoWait );
//cout << foo;
env->ReleaseIntArrayElements(pBuffer, body, 0);
return value;
}

The problem is no matter what I have tried I cannot get the value pdwWordsRead copied to the parameter I passed from java to jni. If I use a local variable I can write out the value so the c++ function is passing it back. Any suggestions would be appreciated.

Java passes primitives by value, not by reference. Also doing things like address of jint and passing them to functions for population isn't a good idea.

JNIEXPORT jint JNICALL Java_com_sig_ccm_CcmBase_SimReadDwordBuffer
  (JNIEnv *env, jobject obj, jintArray pBuffer, jint dwWordsToRead, 
                             jint pdwWordsRead, 
                             jint dwBlockWords, jint dwNoWait){
jint *body = env->GetIntArrayElements(pBuffer, 0);

// I would consider copying the jint[] to a dword[] here... 
// I would also look into jlong instead of jint because jint 
// is 32-bit signed, and dword is 32-bit unsigned.  Then do 
// something like dword == jlong & 0xFFFFFFFF 

DWORD foo = 0;
jint value = SimReadDwordBuffer((DWORD *) body, // Potentially Bad!
                         (DWORD) dwWordsToRead,
                         &foo, 
                         //(DWORD *) &pdwWordsRead, Bad!
                         dwBlockWords,
                         dwNoWait );
// You need to return an object or something else to get both values out!!! 
env->ReleaseIntArrayElements(pBuffer, body, 0);
return value;
}

Again, I have no idea what value is but if its a success indicator, check it and throw an exception if a failure occurred and return the foo variable instead.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM