如题,现在有一个第三方的dll文件“Mwic_32.dll”,由于是第三方的,必须要用JNI实现,我的具体步骤如下:先在java中定义一个类:
public class AAA {
static {
System.load("C:/AUTO_INIT.dll");
}
public native static int auto_init(int a,int b);
public static void main(String args[]){
AAA new_auto_init = new AAA();
System.out.println("Result="+new_auto_init.auto_init(0, 38400));
}
}
之后用javah AAA生成头文件AAA.h在DEV C++中新建一个dll工程,名字为“AUTO_INIT”
之后复制AAA.h中的内容到AUTO_INIT.h的最前边
最后实现AUTO_INIT.cpp中的方法Java_AAA_auto_1init(调用Mwic_32.dll中的auto_init函数)
并生成AUTO_INIT.dll
其中,AUTO_INIT.h代码如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class AAA */#ifndef _Included_AAA
#define _Included_AAA
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: AAA
* Method: auto_init
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_AAA_auto_1init
(JNIEnv *, jclass, jint, jint);#ifdef __cplusplus
}
#endif
#endif
///////////////////////////////////////////////////////
#ifndef _DLL_H_
#define _DLL_H_#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
class DLLIMPORT DllClass
{
public:
DllClass();
virtual ~DllClass(void); private:};
#endif /* _DLL_H_ */
AUTO_INIT.cpp代码:
/* Replace "dll.h" with the name of your header */
#include<windows.h>
#include<stdio.h>#include "AUTO_INIT.h"
#include <windows.h>JNIEXPORT jint JNICALL Java_AAA_auto_1init
(JNIEnv * nev, jclass obj, jint a, jint b){
//const int * aa=a;
//const int * bb=evn->GetInt(b);
//void * objptr = CreateAUTO_INIT();
HINSTANCE hdll = NULL;
hdll = LoadLibrary ( "Mwic_32.dll" );
typedef BOOL ( _stdcall *lpFileEncrypt )( INT, INT);
lpFileEncrypt FileEncrypt1;
FileEncrypt1 = ( lpFileEncrypt )::GetProcAddress( hdll, "auto_init" );//调用dll中的函数
int result = FileEncrypt1(0,38400);
FreeLibrary( hdll );
//printf("restult=%d",result);
//int result=auto_init(a,b);
//env->ReleaseInt(a,aa);
//env->ReleaseInt(b,bb);
return result;
}DllClass::DllClass()
{}
DllClass::~DllClass ()
{}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break; case DLL_PROCESS_DETACH:
break; case DLL_THREAD_ATTACH:
break; case DLL_THREAD_DETACH:
break;
} /* Returns TRUE on success, FALSE on failure */
return TRUE;
}
最后我把生成出来的AUTO_INIT.dll文件放在C盘下,但是用java运行是确提示“An unexpected error has been detected by Java Runtime Environment:”具体的出错信息如下:
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=3644, tid=3696
#
# Java VM: Java HotSpot(TM) Client VM (11.0-b16 mixed mode, sharing windows-x86)
# Problematic frame:
# C 0x00000000
#
# An error report file with more information is saved as:
# C:\app1\AAA\hs_err_pid3644.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Java Result: 1真的不知道是什么原因~~请高手指教!万分感谢!
public class AAA {
static {
System.load("C:/AUTO_INIT.dll");
}
public native static int auto_init(int a,int b);
public static void main(String args[]){
AAA new_auto_init = new AAA();
System.out.println("Result="+new_auto_init.auto_init(0, 38400));
}
}
之后用javah AAA生成头文件AAA.h在DEV C++中新建一个dll工程,名字为“AUTO_INIT”
之后复制AAA.h中的内容到AUTO_INIT.h的最前边
最后实现AUTO_INIT.cpp中的方法Java_AAA_auto_1init(调用Mwic_32.dll中的auto_init函数)
并生成AUTO_INIT.dll
其中,AUTO_INIT.h代码如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class AAA */#ifndef _Included_AAA
#define _Included_AAA
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: AAA
* Method: auto_init
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_AAA_auto_1init
(JNIEnv *, jclass, jint, jint);#ifdef __cplusplus
}
#endif
#endif
///////////////////////////////////////////////////////
#ifndef _DLL_H_
#define _DLL_H_#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
class DLLIMPORT DllClass
{
public:
DllClass();
virtual ~DllClass(void); private:};
#endif /* _DLL_H_ */
AUTO_INIT.cpp代码:
/* Replace "dll.h" with the name of your header */
#include<windows.h>
#include<stdio.h>#include "AUTO_INIT.h"
#include <windows.h>JNIEXPORT jint JNICALL Java_AAA_auto_1init
(JNIEnv * nev, jclass obj, jint a, jint b){
//const int * aa=a;
//const int * bb=evn->GetInt(b);
//void * objptr = CreateAUTO_INIT();
HINSTANCE hdll = NULL;
hdll = LoadLibrary ( "Mwic_32.dll" );
typedef BOOL ( _stdcall *lpFileEncrypt )( INT, INT);
lpFileEncrypt FileEncrypt1;
FileEncrypt1 = ( lpFileEncrypt )::GetProcAddress( hdll, "auto_init" );//调用dll中的函数
int result = FileEncrypt1(0,38400);
FreeLibrary( hdll );
//printf("restult=%d",result);
//int result=auto_init(a,b);
//env->ReleaseInt(a,aa);
//env->ReleaseInt(b,bb);
return result;
}DllClass::DllClass()
{}
DllClass::~DllClass ()
{}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break; case DLL_PROCESS_DETACH:
break; case DLL_THREAD_ATTACH:
break; case DLL_THREAD_DETACH:
break;
} /* Returns TRUE on success, FALSE on failure */
return TRUE;
}
最后我把生成出来的AUTO_INIT.dll文件放在C盘下,但是用java运行是确提示“An unexpected error has been detected by Java Runtime Environment:”具体的出错信息如下:
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=3644, tid=3696
#
# Java VM: Java HotSpot(TM) Client VM (11.0-b16 mixed mode, sharing windows-x86)
# Problematic frame:
# C 0x00000000
#
# An error report file with more information is saved as:
# C:\app1\AAA\hs_err_pid3644.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Java Result: 1真的不知道是什么原因~~请高手指教!万分感谢!
解决方案 »
- 求助!代码运行一半出现java.lang.OutOfMemoryError: Java heap space
- hibernate多表查询求相应的HQL语句
- 求struts连接sqlserver2000数据库的struts-config.xml(jtds-1.2.jar驱动)
- 数据库被锁,急切。怎么解开
- jboss应用服务器哪些版本收费 哪些版本免费
- 大狭们懂ibatis的近来看看啊,我都郁闷了好几天了,今天在搞不定就万了,帮帮我啊!!在线急等在线急等!!
- log4j配置的問題
- Java学完了,J2EE该怎么入门呢?
- 在J2EE環境下如何部署我的應用程序﹖
- CXF客户端调用服务端时候,传递XML字符串,SOAP报文被转义
- apache的rewrite该怎么写
- 在线求一个解决办法!
2. 你先用C测试一下你调用dll的代码, 确认能load dll, 再用dependcy walker查看你调用的dll 函数导出名字, 人家写的dll导出的时候可能使用不同的惯例, 可能是GetProcessAddress函数名的问题。
看看