JNI Complete Manual (2)
From Yippit
My project is more complicated and needs to call dynamic link libraries so that the parameters are required to process the parameters when the JNI transmits parameters to the C program. Can be identified by the C program. The general procedure is as follows: public class sendsms {static {system.out.println ("java.library.path")); System.LoadLibrary ("SMS");} public native static int smsinit (); public native Static int smssend (byte [] mobileno, byte [] smcontent;} At this point here, it is necessary to include the path to the class library, otherwise it will throw an exception when the program is run: java.lang.unsatisfiedLinkerRor: no sms in java.library.pathat java.lang.ClassLoader.loadLibrary (ClassLoader.java:1491) at java.lang.Runtime.loadLibrary0 (Runtime.java:788) at java.lang.System.loadLibrary (System.java:834 AT com.mobilesoft.sms.MobilesoftInfo.Smsms.
/ * DO NOT EDIT THIS FILE - it is machine generated * / # include
/ ** SMS API * Author: yippit * Date: 2004.6.8 * / # ifndef MCS_SMS_H # define MCS_SMS_H # define DLLEXPORT __declspec (dllexport) / * sms storage * / # define SMS_SIM 0 # define SMS_MT 1 / * sms states * / #define SMS_UNREAD 0 # define SMS_READ 1 / * sms type * / # define SMS_NOPARSE -1 # define SMS_NORMAL 0 # define SMS_FLASH 1 # define SMS_MMSNOTI 2typedef struct tagSmsEntry {int index; / * index, start from 1 * / int status; / * read, unread * / int type; / * - 1-can't Parser 0-Normal, 1-Flash, 2-mms * / int storage; / * SMS_SIM, SMS_MT * / CHAR DATE [24]; char Number [ 32]; CHAR TEXT [144];} SMSENTRY; DLLEXPORT INT SMSINIT (VOID); DLLEXPORT INT SMSSEND (Char * Phonenum, Char * Content); DLlexPort Int Smssetsca (Char * SCA); DLLEXPORT INT SMSGETSCA (CHAR * SCA); DLLEXPORT int SmsSetInd (int ind); DLLEXPORT int SmsGetInd (void); DLLEXPORT int SmsGetInfo (int storage, int * max, int * used); DLLEXPORT int SmsSaveFlash (int flag); DLLEXPORT int SmsRead (SmsEntry * entry, int storage, INDEX); DLLEXPORT INT SMSDELETE (Int Storage, INDEX); DLLEXPORT INT SMSMODIFYSTATUS (INT Storage, int index; / * unread -> read * / # Endif After having the two header files, you can write a C program. That is, two methods for achieving JNI calls. In the online information, since the method of calling is relatively simple, (mostly a print string, etc.), so the most troublesome part of the JNI is avoided, and the most critical part, the passage of the parameters. Since Java and C coding is different, the passable parameters are to be processed, otherwise the C program will warn the parameters during the compilation process, for example; Warning C4024: 'smssend': Different Types for Formal and Actual Parameter 2, etc.
Sms.c procedure is as follows: #include "sms.h" #include "com_mobilesoft_sms_mobilesoftinfo_SendSMS.h" JNIEXPORT jint JNICALL Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsInit (JNIEnv * env, jclass jobject) {return SmsInit ();} JNIEXPORT jint JNICALL Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsSend (JNIEnv * env, jclass jobject, jbyteArray mobileno, jbyteArray smscontent) {char * pSmscontent; // jsize theArrayLengthJ = (* env) -> GetArrayLength (env, mobileno); jbyte * arrayBody = (* env) -> GetByteArrayElements (env, mobileno, 0); Char * Pmobileno = (char *) ArrayBody; Printf ("[% s] / n", pmobileno; // jsize size = (* ENV) -> GetArrayLength (ENV, SMSCONTENT); arraybody = (* ENV) -> GetByteaRrayElements (ENV, SMSCONTENT, 0); psmscontent = (char *) ArrayBody; Printf ("<% s> / n", psmscontent; returno, psmscontent);} For C or C , it is meeting It is slightly different, which can be appropriately modified by the reader. It should be noted here that the methods already included in these JNIs such as GetArrayLength, GetByteaRayelements, which are specifically provided to the transition parameter type. There are a lot of specific methods that will be specifically introduced in the next one. After completing the above file, you can compile SMS.C, generate .dll files (recommended to compile in Release, so that the volume of the dynamic link library will be smaller!) After completing the .dll file, you can in Java The method in the C procedure is used.