2014-04-28 47 views
2

我試圖在Java中映射Windows Native API,但是我遇到了WlanEnumInterfaces函數的問題。基本上,這個函數輸出一個包含WLAN_INTERFACE_INFO結構數組的WLAN_INTERFACE_INFO_LIST結構。 當我在我的Java程序中調用WlanEnumInterfaces時,它的返回值指示函數成功,但是當我檢查WLAN_INTERFACE_INFO數組中的項數時,得到的值不一致,如1367280或4000000.該值應爲1,因爲我有我的機器上只有一個無線接口。 這是我的WlanEnumInterfaces功能的映射:Java中的WlanEnumInterfaces(Windows Native WiFi)的奇數

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

有誰知道什麼是

public int WlanEnumInterfaces(
     HANDLE hClientHandle, 
     PointerByReference pReserved, 
     WLAN_INTERFACE_INFO_LIST.ByReference ppInterfaceList); 

WLAN_INTERFACE_INFO_LIST的實現發生了什麼? 謝謝!

回答

2

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(); 
} 
+0

我其實是與事實,我不知道如何操作PointerByReference得到WLAN_INTERFACE_INFO_LIST掙扎。祕密是基於指針的構造函數。謝謝! – gustavohc