Saturday, April 20, 2013

jni jnienv CallFloatMethod example c c++ java


Call<type>Method Routines - CallFloatMethod

Call<type>MethodA Routines
Call<type>MethodV Routines

NativeType Call<type>Method(JNIEnv *env, jobject obj, jmethodID methodID, ...);

NativeType Call<type>MethodA(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args);

NativeType Call<type>MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args);

Methods from these three families of operations are used to call a Java instance method from a native method.They only differ in their mechanism for passing parameters to the methods that they call.(CallFloatMethod)

These families of operations invoke an instance (nonstatic) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When these functions are used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its superclasses. (CallFloatMethod)
Call<type>Method Routines
Programmers place all arguments that are to be passed to the method immediately following the methodID argument. The Call<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.
Call<type>MethodA Routines
Programmers place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The Call<type>MethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.
Call<type>MethodV Routines
Programmers place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The Call<type>MethodV 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 type. You should replace type in Call<type>Method with the Java type of the method you are calling (or use one of the actual method calling routine names from the table) and replace NativeType with the corresponding native type for that routine.

Table 4-3a Instance Method Calling Routines


Call<type>Method Routine Name


Native Type

CallVoidMethod()
CallVoidMethodA()
CallVoidMethodV()

void

CallObjectMethod()
CallObjectMethodA()
CallObjectMethodV()

jobject

CallBooleanMethod()
CallBooleanMethodA()
CallBooleanMethodV()

jboolean

CallByteMethod()
CallByteMethodA()
CallByteMethodV()

jbyte

CallCharMethod()
CallCharMethodA()
CallCharMethodV()

jchar

CallShortMethod()
CallShortMethodA()
CallShortMethodV()

jshort

CallIntMethod()
CallIntMethodA()
CallIntMethodV()

jint

CallLongMethod()
CallLongMethodA()
CallLongMethodV()

jlong

CallFloatMethod()
CallFloatMethodA()
CallFloatMethodV()

jfloat

CallDoubleMethod()
CallDoubleMethodA()
CallDoubleMethodV()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table:
Table 4-3b Instance Method Calling Routines
Call<type>Method Routine Name
Index
CallVoidMethod()
CallVoidMethodA()
CallVoidMethodV()
61
63
62
CallObjectMethod()
CallObjectMethodA()
CallObjectMethodV()
34
36
35
CallBooleanMethod()
CallBooleanMethodA()
CallBooleanMethodV()
37
39
38
CallByteMethod()
CallByteMethodA()
CallByteMethodV()
40
42
41
CallCharMethod()
CallCharMethodA()
CallCharMethodV()
43
45
44
CallShortMethod()
CallShortMethodA()
CallShortMethodV()
46
48
47
CallIntMethod()
CallIntMethodA()
CallIntMethodV()
49
51
50
CallLongMethod()
CallLongMethodA()
CallLongMethodV()
52
54
53
CallFloatMethod()
CallFloatMethodA()
CallFloatMethodV()
55
57
56
CallDoubleMethod()
CallDoubleMethodA()
CallDoubleMethodV()
58
60
59
PARAMETERS:
env: the JNI interface pointer.

obj: a Java object.

methodID: a method ID.
Additional Parameter for Call<type>Method Routines:
arguments to the Java method.
Additional Parameter for Call<type>MethodA Routines:
args: an array of arguments.
Additional Parameter for Call<type>MethodV Routines:
args: a va_list of arguments.
RETURNS:
Returns the result of calling the Java method.
THROWS:
Exceptions raised during the execution of the Java method.

Example - CallFloatMethod

JniMethodInfo t;
JniMethodInfo minfo;
jobject jobj;
if(JniHelper::getStaticMethodInfo(minfo, "com/WBS/Test0001/Test0001", "cppCall_logsth", "()Ljava/lang/Object;")){
jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
if(JniHelper::getMethodInfo(minfo, "com/WBS/Test0001/Test0001", "getResolution", "()F")){
jfloat val = minfo.env->CallFloatMethod(jobj, minfo.methodID);
width = val;
}
}

corresponding java functions are
@public float getResolution() {
Context context = Test0001.instance;
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
// create array with size
        int size[] = {display.getWidth(), display.getHeight()};
        Log.i("MyActivity", "width is " + display.getWidth());
return display.getWidth();
    }
