Tuesday, April 23, 2013

jni jnienv CallStaticVoidMethod example c c++ java


CallStatic<type>Method Routines - CallStaticVoidMethod
CallStatic<type>MethodA Routines
CallStatic<type>MethodV Routines


NativeType CallStatic<type>Method(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);

NativeType CallStatic<type>MethodA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);

NativeType CallStatic<type>MethodV(JNIEnv *env, jclass clazz,
jmethodID methodID, va_list args);

(CallStaticVoidMethod)This family of operations invokes a static method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.
(CallStaticVoidMethod)

CallStatic<type>Method Routines

Programmers should place all arguments that are to be passed to the method immediately following the methodID argument. TheCallStatic<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke. (CallStaticVoidMethod)

CallStatic<type>MethodA Routines

Programmers should place all arguments to the method in an args array of jvalues that immediately follows the methodIDargument. The CallStaticMethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.

CallStatic<type>MethodV Routines

Programmers should place all arguments to the method in an args argument of type va_list that immediately follows the methodIDargument. The CallStaticMethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

The following table describes each of the method calling routines according to their result types. You should replace type inCallStatic<type>Method with the Java type of the method, or one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-7a CallStatic<type>Method Calling Routines

CallStatic<type>Method Routine Name

Native Type

CallStaticVoidMethod()

CallStaticVoidMethodA()

CallStaticVoidMethodV()

void

CallStaticObjectMethod()

CallStaticObjectMethodA()

CallStaticObjectMethodV()

jobject

CallStaticBooleanMethod()

CallStaticBooleanMethodA()

CallStaticBooleanMethodV()

jboolean

CallStaticByteMethod()

CallStaticByteMethodA()

CallStaticByteMethodV()

jbyte

CallStaticCharMethod()

CallStaticCharMethodA()

CallStaticCharMethodV()

jchar

CallStaticShortMethod()

CallStaticShortMethodA()

CallStaticShortMethodV()

jshort

CallStaticIntMethod()

CallStaticIntMethodA()

CallStaticIntMethodV()

jint

CallStaticLongMethod()

CallStaticLongMethodA()

CallStaticLongMethodV()

jlong

CallStaticFloatMethod()

CallStaticFloatMethodA()

CallStaticFloatMethodV()

jfloat

CallStaticDoubleMethod()

CallStaticDoubleMethodA()

CallStaticDoubleMethodV()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-7b CallStatic<type>Method Calling Routines
CallStatic<type>Method Routine Name
Index
CallStaticVoidMethod()
CallStaticVoidMethodA()
CallStaticVoidMethodV()
141
143
142
CallStaticObjectMethod()
CallStaticObjectMethodA()
CallStaticObjectMethodV()
114
116
115
CallStaticBooleanMethod()
CallStaticBooleanMethodA()
CallStaticBooleanMethodV()
117
119
118
CallStaticByteMethod()
CallStaticByteMethodA()
CallStaticByteMethodV()
120
122
121
CallStaticCharMethod()
CallStaticCharMethodA()
CallStaticCharMethodV()
123
125
124
CallStaticShortMethod()
CallStaticShortMethodA()
CallStaticShortMethodV()
126
128
127
CallStaticIntMethod()
CallStaticIntMethodA()
CallStaticIntMethodV()
129
131
130
CallStaticLongMethod()
CallStaticLongMethodA()
CallStaticLongMethodV()
132
134
133
CallStaticFloatMethod()
CallStaticFloatMethodA()
CallStaticFloatMethodV()
135
137
136
CallStaticDoubleMethod()
CallStaticDoubleMethodA()
CallStaticDoubleMethodV()
138
140
139
PARAMETERS  of CallStaticVoidMethod

env: the JNI interface pointer.

clazz: a Java class object.

methodID: a static method ID.

Additional Parameter for CallStatic<type>Method Routines:

arguments to the static method.

Additional Parameter for CallStatic<type>MethodA Routines:

args: an array of arguments.

Additional Parameter for CallStatic<type>MethodV Routines:

args: a va_list of arguments.

RETURNS:

Returns the result of calling the static Java method.

THROWS:

Exceptions raised during the execution of the Java method.
Example of CallStaticVoidMethod


void jni_call_received_hook(char* username){
 JNIEnv* m_env;
 (*m_vm)->AttachCurrentThread(m_vm, (void**) &m_env, NULL );
 jclass cls = (*m_env)->FindClass( m_env, "gui/StateMachine" );
 jmethodID mid = (*m_env)->GetStaticMethodID(m_env, cls, "callReceivedEvent", "(Ljava/lang/String;)V");
 if (mid == 0){
   log(E, "Unable to find method for callback");
   return;
 }
 (*m_env)->CallStaticVoidMethod(m_env, cls, mid, (*m_env)->NewStringUTF(m_env, username));
}