如何通过JNA将指针映射到结构

如何通过JNA将指针映射到结构

本文介绍了如何通过JNA将指针映射到结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经定义了一个c结构,如下所示:

Now I have defined a c structure as following:

struct HostNameEntry {
    char *hostName;
    struct HostNameEntry *next;
};

我定义了一种方法,如下所示:

And I have defined a method as following:

listHosts(HostNameEntry ** hostNameListPtr)

listHosts(HostNameEntry **hostNameListPtr)

上述方法将把HostNameEntry重新调回调用方.

The above method will retun a HostNameEntry back the caller.

如何通过JNA映射此结构/方法?以及如何获取存储在HostNameEntry中的主机名?

How to mapping this structure/method by JNA? And how to get the hostName stored in HostNameEntry?

非常感谢

推荐答案

您可以使用Structure.ByReference标记HostNameEntry类的版本,以强制字段采用指针值(而不是内联).

You tag a version of your HostNameEntry class with Structure.ByReference to force the field to take on a pointer value (instead of being inlined).

public class HostNameEntry extends Structure {
   public static class ByReference extends HostNameEntry implements Structure.ByReference { }

   public String hostName;
   public HostNameEntry.ByReference next;

   public HostNameEntry() { }
   public HostNameEntry(Pointer p) { super(p); read(); }
}

public interface MyInterface extends Library {
    MyInterface INSTANCE = ...;
    void listHosts(PointerByReference pr);
}

// actual usage
PointerByReference pref = new PointerByReference();
MyInterface.INSTANCE.listHosts(pref);
HostNameEntry first = new HostNameEntry(pref.getValue());

这篇关于如何通过JNA将指针映射到结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 02:51