JNI传递struct参数

时间:2015-09-17 02:42:45

标签: java c struct java-native-interface

我想知道如何编写My C代码以将Java对象变量映射到Struct 我的java类调用用C编写的本机方法,下面是我的代码。

JavaCode:

public class JNIWrapper {

static{
    System.loadLibrary("JNIDemo");
}

public native int packet_mining_start(pax_store_mining_query_param_s   packet_mining_params);
}

我的班级pax_store_mining_query_param_s:

public class pax_store_mining_query_param_s {
String query_id;                        // A unique number, if applicable.
                        // A unique number, if applicable.
String flow_id;                     
String time_start;
String time_end;
}

标头文件: 1)sam_api:

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
   /* Header for class demo_JNIWrapper */

   #ifndef _Included_demo_JNIWrapper
   #define _Included_demo_JNIWrapper
   #include "pax_store_mining.h"//Contains my C structures 
   #ifdef __cplusplus
   extern "C" {
   #endif
   JNIEXPORT jint JNICALL Java_demo_JNIWrapper_packet_1mining_1start
     (JNIEnv *, jobject, jobject);

   }

3)包含结构的第二个头文件:

     #ifndef PAX_STORE_QUERY_H
#define PAX_STORE_QUERY_H

#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct pax_store_mining_query_param_s {
    uint32_t query_id;                      // A unique number, if applicable.
    uint64_t flow_id;                       
    uint64_t time_start;
    uint64_t time_end;
};   

4)下面是我的C代码:

#include<stdio.h>
#include <jni.h>
#include "demo_JNIWrapper.h"

JNIEXPORT jint JNICALL Java_demo_JNIWrapper_packet_1mining_1start
  (JNIEnv * env, jobject jobj, jobject packet_mining_params){

return 0;

}  

我想从我的java对象映射这个packet_mining_params结构,该对象具有与此相同的名称和参数。这会自动映射吗?

0 个答案:

没有答案