Parameter conversion between Java and C

Source: Internet
Author: User

1: returns a string in C.
...................
(* Env)-> newstringutf (ENV, "Zhongguo China ");
...................

2: C returns an array.
.....................
Int I = 0;
Jintarray array;
Array = (* env)-> newintarray (ENV, 10 );
For (; I <10; I ++)
{
(* Env)-> setobjectarrayelement (ENV, array, I, I * 2 );
}
Return array;

3: In C, the input parameter called is array, which is the input array.
.........
Int sum = 0, I;
Int Len = (* env)-> getarraylength (ENV, array );
Jint * element = (* env)-> getintarrayelement (ENV, array, 0 );
For (I = 0; I <Len; I ++)
{
Sum + = * (element + I );
}
Return sum;

4: In C, calling methods of classes in Java has no parameters. Only the return value is string.
@ "() Ljava/lang/string;" indicates that if the parameter is null, the returned value is of the string type.
Jniexport jstring jnicall {
Tagshow (Event)
} "> Java_tao_hai_bing_demo_getcallback (jnienv ENV, jobject object ){

Jmethodid mid;
Jclass CLS = (* env)-> findclass (ENV, "Tao/HAI/Bing/demo"); // package name + class name
Mid = (* env)-> getmethodid (ENV, CLS, "Callback", "() ljava/lang/string;"); // method name in callback Java
Jstring MSG = (* env)-> callobjectmethod (ENV, object, mid); // pay attention to the jobject passed by JNI.
Return MSG;
}

5: Calling static methods of classes in Java in C without parameters, only return values: String
@ "() Ljava/lang/string;" indicates that if the parameter is null, the returned value is of the string type.
Jniexport jstring jnicall java_tao_hai_bing_demo_getcallback (jnienv ENV, jobject object ){

Jmethodid mid;
Jclass CLS = (* env)-> findclass (ENV, "Tao/HAI/Bing/demo"); // package name + class name
Mid = (* env)-> gestatictmethodid (ENV, CLS, "Callback", "() ljava/lang/string;"); // method name in callback Java
Jstring MSG = (* env)-> callstaticobjectmethod (ENV, CLS, mid); // note that the jobject passed by JNI
Return MSG;
}

6: Call methods in Java in C. Two parameters. The first parameter is int. The second parameter is string. the return value is string.
@ "(Iljava/lang/string;) ljava/lang/string" indicates that the parameter is the first parameter, and the second parameter is the integer parameter. The return value of string is the string parameter.
Jniexport jstring jnicall java_tao_hai_bing_demo_getcallback (jnienv ENV, jobject object ){

Jmethodid mid;
Jclass CLS = (* env)-> findclass (ENV, "Tao/HAI/Bing/demo"); // package name + class name
Mid = (* env)-> gestatictmethodid (ENV, CLS, "Callback", "(iljava/lang/string;) ljava/lang/string ;"); // method name in callback Java
Jstring Param = (* env)-> newstringutf (ENV, "taohaibing ");
Jstring MSG = (* env)-> callstaticobjectmethod (ENV, CLS, mid, 22, Param); // The jobject passed by JNI
Return MSG;
}

8: calling global variables in Java in C
Jclass CLS = (* env)-> findclass (ENV, "Tao/HAI/Bing/demo ");
Jfieldid id = (* env)-> getfieldid (ENV, CLS, "num", "I"); // If num is the variable I in Java, the type of the variable is integer.
Jint Param = (* env)-> getintfield (ENV, object, ID );

Jfieldid Id2 = (* env)-> getfieldid (ENV, CLS, "num2", "ljava/lang/string "); // num2 is the variable I in Java, which indicates that the variable type is string.
Jstring Param = (* env)-> getobjectfield (ENV, object, ID );

8: Call static variables in Java in C
Jclass CLS = (* env)-> findclass (ENV, "Tao/HAI/Bing/demo ");
Jfieldid id = (* env)-> getstaticfieldid (ENV, CLS, "num", "ljava/lang/string "); // If num is the variable I in Java, the type of the variable is integer.
Jstring Param = (* env)-> gestaticobjectfield (ENV, CLS, ID );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.