跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 关于 C 双指针参数的 JNA 问题求助, 例子很简单,求大佬指点下
未分類
15 7 月 2020

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

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

資深大佬 : camork 8

话不多说,直接上代码:

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("%dn", 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 函数, 但是获取结果出错了, 也不太清楚具体获取的步骤对不对, 有没有大佬指点下.

大佬有話說 (0)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具