public static Object cppCall_logsth(){
    Log.i("cppCall", "test~~~~!!!");
        return Test0001.instance;
    }@


jni jnienv CallIntMethod example c c++ java


Call<type>Method Routines - CallIntMethod

Call<type>MethodA Routines
Call<type>MethodV Routines

NativeType Call<type>Method(JNIEnv *env, jobject obj, jmethodID methodID, ...);

NativeType Call<type>MethodA(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args);

NativeType Call<type>MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args);

Methods from these three families of operations are used to call a Java instance method from a native method.They only differ in their mechanism for passing parameters to the methods that they call.(CallIntMethod)

These families of operations invoke an instance (nonstatic) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When these functions are used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its superclasses. (CallIntMethod)
Call<type>Method Routines
Programmers place all arguments that are to be passed to the method immediately following the methodID argument. The Call<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.
Call<type>MethodA Routines
Programmers place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The Call<type>MethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.
Call<type>MethodV Routines
Programmers place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The Call<type>MethodV 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 type. You should replace type in Call<type>Method with the Java type of the method you are calling (or use one of the actual method calling routine names from the table) and replace NativeType with the corresponding native type for that routine.

Table 4-3a Instance Method Calling Routines


Call<type>Method Routine Name


Native Type

CallVoidMethod()
CallVoidMethodA()
CallVoidMethodV()

void

CallObjectMethod()
CallObjectMethodA()
CallObjectMethodV()

jobject

CallBooleanMethod()
CallBooleanMethodA()
CallBooleanMethodV()

jboolean

CallByteMethod()
CallByteMethodA()
CallByteMethodV()

jbyte

CallCharMethod()
CallCharMethodA()
CallCharMethodV()

jchar

CallShortMethod()
CallShortMethodA()
CallShortMethodV()

jshort

CallIntMethod()
CallIntMethodA()
CallIntMethodV()

jint

CallLongMethod()
CallLongMethodA()
CallLongMethodV()

jlong

CallFloatMethod()
CallFloatMethodA()
CallFloatMethodV()

jfloat

CallDoubleMethod()
CallDoubleMethodA()
CallDoubleMethodV()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table:
Table 4-3b Instance Method Calling Routines
Call<type>Method Routine Name
Index
CallVoidMethod()
CallVoidMethodA()
CallVoidMethodV()
61
63
62
CallObjectMethod()
CallObjectMethodA()
CallObjectMethodV()
34
36
35
CallBooleanMethod()
CallBooleanMethodA()
CallBooleanMethodV()
37
39
38
CallByteMethod()
CallByteMethodA()
CallByteMethodV()
40
42
41
CallCharMethod()
CallCharMethodA()
CallCharMethodV()
43
45
44
CallShortMethod()
CallShortMethodA()
CallShortMethodV()
46
48
47
CallIntMethod()
CallIntMethodA()
CallIntMethodV()
49
51
50
CallLongMethod()
CallLongMethodA()
CallLongMethodV()
52
54
53
CallFloatMethod()
CallFloatMethodA()
CallFloatMethodV()
55
57
56
CallDoubleMethod()
CallDoubleMethodA()
CallDoubleMethodV()
58
60
59
PARAMETERS:
env: the JNI interface pointer.

obj: a Java object.

methodID: a method ID.
Additional Parameter for Call<type>Method Routines:
arguments to the Java method.
Additional Parameter for Call<type>MethodA Routines:
args: an array of arguments.
Additional Parameter for Call<type>MethodV Routines:
args: a va_list of arguments.
RETURNS:
Returns the result of calling the Java method.
THROWS:
Exceptions raised during the execution of the Java method.

Example - CallIntMethod

public class IniManipulate {
   private int getIniParamInt(String mezoNev)
                  {return 999;}  // settings.getInt("abc", -9999);
C code:
const char* paramOut 
paramOut = "abc";
jmethodID mid = (*env)->GetMethodID(env,cls1,"getIniParamInt","(Ljava/lang/String;)I");
     if (mid == NULL) {cDebug1 = 888;return; }
jstring* parameter = (*env)->NewStringUTF(env, paramOut);
     if (parameter == NULL) {return;}
jint paramInt = (jint) (*env)->CallIntMethod(env,thiz, mid, parameter);
cDebug1 = (int)paramInt;

jni jnienv CallShortMethod example c c++ java


Call<type>Method Routines - CallShortMethod

Call<type>MethodA Routines
Call<type>MethodV Routines

NativeType Call<type>Method(JNIEnv *env, jobject obj, jmethodID methodID, ...);

NativeType Call<type>MethodA(JNIEnv *env, jobject obj, jmethodID methodID, jvalue *args);

NativeType Call<type>MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args);

