JNI 调用,求助。
需要对 OpenSSL 的一批函数进行 JNI 调用封装。现在是这个函数中传入的参数有结构体,对应的 java 调用时如何定义?
比如:我的例子如下。
C 程序为:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sj {
float sou1;
float sou2;
float des;
};
int sub(struct sj *a);
int add(struct sj *b);
int sub(struct sj *a){
a->des = a->sou1 - a->sou2;
return 0;
}
int add(struct sj *b){
b->des = b->sou1 + b->sou2 ;
return 0;
}
对于这样的 C 程序,JNI 调用应该如何做?
附:
我的 java 代码及生成的 .h 的文件内容如下:
subadd.java/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author work
*/
public class CertSys {
static {
System.loadLibrary("libCertSys.so");
}
public class Sj {
private float a;
private float b;
private float c;
public float getA(){
return a;
}
public void setA(float a){
this.a = a;
}
public float getB(){
return b;
}
public void setB(float b){
this.b = b;
}
public float getC(){
return c;
}
public void setC(float c){
this.c = c;
}
}
public native static int sub(Sj i);
public native static int add(Sj j);
}.h
文件内容如下:/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class CertSys */
#ifndef _Included_CertSys
#define _Included_CertSys
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: CertSys
* Method: sub
* Signature: (LCertSys/Sj;)I
*/
JNIEXPORT jint JNICALL Java_CertSys_sub
(JNIEnv *, jclass, jobject);
/*
* Class: CertSys
* Method: add
* Signature: (LCertSys/Sj;)I
*/
JNIEXPORT jint JNICALL Java_CertSys_add
(JNIEnv *, jclass, jobject);
#ifdef __cplusplus
}
#endif
#endif 你都是大神了,这得什么级别才可以帮你啊 回复 2# 346196247
那个是以前混清茶混来的。
:em17: CertSysa = new CertSys();
a.setA();
.....
CertSys.sub(a);
.. 不知对否。。
@nan_jia晕,我就是不会啊。 用 jNative 比较方便。。 @nan_jia具体如何做,能写一个例子看看么?
就我上面给出的情况,能给一个例子么? 你发出来的JAVA 文件 就是正确的吧。
CertSysa = new CertSys();
a.setA();
.....
CertSys.sub(a);
这样用就没问题。 @nan_jia你是说我的这个写法是对的? 我看你生成的是没问题的
页:
[1]
2