JNI _10_jni Comprehensive Development

Source: Internet
Author: User

1. If you cannot find a class when using Ndk-build, you can use the following two methods to resolve it:

1, 1 into the SRC directory

D:\project3\jniandroid\src>set CLASSPATH=D:\PROJECT3\JNIANDROID\SRC

1, 2 set Classpath
Classpath.; %java_home%\lib\dt.jar;%java_home%\tools.jar; E:\developLib\android\platforms\android-8\android.jar

2. Basic class

Ublic class Nativemodule {public    native int testarg (int i, Boolean B, Char C, double d);    Public native byte[] Testbyte (byte[] b);    Public native string[] TestString (String s, string[] sarr);    public native int SetInfo (myinfo info);    Public native MyInfo GetInfo ();    static {       system.loadlibrary ("Nativemodule");}    }

Where the MyInfo class is defined as follows: public class Record {    int id;    String name;    byte[] data;} public class MyInfo {public    Boolean B;    public char C;    public double D;    public int i;    Public byte[] array;    public String s;    Public Record Rec;}

c struct Definition

typedef struct{    int ID;    Char name[255];    Char data[255];} Record; typedef struct{    BOOL B;    char c;    Double D;    int i;    Char arr[255];    Char sz[255];    Record Rec;} MyInfo;

3. Write C library

3.1) Examples of different types of Java and C parameter conversions

Different types of parameter handling Jniexport jintjnicall java_com_jinhill_util_nativemodule_testarg  (jnienv *env, Jobject Jo, Jint ji, JBOOLEANJB,  Jchar JC, jdouble JD) {         //Get Jint type value         int i = ji;         Get Jboolean Type value         BOOL b = JB;         Gets the jdouble type value         double d = JD;         Gets the Jchar type value, Java of char two byte         char ch[5] = {0};         int size = 0;         Size = WideCharToMultiByte (Cp_acp,null, (LPCWSTR) &JC,-1, CH, 5, NULL, FALSE);         if (size <= 0) {                   return-1;         }         Trace ("Ji=%d,jb=%d,jc=%s,jd=%lf", I, B, ch, d);         return 0;}

3.2) Java byte and C char array type array conversion instance

Btye array processing, parameter as input or output, return Btye array Jniexportjbytearray jnicall java_com_jinhill_util_nativemodule_testbyte (JNIEnv *env,         Jobject Jo, Jbytearray jbarr) {char chtmp[] = "Hello jni!";         int ntmplen = strlen (chtmp);         Get Jbytearray char *charr = (char*) env->getbytearrayelements (jbarr,0);         Get jbytearray length int narrlen = Env->getarraylength (Jbarr);         Char *szstrbuf = (char*) malloc (NARRLEN*2+10);         memset (szstrbuf, 0, narrlen*2+10);         Bytes2string (Charr, Narrlen, szstrbuf,narrlen*2+10);         Trace ("jbarr=%s", szstrbuf);         The Jbarr is used as the output parameter memset (charr, 0, Narrlen);         memcpy (Charr, chtmp, Ntmplen);     Return to Jbytearray Jbytearray JARRRV =env->newbytearray (Ntmplen);         Jbyte *jby =env->getbytearrayelements (JARRRV, 0);    memcpy (Jby, Chtmp, strlen (chtmp));          Env->setbytearrayregion (JARRRV, 0,ntmplen, Jby); return JARRRV;}

 3.3)   java string and C char array type conversion instance  
String and string[] handle Jniexportjobjectarray jnicall java_com_jinhill_util_nativemodule_teststring (JNIEnv *env,         Jobject Jo, jstring jstr,jobjectarray joarr) {int i = 0;         Char chtmp[50] = {0};         Get jstring value const char* PSZSTR = (char*) env->getstringutfchars (jstr, 0);         Trace ("jstr=%s", pszstr);         Gets the Jobjectarray value int narrlen =env->getarraylength (Joarr);         Trace ("Joarr len=%d", Narrlen);                   for (i=0; i<narrlen; i++) {jstring js = (jstring) env->getobjectarrayelement (Joarr, i);                   Const char* PSZ = (char*) env->getstringutfchars (JS, 0);         Trace ("joarr[%d]=%s", I, psz);         }//Joarr as the output parameter jstring jstrtmp = NULL;                   for (i=0; i<narrlen; i++) {sprintf (chtmp, "No.%dhello jni!", i);                   Jstrtmp =env->newstringutf (chtmp);                   Env->setobjectarrayelement (Joarr,i, jstrtmp); Env->D Eletelocalref (jstrtmp);         }//Return to Jobjectarray jclass jstrcls =env->findclass ("ljava/lang/string;");         Jobjectarray Jstrarray =env->newobjectarray (2, Jstrcls, NULL);                   for (i=0; i<2; i++) {sprintf (chtmp, "No.%dreturn jni!", i);                   Jstrtmp =env->newstringutf (chtmp);                   Env->setobjectarrayelement (Jstrarray,i, jstrtmp);         Env->deletelocalref (JSTRTMP); } return Jstrarray;}
 3.4)   java class and C struct type conversion instance
