当前位置: 代码迷 >> J2EE >> 100分求解!java调用第三方dll中的函数解决办法
  详细解决方案

100分求解!java调用第三方dll中的函数解决办法

热度:96   发布时间:2016-04-22 01:04:02.0
100分求解!!java调用第三方dll中的函数
如题,现在有一个第三方的dll文件“Mwic_32.dll”,由于是第三方的,必须要用JNI实现,我的具体步骤如下:

先在java中定义一个类:
Java code
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代码如下:
C/C++ code
/* DO NOT EDIT THIS FILE - it is machine generated */#include "jni.h"/* Header for class AAA */#ifndef _Included_AAA#define _Included_AAA#ifdef __cplusplusextern "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代码:
C/C++ code
/* 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:
  相关解决方案