jna 解决乱码 GBK

12 篇文章 0 订阅

如何使用java调用dll?

一般是用jni直接来调用dll的接口.因为中文乱码,GBK,utf-8等一系列原因,我选择了用jna这个第三方接口来.

Dll放的位置:可以放在项目的根目录下,可以放在jdk目录中的bin目录下.这两个地方jna都是可以找到的.

因为调用的过程中设计了中文,dll中的中文是用GBK进行编码的,所以java传给dll也得是GBK,不然就会乱码.因为c,c++中的char*对应java中的string,不过jna默认的编码方式是utf-8,所以要手动的将编码方式改为GBK.

[java]  view plain copy
  1. System.setProperty("jna.encoding","GBK");        
             

     c,c++对应java中的类型如下:

C Type

Native Representation

Java Type

char

8-bit integer

byte

wchar_t

platform-dependent

char

short

16-bit integer

short

int

32-bit integer

int

int

boolean flag

boolean

enum

enumeration type

int (usually)

long long, __int64

64-bit integer

long

float

32-bit floating point

float

double

64-bit floating point

double

pointer (e.g. void*)

platform-dependent (32- or 64-bit pointer to memory)

Buffer
Pointer

pointer (e.g. void*),
array

32- or 64-bit pointer to memory (argument/return)
contiguous memory (struct member)

<P>[] (array of primitive type)

In addition to the above types, which are supported at the native layer, the JNA Java library automatically handles the following types. All but NativeMapped and NativeLong are converted to Pointer before being passed to the native layer.

long

platform-dependent (32- or 64-bit integer)

NativeLong

const char*

NUL-terminated array (native encoding or jna.encoding)

String

const wchar_t*

NUL-terminated array (unicode)

WString

char**

NULL-terminated array of C strings

String[]

wchar_t**

NULL-terminated array of wide C strings

WString[]

void**

NULL-terminated array of pointers

Pointer[]

struct*
struct

pointer to struct (argument or return) (or explicitly)
struct by value (member of struct) (or explicitly)

Structure

union

same as Structure

Union

struct[]

array of structs, contiguous in memory

Structure[]

void (*FP)()

function pointer (Java or native)

Callback

pointer (<T> *)

same as Pointer

PointerType

other

integer type

IntegerType

other

custom mapping, depends on definition

NativeMapped

 

 

获取double*指针返回的数据,char*的返回数据.

对于函数原型为

const char*fszSimilarArticle( int iIndex, double *pSimilarity );

其中pSimilarity是返回的数据,c,c++语言经常这样子进行返回数据.

对应的java原型:

StringfszSimilarArticle(int iIndex,Memory pSimilarity);

使用 jna-4.0.0.jar

调用方式:

         

[java]  view plain copy
  1. Double pSimilarity = null;  
  2. Memory memory_sililar = new Memory(8);  
  3. String articleId_section = fszSimilarArticle(iIndex,memory_sililar);  
  4. pSimilarity=memory_sililar.getDouble(0);  

    这样可以获取返回数据.

创建对应的接口:

[java]  view plain copy
  1. package netinet.thesis.release;  
  2. importcom.sun.jna.Library;  
  3. importcom.sun.jna.Memory;  
  4. importcom.sun.jna.Native;  
  5. public MOD extends Library {  
  6.    /*mean语义分析*/  
  7.    public final static String  CHECK_MEAN = "0";  
  8.    /*order词序分析*/  
  9.    public final static String  CHECK_ORDER = "1";        
  10.    public int fnVersion(Memory CheckArticle);  
  11.    public String fszSimilarArticle(intiIndex,Memory pSimilarity);  
  12. }  

创建实例的方式:

[java]  view plain copy
  1. public static MOD newThesisMod(){  
  2.            System.setProperty("jna.encoding","GBK");         //如果不适用GBK进行编码,则改为对应的编码方式.  
  3.            return (MOD)Native.loadLibrary("MOD",MOD.class);                    
  4. }  

jar包下载

http://download.csdn.net/detail/w329636271/8846031

 

github官网:  http://twall.github.io/jna/3.4.0/javadoc/overview-summary.html

如何创建一个dll: http://blog.csdn.net/gcangle/article/details/8504629

Api: https://jna.java.net/javadoc/com/sun/jna/Memory.html

参考: http://forestqqqq.iteye.com/blog/1941631


转载于:http://blog.csdn.net/w329636271/article/details/46665239

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值