问题
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