JNI技术初步(三)

全部的C/C++方法实现代码如下: /// * * 一缕阳光(sundy)版权所有,保留所有权利。 / ///* * * TODO Jni 中一个从Java到C/C++参数传递测试类 * * @author 刘正伟(sundy) * @see http://www.cnweblog.com/sundy * @see sundy26@126.com * @version 1.0 * @since 2005-4-30 * * 修改记录: * * 日期 修改人 描述 * ———————————————————————————————- * * * / // JniManage.cpp : 定义 DLL 应用程序的入口点。 // package com.sundy.jnidemo; #i nclude “stdafx.h”#i nclude <stdio.h> #i nclude <math.h> #i nclude “jni.h” #i nclude “jni_md.h”

#i nclude “./head/Base.h” #i nclude “head/wmi.h” #i nclude “head/com_sundy_jnidemo_ChangeMethodFromJni.h” //通过javah -jni javactransfer 生成 #i nclude <stdio.h> #i nclude “stdlib.h” #i nclude “string.h”

pragma comment (lib,“BaseInfo.lib”)

pragma comment (lib,“jvm.lib”)

//硬盘信息 struct { char name[256]; int serial; }DiskInfo; ///BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { LPTSTR strName = new CHAR[256] ; (GetHostName)(strName); printf(“%s\n”,strName); delete [] strName;

return TRUE; }/ //将jstring类型转换成windows类型 char jstringToWindows( JNIEnv env, jstring jstr ); //将windows类型转换成jstring类型 jstring WindowsTojstring( JNIEnv env, char* str );

//主函数 BOOL WINAPI DllMain(HANDLE hHandle, DWORD dwReason, LPVOID lpReserved) { return TRUE; } //输入常用的数值类型 Boolean,Byte,Char,Short,Int,Float,Double JNIEXPORT void JNICALL Java_com_sundy_jnidemo_ChangeMethodFromJni_displayParms (JNIEnv env, jobject obj, jstring s, jint i, jboolean b) { const char szStr = (env)->GetStringUTFChars(s, 0 ); printf( “String = [%s]\n”, szStr ); printf( “int = %d\n”, i ); printf( “boolean = %s\n”, (b==JNI_TRUE ? “true” : “false”) ); (env)->ReleaseStringUTFChars(s, szStr ); }

//调用一个静态方法,只有一个简单类型输出 JNIEXPORT jint JNICALL Java_com_sundy_jnidemo_ChangeMethodFromJni_add (JNIEnv *env, jobject, jint a, jint b) { int rtn = (int)(a + b); return (jint)rtn; }

//////输入一个数组,这里输入的是一个Boolean类型的数组 JNIEXPORT void JNICALL Java_com_sundy_jnidemo_ChangeMethodFromJni_setArray (JNIEnv env, jobject, jbooleanArray ba) { jboolean pba = (env)->GetBooleanArrayElements(ba, 0 ); jsize len = (env)->GetArrayLength(ba); int i=0; // change even array elements for( i=0; i < len; i+=2 ) { pba[i] = JNI_FALSE; printf( “boolean = %s\n”, (pba[i]==JNI_TRUE ? “true” : “false”) ); } (env)->ReleaseBooleanArrayElements(ba, pba, 0 ); }

//////返回一个字符串数组 JNIEXPORT jobjectArray JNICALL Java_com_sundy_jnidemo_ChangeMethodFromJni_getStringArray (JNIEnv env, jobject) { jstring str; jobjectArray args = 0; jsize len = 5; char sa[] = { “Hello,”, “world!”, “JNI”, “is”, “fun” }; int i=0; args = (env)->NewObjectArray(len,(env)->FindClass(“java/lang/String”),0); for( i=0; i < len; i++ ) { str = (env)->NewStringUTF(sa[i] ); (env)->SetObjectArrayElement(args, i, str); } return args; }

//返回一个结构,这里返回一个硬盘信息的简单结构类型 JNIEXPORT jobject JNICALL Java_com_sundy_jnidemo_ChangeMethodFromJni_getStruct (JNIEnv env, jobject obj) { /**// 下面为获取到Java中对应的实例类中的变量*/

//获取Java中的实例类 jclass objectClass = (env)->FindClass(“com/sundy/jnidemo/DiskInfo”);

//获取类中每一个变量的定义 //名字 jfieldID str = (env)->GetFieldID(objectClass,“name”,“Ljava/lang/String;”); //序列号 jfieldID ival = (env)->GetFieldID(objectClass,“serial”,“I”); //给每一个实例的变量付值 (env)->SetObjectField(obj,str,(env)->NewStringUTF(“my name is D:”)); (env)->SetShortField(obj,ival,10);

return obj; }