Java 调用dll 乱码,整理MINGW编译dll使用JNI被java调用的几种方式

第一种方式,在msys中直接编译各个源文件,不管是c或者c++文件,都能生产被JNI调用的dll库

JNI-MINGW-DLL

Posted August 10th, 2008 by fhackenberger

JNI is the Java Native Interface, you will need to download and install the Java SDK. Note the installation directory (ie/ c:/j2sdk1.4.1_02 ) for use later. If you are using MSYS add a line similiar to the following in /etc/fstab and then restart MSYS:

c:/j2sdk1.4.1_02      /java

In MSYS the JNI DLL can be generated using the following (NOTE: -Wl has an 'L' not a '1'):

gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at   -I/java/include -I/java/include/win32   -shared -o JavaImp.dll someJavaImp.c

In a standard command console it can be generated as follows (one continuous line):

gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at

-Ic:/j2sdk1.4.1_02/include -Ic:/j2sdk1.4.1_02/include/win32

-shared someJavaImp.c -o JavaImp.dll

Where JavaImp.dll should be named whatever you refer to the library as in your .java file. To use the above dll you would refer to it as follows in your java classes code:

System.loadLibrary( "JavaImp" );

If you encounter problems ensure your CLASS_PATH and PATH are set appropriately for your environment. Please refer to Java Native Interface for further details on using a JNI DLL in java code.

第二种方式, 通过JNative直接解析和调用本地dll文件中的函数

http://sourceforge.net/projects/jnative/

test.h

----------------------------------------------------------------------------------------------------------

#ifndef MINGW_DLL_H__

#define MINGW_DLL_H__

int add(int a,int b);

#endif

test.c

-----------------------------------------------------------------------------------------------------------

#include 

#include "test.h"

int add(int a,int b)

{

return a+b;

}

gcc -Wall -shared test.c -o test.dll

test.java

---------------------------------------------------------------------------------------------------------------------

import org.xvolks.jnative.JNative;

import org.xvolks.jnative.Type;

import org.xvolks.jnative.exceptions.NativeException;

public class Test{

public static  int testJNative(int a,int b) throws NativeException, IllegalAccessException{

JNative n = null;

try{

jn = new JNative("test.dll", "add");

jn.setRetVal(Type.INT);

jn.setParameter(0 , a ) ;

jn.setParameter(1, b);

jn.invoke();

System.out.println(  "return value:"  + jn.getRetVal());

return Integer.parseInt(jn.getRetVal());

}finally{

if (jn != null)

jn.dispose();

}

}

public static void main(String[] args) throws NativeException, IllegalAccessException{

testJNative(1, 4);

}

}

第三种方式 这种方式只是本人的猜想,尚未验证。

由于第一种方式直接在eclipse下进行编译和链接,会导致dll文件中export的函数名称带有类似

某些后缀(@8,@16等),可以采用dlltool进行dll的一些修饰工作,把不能被JNI直接调用的dll文件进行某种修改即去掉@等后缀或者是VC的下划线等前缀,编程能直接被调用的动态链接库。

可以使用dll export view工具,查看dll文件中函数的名字以及相当地址偏移等

http://www.nirsoft.net/utils/dll_export_viewer.html

可以使用dlltool工具对dll文件的进行修饰

http://www.sourceware.org/binutils/docs-2.16/binutils/dlltool.html

mingw中使用dlltool生产动态链接库

http://blog.csai.cn/user1/265/archives/2006/3091.html

————————————————

版权声明:本文为CSDN博主「zjuylok」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/zjuylok/article/details/4152864

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值