V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
camork
V2EX  ›  Java

关于 C 双指针参数的 JNA 问题求助, 例子很简单,求大佬指点下

  •  
  •   camork ·
    camork · 2020-07-14 23:54:05 +08:00 · 1471 次点击
    这是一个创建于 1383 天前的主题,其中的信息可能已经有所发展或是发生改变。

    话不多说,直接上代码:

    C 结构体

    typedef struct _A {
        unsigned int num;
        struct _A *next;
    } A, *PA;
    
    

    测试方法

    void test(PA *a) {
        PA current = (PA) malloc(sizeof(A));
        current->num = 123321;
    
        PA next = (PA) malloc(sizeof(A));
        next->num = 456;
        current->next = next;
    
        *a = current;
    }
    

    这是在 C 里面的运行例子,

    int main() {
        PA a = NULL;
        test(&a);
    
        printf("%d\n", a->num);
        printf("%d", a->next->num);
    }
    

    JNA 代码(不确定映射对不对)

    public interface DLLLibrary extends Library {
    	......
    
    	void test(PointerByReference a);
    }
    
    public class A extends Structure {
    	public int num;
    	public ByReference next;
    	public A() {
    		super();
    	}
    	protected List<String> getFieldOrder() {
    		return Arrays.asList("num", "next");
    	}
    
    	public A(Pointer peer) {
    		super(peer);
    	}
    	public static class ByReference extends A implements Structure.ByReference { }
    	public static class ByValue extends A implements Structure.ByValue { }
    }
    
    public static void main(String[] args) {
        PointerByReference pointer = new PointerByReference();
        DLLLibrary.INSTANCE.test(pointer);
    
        assert pointer.getValue().getInt(0) == 123321; //这里是正确的
        A a = new A(pointer.getValue());
        //assert a.next.num == 456;  //后续期望的调用
        a.read(); //java.lang.Error: Invalid memory access
    }
    

    最后 Java 这边传个双重指针给 C 函数, 但是获取结果出错了, 也不太清楚具体获取的步骤对不对, 有没有大佬指点下.

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3240 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:05 · PVG 20:05 · LAX 05:05 · JFK 08:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.