Jniexport jint jnicall java_com_jinhill_util_nativemodule_setinfo (jnienv *env, Jobject Jo, Jobject jobj) {char ChHexT    MP[512] = {0};    Convert Java class to C structure MyInfo mi;    Gets the instance class in Java Record jclass Jcrec = Env->findclass ("Com/jinhill/util/record");    int ID Jfieldid jfid = env->getfieldid (Jcrec, "id", "I");    String name Jfieldid jfname = Env->getfieldid (Jcrec, "name", "ljava/lang/string;");    byte[] data;    Jfieldid jfdata = Env->getfieldid (Jcrec, "Data", "[B");    Gets the instance class in Java myinfo jclass jcinfo = Env->findclass ("Com/jinhill/util/myinfo");    Gets the definition of each variable in the class//boolean b jfieldid jfb = Env->getfieldid (Jcinfo, "B", "Z");    Char c jfieldid JFC = Env->getfieldid (Jcinfo, "C", "C");    Double D Jfieldid jfd = Env->getfieldid (Jcinfo, "D", "D");    int i jfieldid JFI = Env->getfieldid (Jcinfo, "I", "I");    byte[] array Jfieldid JFA = Env->getfieldid (jcinfo, "array", "[B"); String s Jfieldid jfs = ENV-&GT    Getfieldid (Jcinfo, "s", "ljava/lang/string;");    Record Rec;     Jfieldid Jfrec = Env->getfieldid (Jcinfo, "Rec", "lcom/jinhill/util/record;");    Gets the value of the variable B of the instance mi.b = Env->getbooleanfield (Jobj, JFB);    Gets the value of the variable C of the instance Jchar JC = Env->getcharfield (Jobj, JFC);    Char Ch[5] = {0};    int size = 0;    Size = WideCharToMultiByte (CP_ACP, NULL, (LPCWSTR) &AMP;JC,-1, CH, 5, NULL, FALSE);    MI.C = ch[0];    Gets the value of the variable d of the instance MI.D = Env->getdoublefield (Jobj, JFD);    Gets the value of the variable I for the instance mi.i = Env->getintfield (Jobj, JFI);    Gets the value of the instance variable array jbytearray ja = (jbytearray) Env->getobjectfield (Jobj, JFA);    int narrlen = env->getarraylength (JA);    Char *charr = (char*) env->getbytearrayelements (ja, 0);    memcpy (Mi.arr, Charr, Narrlen);    Gets the value of the instance's variable s jstring jstr = (jstring) Env->getobjectfield (Jobj, JFS);    Const char* PSZSTR = (char*) env->getstringutfchars (jstr, 0);     strcpy (MI.SZ, PSZSTR); Gets the Record object Jobject Jorec = Env->getobjectfield (Jobj, JFREC);    Gets the Record object id value mi.rec.id = Env->getintfield (Jorec, Jfid);    Trace ("mi.rec.id=%d", mi.rec.id);    Gets the Record object name value jstring jstrn = (jstring) Env->getobjectfield (Jorec, jfname);    Pszstr = (char*) env->getstringutfchars (jstrn, 0);    strcpy (Mi.rec.name, PSZSTR);    Gets the Record object data value Jbytearray JBD = (jbytearray) Env->getobjectfield (Jorec, jfdata);    Narrlen = Env->getarraylength (JBD);    Charr = (char*) env->getbytearrayelements (JBD, 0);    memcpy (Mi.rec.data, Charr, Narrlen);    Log Output bytes2string (Mi.arr, Narrlen, chhextmp, sizeof (CHHEXTMP));  Trace ("mi.arr=%s, mi.b=%d, mi.c=%c, MI.D=%LF, mi.i=%d, \ n mi.sz=%s\n mi.rec.id=%d, mi.rec.name=%s", Chhextmp, MI.B, MI.C,     MI.D, Mi.i, Mi.sz, Mi.rec.id, mi.rec.name); return 0;}
 3.5)   c struct type and Java Class conversion example   
