Java代码
调用类
package JniCallDLL;
import db.RFParaTypeInfo;
import db.XYZPosition;
public class JniDLL {
public native int add(int a,int b);//在我写的实际需要调用的动态库中有加减乘除几个测试方法,我这里只测试了其中的add方法
public native int sum(int a,int b);
public native RFParaTypeInfo processRFPara(RFParaTypeInfo input);
public native double processXYZPosition(XYZPosition input);
static {
System.loadLibrary("TestJniDLL");//这是我即将要重新实现的动态库名字
}
}
入参类
/**
* @author 28081
*/
public class XYZPosition {
private double X;
private double Y;
public XYZPosition(double x, double y) {
this.X = x;
this.Y = y;
}
public double getX() {
return X;
}
public double getY() {
return Y;
}
}
Java调用代码
package JniCallDLL;
import db.RFParaTypeInfo;
import db.XYZPosition;
public class RunDllCall {
public static void main(String[] args)
{
JniDLL jniDll = new JniDLL();
System.out.println(jniDll.add(2,3));
System.out.println(jniDll.sum(2,3));
XYZPosition xyz=new XYZPosition(5,6);
System.out.println(jniDll.processXYZPosition(xyz));
String str="";
}
}
C++代码
#include "TestJniDLL.h"
#include <iostream>
#include <stdio.h>
#include "JniCallDLL_JniDLL.h"
#include <Windows.h> //HMODULE
#include <tchar.h> //_T
typedef int(*Dllfun)(int, int);//定义一个名为Dllfun的返回值和两个参数都为int的函数指针
JNIEXPORT jint JNICALL Java_JniCallDLL_JniDLL_add
(JNIEnv*, jobject, jint a, jint b) {
//HMODULE hinst=LoadLibrary(_T("MyMFCLibrary.dll"));//加载第三方动态库,不带路劲的话,默认就在当前动态库的相同路径下,带路径的话,路径的'\'需要写成'\\'
//if (hinst==NULL)
//{
// return 0;//调用第三方动态库失败
//}
//Dllfun dllfun = (Dllfun)GetProcAddress(hinst, "myAdd"); //获取第三方动态库中的add()方法
//int result = dllfun(a, b);//调用add()方法
return 20;
}
JNIEXPORT jint JNICALL Java_JniCallDLL_JniDLL_sum
(JNIEnv*, jobject, jint a, jint b) {
//HMODULE hinst=LoadLibrary(_T("MyMFCLibrary.dll"));//加载第三方动态库,不带路劲的话,默认就在当前动态库的相同路径下,带路径的话,路径的'\'需要写成'\\'
//if (hinst==NULL)
//{
// return 0;//调用第三方动态库失败
//}
//Dllfun dllfun = (Dllfun)GetProcAddress(hinst, "myAdd"); //获取第三方动态库中的add()方法
//int result = dllfun(a, b);//调用add()方法
return 50;
}
JNIEXPORT jdouble JNICALL Java_JniCallDLL_JniDLL_processXYZPosition(JNIEnv* env, jobject obj, jobject position) {
// 1. 获取XYZPosition的类引用
jclass cls = env->GetObjectClass(position);
if (cls == nullptr) {
std::cerr << "Failed to get class reference." << std::endl;
return 1;
}
// 2. 获取getX和getY方法的ID
jmethodID midGetX = env->GetMethodID(cls, "getX", "()D");
jmethodID midGetY = env->GetMethodID(cls, "getY", "()D");
if (midGetX == nullptr || midGetY == nullptr) {
std::cerr << "Failed to get method IDs." << std::endl;
return 1;
}
// 3. 调用方法获取值
jdouble x = env->CallDoubleMethod(position, midGetX);
jdouble y = env->CallDoubleMethod(position, midGetY);
// 4. 输出结果
std::cout << "X: " << x << ", Y: " << y << std::endl;
const char* nativeString = "Hello from native code";
jstring strRes = env->NewStringUTF(nativeString);
jdouble re = x * y;
return re;
}
TestJniDLL::TestJniDLL()
{
}
TestJniDLL::~TestJniDLL()
{
}
C++对外调用方法
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class JniCallDLL_JniDLL */
#ifndef _Included_JniCallDLL_JniDLL
#define _Included_JniCallDLL_JniDLL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JniCallDLL_JniDLL
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_JniCallDLL_JniDLL_add
(JNIEnv *, jobject, jint, jint);
JNIEXPORT jint JNICALL Java_JniCallDLL_JniDLL_sum
(JNIEnv*, jobject, jint, jint);
JNIEXPORT jdouble JNICALL Java_JniCallDLL_JniDLL_processXYZPosition
(JNIEnv* env, jobject obj, jobject position);
#ifdef __cplusplus
}
#endif
#endif
输出结果
(比较忙、后续详细讲下)