日期:2014-05-20 浏览次数:20639 次
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")){