100分求解!!java调用第三方dll中的函数
如题,现在有一个第三方的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)); }}
/* 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_ */
/* 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;}
# 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
真的不知道是什么原因~~请高手指教!万分感谢!
[解决办法]
和jdk的版本有关系?
[解决办法]
应该是版本问题哦
你让别人机子上试试呢~
[解决办法]
1.两个dll放一起
2. 你先用C测试一下你调用dll的代码, 确认能load dll, 再用dependcy walker查看你调用的dll 函数导出名字, 人家写的dll导出的时候可能使用不同的惯例, 可能是GetProcessAddress函数名的问题。
[解决办法]
http://blog.csdn.net/rohsuton/archive/2008/12/29/3637272.aspx
看看