您的位置:首页 > 其它

jna 解决乱码 GBK

2015-08-10 18:37 357 查看
如何使用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
plaincopy

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
plaincopy

Double pSimilarity = null;

Memory memory_sililar = new Memory(8);

String articleId_section = fszSimilarArticle(iIndex,memory_sililar);

pSimilarity=memory_sililar.getDouble(0);

这样可以获取返回数据.

创建对应的接口:

[java] view
plaincopy

package netinet.thesis.release;

importcom.sun.jna.Library;

importcom.sun.jna.Memory;

importcom.sun.jna.Native;

public MOD extends Library {

/*mean语义分析*/

public final static String CHECK_MEAN = "0";

/*order词序分析*/

public final static String CHECK_ORDER = "1";

public int fnVersion(Memory CheckArticle);

public String fszSimilarArticle(intiIndex,Memory pSimilarity);

}

创建实例的方式:

[java] view
plaincopy

public static MOD newThesisMod(){

System.setProperty("jna.encoding","GBK"); //如果不适用GBK进行编码,则改为对应的编码方式.

return (MOD)Native.loadLibrary("MOD",MOD.class);

}

jar包下载

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

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

如何创建一个dll: /article/8815249.html

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

参考: /article/3943044.html

转载于:http://blog.csdn.net/w329636271/article/details/46665239
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: