Search This Blog

Showing posts with label JNI. Show all posts
Showing posts with label JNI. Show all posts

Wednesday, July 17, 2019

JNI calls in Multi-thread application (or call back from java to C++ )

When you try to accee
Recently i noticed when i tried to access my global JNIEnv  from other thread ( not the one who initialize it) , i get failed to call to FindClass.

from the document :
"The JNI interface pointer (JNIEnv) is valid only in the current thread"

so you must call to AttachCurrentThread() method first on the new thread, and detach it via DetachCurrentThread()

you can read more about it at 
The Invocation API



enjoy
Yaniv Tzanany

Wednesday, October 9, 2013

Passing jstring to JAVA from C++ - the right way.

hi
i used to use this function
m_env->NewStringUTF(formattedMsg);

and one day i saw that the string i passed in my case it was huge more than 500k, cut in the end.
so i replcae this function with this one ... and its worked !!

jstring WindowsToJstring(JNIEnv* pEnv, LPCTSTR cstr) {
jstring retJstring = NULL;
int slen = strlen(cstr);
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)cstr, slen, NULL, 0 );
unsigned short* tempbuffer = (unsigned short *)malloc( length*2 + 1 );
MultiByteToWideChar( CP_ACP, 0, (LPCSTR)cstr, slen, (LPWSTR)tempbuffer, length );
retJstring = (pEnv)->NewString((jchar*)tempbuffer, length );
free( tempbuffer );
return retJstring;
}

more details could be found here
http://www.anyang-window.com.cn/between-java-and-c-transmission-through-jni-chinese-string-and-hash-issues/

and
http://stackoverflow.com/questions/8258834/sending-utf-chars-to-java-from-c-using-jni

Monday, July 30, 2012

JNI - get method signature

sometimes when you mixed java and C++ code you must used JNI.
So the best way to find the method signature is via javap application that exist in JAVA jdk.

in case you want to see the method  signature to initialize your jmethodID  follow the next steps:

  1. open command windows
  2. navigate to your class directory (where the class file exist, e.g. c:\xxx\bin\com\)
  3. run the javap.exe -c -s [class name]    ( without the .class e.g. javap -c -s myclass)
make sure your javap is in the PATH , or write the full path to it.

enjoy
Yaniv Tzanany