How to map a pointer pointer to a structure by JNA?

北城以北 提交于 2019-12-11 06:09:38

问题


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)

The above method will retun a HostNameEntry back the caller.

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

Thanks a lot


回答1:


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());


来源:https://stackoverflow.com/questions/12040072/how-to-map-a-pointer-pointer-to-a-structure-by-jna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!