1、VC6(CPP)的DLL代码:
#include<stdio.h> #include "jniZ_JNIjw06.h" // 全局变量 jfieldID g_propInt_id = 0; jfieldID g_propStr_id = 0; JNIEXPORT void JNICALL Java_jniZ_JNIjw06_InitNativeIDs (JNIEnv * env, jclass clazz) { printf("Java_jniZ_JNIjw06_InitNativeIDs - 1 "); g_propInt_id = env->GetFieldID(clazz, "propInt", "I"); g_propStr_id = env->GetFieldID(clazz, "propStr", "Ljava/lang/String;"); printf("Java_jniZ_JNIjw06_InitNativeIDs - 2 "); } JNIEXPORT void JNICALL Java_jniZ_JNIjw06_OtherNative (JNIEnv * env, jobject obj) { printf("Java_jniZ_JNIjw06_OtherNative - 1 "); // get field with g_propInt_id / g_propStr_id ... printf("Java_jniZ_JNIjw06_OtherNative - 2 "); }
2、Java代码:
package jniZ; public class JNIjw06 { static { InitNativeIDs(); } static native void InitNativeIDs(); int propInt = 0; String propStr = ""; public native void OtherNative(); public static void main(String[] args) { Object ll; } }
3、Java控制台输出:
4、
5、