67,512
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C"
int _declspec(dllexport) add(int a ,int b)
{
return a+b;
}
public class DemoJni{
public native int getNumber(int a,int b);
public static void main(String[] args) {
}
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class DemoJni */
#ifndef _Included_DemoJni
#define _Included_DemoJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: DemoJni
* Method: getNumber
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_DemoJni_getNumber
(JNIEnv *, jobject, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
// B.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "jni.h"
#include "DemoJni.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
JNIEXPORT jint JNICALL Java_DemoJni_getNumber
(JNIEnv * env, jobject o, jint x, jint y)
{
typedef int (*ADD)(int ,int);//函数指针类型
HINSTANCE Hint = ::LoadLibrary("A.dll");//加载我们刚才生成的dll
ADD add = (ADD)GetProcAddress(Hint,"add");//取得dll导出的add方法
return add(x,y);
FreeLibrary(Hint);
}
public class DemoJni{
public native int getNumber(int a,int b);
public static void main(String[] args) {
System.loadLibrary("B");
DemoJni p=new DemoJni();
System.out.println(p.getNumber(1, 100));
}
}
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class test {
static JNative myjnative = null;
public int getnumber(int a, int b) throws NativeException,
IllegalAccessException {
try {
if (myjnative == null) {
myjnative = new JNative("A.dll", "add");
myjnative.setRetVal(Type.INT);
}
int i = 0;
myjnative.setParameter(i++, a);
myjnative.setParameter(i++, b);
myjnative.invoke();
return myjnative.getRetValAsInt();
} finally {
if (myjnative != null) {
myjnative.dispose();
}
}
}
public static void main(String[] args) throws NativeException,
IllegalAccessException {
test uc = new test();
int result = uc.getnumber(1,100);
System.err.println("result:" + result);
}
}
在你调用的Java类里添加你需要调用的方法,
如:int Init(const char * Data1,char * retMsg,char* Data2)
类里:public native int init(const String Data1,String retMsg,String Data2);