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中的类型如下:
获取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
一般是用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 |
对于函数原型为
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
相关文章推荐
- AC自动机算法详解
- 次小生成树 【总结】
- Day1_HTML_总结
- JNA调用C语言动态链接库学习实践总结
- Enumeration与Iterator的对比,ArrayList与Vector的比较
- find、locate、whereis、which和type
- leetcode Remove Duplicates from Sorted Array
- 信息化的十年
- hdu 5351 规律+大数
- Myeclipse2015去空格和等号的代码补全
- DEVOPS 技能列表
- 直接部署java项目到tomcat服务器
- 【暑期基础3】D HDU 2056 Rectangles (几何)
- I - In Touch-Gym 100492I-水题
- Hadoop和大数据:60款顶级开源工具
- Mac系统自带的邮箱发送到Outlook变成乱码——解决方案
- 菜鸟vimer成长记——第2.4章、cmd-line模式
- java 线程基础学习
- 笔试测试题2
- 2015年8月10日 C语言 宏和快排