跪求懂封装java可用的dll的大牛进来!封装别人的dll为java可用的dll,二次封装哦!
public class TestNativeDemo {
// 声明本地方法
public native String testJni(String arg);
public native int FPBegin();
static {
// 加载DLL文件
System.loadLibrary("TestNativeDemoCPP");
}
public static void main(String args[]) {
TestNativeDemo ob = new TestNativeDemo();
// 调用本地方法
String result = ob.testJni("Hello,Jni"); // call a native method
int res = ob.FPBegin();
System.out.println("TestNativeDemo...testJni=" + result + "//" + res);
}
}
//上面是我的生成dll的java代码,然后用javac编译,用javah生成头文件。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestNativeDemo */
#ifndef _Included_TestNativeDemo
#define _Included_TestNativeDemo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestNativeDemo
* Method: testJni
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_TestNativeDemo_testJni
(JNIEnv *, jobject, jstring);
/*
* Class: TestNativeDemo
* Method: FPBegin
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_TestNativeDemo_FPBegin
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
//上面是我的TestNativeDemo.h代码
// TestNativeDemoCpp.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <TCHAR.H>
using namespace std;
HINSTANCE otherDll = NULL;
JNIEXPORT jstring JNICALL Java_TestNativeDemo_testJni (JNIEnv *env,jobject obj,jstring pString){
const char *nativeString = env->GetStringUTFChars(pString, 0);
if (otherDll != NULL)
{
if(otherDll) FreeLibrary(otherDll);
}
otherDll = NULL;
otherDll = LoadLibrary(_T("otherDll.dll"));
HINSTANCE IDFP_hdll = otherDll;
if (NULL != IDFP_hdll)
{
if (NULL == GetProcAddress(IDFP_hdll, "FP_Begin")){
nativeString = "-1加载库失败";
} else {
nativeString = "0成功!";
}
}
else
{
nativeString = "-1加载库失败";
}
//从jstring中获取本地方法传递的字符串
printf("%s", nativeString);
//DON'T FORGET THIS LINE!!!
env->ReleaseStringUTFChars(pString, nativeString);
return pString;
}
JNIEXPORT jint JNICALL Java_TestNativeDemo_FPBegin (JNIEnv *evn, jobject obj) {
int i = 0;
if (otherDll != NULL)
{
if(otherDll) FreeLibrary(otherDll);
}
otherDll = NULL;
otherDll = LoadLibrary(_T("otherDll.dll"));
HINSTANCE IDFP_hdll = otherDll;
if (NULL != IDFP_hdll)
{
if (NULL == GetProcAddress(IDFP_hdll, "FP_Begin")){
i = 2;
} else {
i = 0;
}
}
else
{
i = 1;
}
return (jint)i;
}
//上面是我的TestNativeDemoCpp.cpp代码otherDll是别人提供给我的一个dll,我要生成一个dll调用他的dll
//然后执行java TestNativeDemo命令
0成功!Exception in thread "main" java.lang.UnsatisfiedLinkError: TestNativeDemo.FPBegin<>I
at TestNativeDemo.FPBegin<Native Method>
at TestNativeDemo.main<TestNativeDemo.java:22>
//上面代码可以看到我其他的部分暂时都没错了,但是我自己定义的FPBegin调用的话就告诉我没有这个方法,你们说怎么回事呢?而且它生成的.h里面原来在我的自己的函数前面是自带一个_1的,不知道为什么。
分不够可以加哦!