Methods from these three families of operations are used to call a Java instance method from a native method.They only differ in their mechanism for passing parameters to the methods that they call.(CallShortMethod)

These families of operations invoke an instance (nonstatic) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When these functions are used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its superclasses. (CallShortMethod)
Call<type>Method Routines
Programmers place all arguments that are to be passed to the method immediately following the methodID argument. The Call<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke.
Call<type>MethodA Routines
Programmers place all arguments to the method in an args array of jvalues that immediately follows the methodID argument. The Call<type>MethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.
Call<type>MethodV Routines
Programmers place all arguments to the method in an args argument of type va_list that immediately follows the methodID argument. The Call<type>MethodV 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 type. You should replace type in Call<type>Method with the Java type of the method you are calling (or use one of the actual method calling routine names from the table) and replace NativeType with the corresponding native type for that routine.

Table 4-3a Instance Method Calling Routines


Call<type>Method Routine Name


Native Type

CallVoidMethod()
CallVoidMethodA()
CallVoidMethodV()

void

CallObjectMethod()
CallObjectMethodA()
CallObjectMethodV()

jobject

CallBooleanMethod()
CallBooleanMethodA()
CallBooleanMethodV()

jboolean

CallByteMethod()
CallByteMethodA()
CallByteMethodV()

jbyte

CallCharMethod()
CallCharMethodA()
CallCharMethodV()

jchar

CallShortMethod()
CallShortMethodA()
CallShortMethodV()

jshort

CallIntMethod()
CallIntMethodA()
CallIntMethodV()

jint

CallLongMethod()
CallLongMethodA()
CallLongMethodV()

jlong

CallFloatMethod()
CallFloatMethodA()
CallFloatMethodV()

jfloat

CallDoubleMethod()
CallDoubleMethodA()
CallDoubleMethodV()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table:
Table 4-3b Instance Method Calling Routines
Call<type>Method Routine Name
Index
CallVoidMethod()
CallVoidMethodA()
CallVoidMethodV()
61
63
62
CallObjectMethod()
CallObjectMethodA()
CallObjectMethodV()
34
36
35
CallBooleanMethod()
CallBooleanMethodA()
CallBooleanMethodV()
37
39
38
CallByteMethod()
CallByteMethodA()
CallByteMethodV()
40
42
41
CallCharMethod()
CallCharMethodA()
CallCharMethodV()
43
45
44
CallShortMethod()
CallShortMethodA()
CallShortMethodV()
46
48
47
CallIntMethod()
CallIntMethodA()
CallIntMethodV()
49
51
50
CallLongMethod()
CallLongMethodA()
CallLongMethodV()
52
54
53
CallFloatMethod()
CallFloatMethodA()
CallFloatMethodV()
55
57
56
CallDoubleMethod()
CallDoubleMethodA()
CallDoubleMethodV()
58
60
59
PARAMETERS:
env: the JNI interface pointer.

obj: a Java object.

methodID: a method ID.
Additional Parameter for Call<type>Method Routines:
arguments to the Java method.
Additional Parameter for Call<type>MethodA Routines:
args: an array of arguments.
Additional Parameter for Call<type>MethodV Routines:
args: a va_list of arguments.
RETURNS:
Returns the result of calling the Java method.

THROWS:
Exceptions raised during the execution of the Java method.

Example - CallShortMethod

  if     (env->IsInstanceOf(valueObject, boolClass)           == JNI_TRUE)
    {
        jmethodID booleanValueMID   = env->GetMethodID(boolClass, "booleanValue", "()Z");
        bool booleanValue           = (bool) env->CallBooleanMethod(valueObject, booleanValueMID);
        addBoolean(key, booleanValue);
    }
    else if(env->IsInstanceOf(valueObject, shortClass)           == JNI_TRUE)
    {
        jmethodID characterValueMID  = env->GetMethodID(shortClass, "shortValue", "()S");
        short shortValue          = (short) env->CallShortMethod(valueObject, shortValueMID);
        addShort   (key, shortValue);
    }