java调用c库实例

java由于语言限制,不能使用char*作为返回值。 局部变量的字符串不能正常返回,vc可以正常接收,java不能接收。

c语言

1
int __stdcall GetCharacterCodeJn(char* outjson, int* outjson_len)

  

java语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package softgranttest;
 
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
 
public class Test {
     
     // 定义接口CLibrary,继承自com.sun.jna.Library
    public interface testdll extends Library {
        testdll Instance = (testdll) Native.loadLibrary("totfranjo"  , testdll.class);
 
 
        int GetYellopCouCod(Pointer outjson_pointer,Pointer outjson_len_pointer);
         
         
    }
 
    public static void main(String[] args) throws IOException {
 
        Test_RRROOO();
         
    }
     
    /**
     * 特征码
     *
     * @throws UnsupportedEncodingException
     */
    private static void Test_RRROOO() throws UnsupportedEncodingException
    {
  
        String strRet = "";
         
        Pointer outjson_pointer = new Memory(1024);
        Pointer outjson_len_pointer = new Memory(8);
         
        int nRet = testdll.Instance.GetYellopCouCod(outjson_pointer,outjson_len_pointer);
         
        int outjson_len = outjson_len_pointer.getInt(0);
        byte[] bytOutjson = outjson_pointer.getByteArray(0, outjson_len);
         
        strRet = new String(bytOutjson, "gb2312");
         
        System.out.println(strRet);
    }
     
 
   
     
}

  

posted @   jiftle  阅读(2108)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示