我正在尝试用Java映射Windows Native Wifi API,但WlanEnumInterfaces函数遇到问题。基本上,此函数输出包含WLAN_INTERFACE_INFO结构数组的WLAN_INTERFACE_INFO_LIST结构。
当我在Java程序中调用WlanEnumInterfaces时,它的返回值表示该函数成功,但是当我检查WLAN_INTERFACE_INFO数组中的项数时,得到的值不一致,如1367280或4000000。该值应为1,因为我只有我的机器上有一个无线接口。
这是WlanEnumInterfaces函数的映射:
public int WlanEnumInterfaces(
HANDLE hClientHandle,
PointerByReference pReserved,
WLAN_INTERFACE_INFO_LIST.ByReference ppInterfaceList);
WLAN_INTERFACE_INFO_LIST的实现:
public static class WLAN_INTERFACE_INFO_LIST extends Structure {
public static class ByReference extends WLAN_INTERFACE_INFO_LIST implements Structure.ByReference{}
public int dwNumberOfItems;
public int dwIndex;
public WLAN_INTERFACE_INFO[] InterfaceInfo = new WLAN_INTERFACE_INFO[10];
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[] {"dwNumberOfItems","dwIndex","InterfaceInfo"});
}
}
以及调用该函数的部分代码:
HANDLE wlanHandle = getWlanHandle();
WLAN_INTERFACE_INFO_LIST.ByReference ppInterfaceList = new WLAN_INTERFACE_INFO_LIST.ByReference();
int ret = Wlanapi.INSTANCE.WlanEnumInterfaces(wlanHandle,null,ppInterfaceList);
System.out.println("Return: " + ret);
System.out.println("WlanEnumInterfaces->number of items: "+ppInterfaceList.dwNumberOfItems);
有人知道发生了什么吗?
谢谢!
最佳答案
WlanEnumInterfaces
将为您提供的字段填充结构的地址。您传递的是结构的地址,而不是地址的地址。
使用PointerByReference
获取结构的地址,例如
PointerByReference pref = new PointerByReference();
Wlanapi.INSTANCE.WlanEnumInterfaces(handle, null, pref);
WLAN_INTERFACE_INFO_LIST list = new WLAN_INTERFACE_INFO_LIST(pref.getValue());
然后您需要
Pointer
的基于WLAN_INTERFACE_INFO_LIST
的构造函数,即WLAN_INTERFACE_INFO_LIST(Pointer p) {
super(p);
this.dwNumberOfItems = p.readInt(0);
this.InterfaceInfo = new WLAN_INTERFACE_INFO[this.dwNumberOfItems];
read();
}