Jniexport jobject jnicall java_com_jinhill_util_nativemodule_getinfo (jnienv *env, Jobject Jo) {wchar_t wStr[255] = {0}    ;    Char chtmp[] = "Hello JNI";    int ntmplen = strlen (chtmp);    Convert c structure to Java class MyInfo mi;    memcpy (Mi.arr, Chtmp, strlen (chtmp));    MI.B = TRUE;    MI.C = ' B ';    MI.D = 2000.9;    MI.I = 8;    strcpy (Mi.sz, "Hello world!");    Mi.rec.id = 2011;    memcpy (Mi.rec.data, "\x01\x02\x03\x04\x05\x06", 6);    strcpy (Mi.rec.name, "My JNI");    Gets the instance class in Java Record jclass Jcrec = Env->findclass ("Com/jinhill/util/record");    int ID Jfieldid jfid = env->getfieldid (Jcrec, "id", "I");    String name Jfieldid jfname = Env->getfieldid (Jcrec, "name", "ljava/lang/string;");    byte[] data;     Jfieldid jfdata = Env->getfieldid (Jcrec, "Data", "[B");    Gets the instance class in Java Jclass jcinfo = Env->findclass ("Com/jinhill/util/myinfo");    Gets the definition of each variable in the class//boolean b jfieldid jfb = Env->getfieldid (Jcinfo, "B", "Z"); Char c jfieldid JFC =Env->getfieldid (Jcinfo, "C", "C");    Double D Jfieldid jfd = Env->getfieldid (Jcinfo, "D", "D");    int i jfieldid JFI = Env->getfieldid (Jcinfo, "I", "I");    byte[] array Jfieldid JFA = Env->getfieldid (jcinfo, "array", "[B");    String s Jfieldid jfs = Env->getfieldid (Jcinfo, "s", "ljava/lang/string;");    Record Rec;     Jfieldid Jfrec = Env->getfieldid (Jcinfo, "Rec", "lcom/jinhill/util/record;");     Create a new object Jobject Jorec = Env->allocobject (JCREC);     Env->setintfield (Jorec, Jfid, mi.rec.id);    Jstring jstrn = Env->newstringutf (mi.rec.name);     Env->setobjectfield (Jorec, Jfname, JSTRN);     Jbytearray Jbarr = Env->newbytearray (6);    Jbyte *JB = env->getbytearrayelements (Jbarr, 0);    memcpy (JB, Mi.rec.data, 6);    Env->setbytearrayregion (Jbarr, 0, 6, JB);    Env->setobjectfield (Jorec, Jfdata, Jbarr);     Create a new object Jobject joinfo = Env->allocobject (Jcinfo); Assigns a value to a class member Env->setbooleanfield (Joinfo, JFB, MI.B);//MultiByteToWideChar (CP_ACP, 0, Mi.c,-1, WSTR, 255);//Env->setcharfield (Joinfo, JFC, (Jchar) wstr);    Env->setcharfield (Joinfo, JFC, (Jchar) mi.c);    Env->setdoublefield (Joinfo, JFD, MI.D);    Env->setintfield (Joinfo, Jfi, mi.i);     Jbytearray Jarr = Env->newbytearray (Ntmplen);    Jbyte *jby = env->getbytearrayelements (Jarr, 0);    memcpy (Jby, Mi.arr, Ntmplen);    Env->setbytearrayregion (Jarr, 0, Ntmplen, jby);    Env->setobjectfield (Joinfo, JFA, Jarr);    Jstring jstrtmp = Env->newstringutf (chtmp);    Env->setobjectfield (Joinfo, JFS, jstrtmp);    Env->setobjectfield (Joinfo, Jfrec, Jorec); return joinfo;}
3.6) Chinese problem when converting Java string to C char array

Convert jstring type to Windows type char* jstringtowindows (jnienv *env, jstring jstr) {<span style= "White-space:pre" ></ Span>int length = (env)->getstringlength (JSTR) <span style= "White-space:pre" ></span>const jchar* Jcstr = (env)->getstringchars (jstr, 0), <span style= "White-space:pre" ></span>char* Rtn = (char*) malloc ( length*2+1); <span style= "White-space:pre" ></span>int size = 0;<span style= "White-space:pre" ></ Span>size = WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) Jcstr, Length,rtn, (length*2+1), NULL, NULL); <span style= " White-space:pre "></span>if (size <= 0) <span style=" White-space:pre "></span>return NULL;< Span style= "White-space:pre" ></span> (env)->releasestringchars (JSTR, jcstr); <span style= " White-space:pre "></span>rtn[size] = 0;<span style=" White-space:pre "></span>return Rtn;} Convert Windows type to jstring type jstring windowstojstring (jnienv* env, char* str) {<span styLe= "White-space:pre" ></span>jstring Rtn = 0;<span style= "White-space:pre" ></span>int Slen = strlen (str); <span style= "White-space:pre" ></span>unsigned short * buffer = 0;<span style= "White-space: Pre "></span>if (Slen = = 0) <span style=" White-space:pre "&GT;&LT;/SPAN&GT;RTN = (env)->newstringutf (str ); <span style= "White-space:pre" ></span>else<span style= "White-space:pre" ></span>{< Span style= "White-space:pre" ></span>int length = MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, NULL, 0); &L T;span style= "White-space:pre" ></span>buffer = (unsigned short *) malloc (length*2 + 1); <span style= " White-space:pre "></span>if (MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, Slen, (LPWSTR) buffer, length) >0) <span style= "White-space:pre" &GT;&LT;/SPAN&GT;RTN = (env)->newstring ((jchar*) buffer, length); <span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>if (buffer) <span style= "White-space:pre" ></span>free (buffer); <span style= "White-space: Pre "></span>return Rtn;}


JNI _10_jni Comprehensive Development

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.