VS2013,C++做的DLL:
char* pt;
char * test1(){
pt = (char*)malloc(200000000);
char buf[100] = "hello你world";
memcpy(pt, buf, 100);
return pt;
}
char * test2(){
if (pt != NULL)
{
free(pt);
cout << "free(pt)" << endl;
}
else
cout << "no free" << endl;
return "helloworld你好,delete pt:";
}
JNA代码:
System.setProperty("jna.encoding","gb18030"); //当DLL中返回中文时,gbk/gb2312/gb18030 t1可以正确显示中文,但如果setProperty设置为utf8则乱码
System.out.println(System.getProperty("jna.encoding"));
String t1 = DLLLibrary.DLL_LIBRARY.test1();
System.out.println(t1);
Pointer t2 = DLLLibrary.DLL_LIBRARY.test2();
byte[] buf = new byte[25];
t2.read(0,buf,0,buf.length);
String string = new String(buf, Charset.forName("gbk"));//在vs2013所做的dll中返回中文字符串(工程设置编码为utf8),此处只能设置为gbk/gb2312/gb18030才能成功,其它编码都不能成功显示中文
//具体可参考 vs2013 代码 文件编码(https://blog.csdn.net/qiushangren/article/details/83411351)
//vs2013 在工程开始载入过程或者选择右键添加现有项载入带bom的文件,都会将文件进行转码,改为对应的ansi编码(windows的ansi的中文编码方案为ascii+gb18030)
System.out.println(string);