JNI如何传递vc中带指针的参数和函数的问题,类似void win_startel(char *eventlog)和char *el_GetMessage(EVENTLOGRECORD *er,  char *event_name, char * event_sourcename, LPTSTR *el_sstring)的函数怎么在JAVA中调用,请高手帮me下

解决方案 »

  1.   

    step1:创建HelloWorld.java
    内容如下:
    public class HelloWorld{
    public native void win_startel(char[] eventlog);
    static{
    System.loadLibrary("c_lib"); 
    }
    public static void main(String[] args){
    System.out.println("以下是通过调用dll来实现:");
    char[] hi=new char[]{'h','e','l','l','o'}; 
    new HelloWorld().win_startel(hi);
    }
    }
    step2:创建HelloWorld.h
    javah HelloWorld.java
    内容如下:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class HelloWorld */#ifndef _Included_HelloWorld
    #define _Included_HelloWorld
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     HelloWorld
     * Method:    win_startel
     * Signature: ([C)V
     */
    JNIEXPORT void JNICALL Java_HelloWorld_win_1startel
      (JNIEnv *, jobject, jcharArray);#ifdef __cplusplus
    }
    #endif
    #endif
    step3:创建dll
    VC++代码如下:#include "jni.h"
    #include "HelloWorld.h"
    #include "stdio.h"
    JNIEXPORT void JNICALL Java_HelloWorld_win_1startel (JNIEnv *env, jobject object, jcharArray array)
    {
    int i;
    jsize len=env->GetArrayLength(array); //获取数组大小
    jchar *body=env->GetCharArrayElements(array, 0);//body指向数组基地址
    for(i=0;i<len;i++)
    putchar(body[i]); //输出结果}step4:运行,java HelloWorld另外,如果想尽一步了解jni知识,请下载:http://download.csdn.net/down/601136/xjhh120