2010-09-15 147 views
0

我目前正在嘗試使用wlanapi.dll WlanQueryInterface調用從無線接入點檢索信號強度。C#wlanapi.dll問題接收信號強度

這裏是我的API聲明

[DllImport("wlanapi.dll", SetLastError = true)] 
private static extern UInt32 WlanQueryInterface(IntPtr hClientHandle, ref Guid pInterfaceGuid, WLAN_INTF_OPCODE OpCode, IntPtr pReserved, out Int32 pdwDataSize, ref IntPtr ppData, IntPtr pWlanOpCodeValueType); 

,當我看着它被設置爲5400微軟API文檔WLAN_ASSOCIATION_ATTRIBUTES結構wlanSignalStrength進入的問題是說,它應該是0之間的值 - 100 。

這裏是我的結構聲明:

/// <summary> 
/// Structure contains association attributes for a connection. 
/// </summary> 
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
private struct WLAN_ASSOCIATION_ATTRIBUTES 
{ 
    /// <summary> 
    /// A DOT11_SSID structure that contains the SSID of the association. 
    /// </summary> 
    public DOT11_SSID dot11Ssid; 
    /// <summary> 
    /// A DOT11_BSS_TYPE value that specifies whether the network is infrastructure or ad hoc. 
    /// </summary> 
    public DOT11_BSS_TYPE dot11BssType; 
    /// <summary> 
    /// A DOT11_MAC_ADDRESS that contains the BSSID of the association. 
    /// </summary> 
    public DOT11_MAC_ADDRESS dot11Bssid; 
    /// <summary> 
    /// A DOT11_PHY_TYPE value that indicates the physical type of the association 
    /// </summary> 
    public DOT11_PHY_TYPE dot11PhyType; 
    /// <summary> 
    /// The position of the DOT11_PHY_TYPE value in the structure containing the list of PHY types. 
    /// </summary> 
    public ulong uDot11PhyIndex; 
    /// <summary> 
    /// A percentage value that represents the signal quality of the network. 
    /// </summary> 
    public Int32 wlanSignalQuality; 
    /// <summary> 
    /// Contains the receiving rate of the association. 
    /// </summary> 
    public ulong ulRxRate; 
    /// <summary> 
    /// Contains the transmission rate of the association. 
    /// </summary> 
    public ulong ulTxRate; 
} 

這是我的呼籲WlanQueryInterface:

Int32 iDataSize; 
    IntPtr ppData = IntPtr.Zero; 
    WLAN_CONNECTION_ATTRIBUTES wcaAttributes = new WLAN_CONNECTION_ATTRIBUTES(); 
    String[] sReturn = new String[4]; 

    if (WlanQueryInterface(pClientHandle, ref gInterfaceGUID, WLAN_INTF_OPCODE.wlan_intf_opcode_current_connection, IntPtr.Zero, out iDataSize, ref ppData, IntPtr.Zero) == ERROR_SUCCESS) 
    { 
     wcaAttributes = (WLAN_CONNECTION_ATTRIBUTES)Marshal.PtrToStructure(ppData, typeof(WLAN_CONNECTION_ATTRIBUTES)); 

     sReturn[0] = wcaAttributes.wlanAssociationAttributes.dot11Ssid.ucSSID; 
     sReturn[1] = wcaAttributes.strProfileName; 
     sReturn[2] = String.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}", wcaAttributes.wlanAssociationAttributes.dot11Bssid.bOne, wcaAttributes.wlanAssociationAttributes.dot11Bssid.bTwo, wcaAttributes.wlanAssociationAttributes.dot11Bssid.bThree, wcaAttributes.wlanAssociationAttributes.dot11Bssid.bFour, wcaAttributes.wlanAssociationAttributes.dot11Bssid.bFive, wcaAttributes.wlanAssociationAttributes.dot11Bssid.bSix); 
     sReturn[3] = wcaAttributes.wlanAssociationAttributes.wlanSignalQuality.ToString(); //This returns 5400 when the actual strength is ~99 

     WlanFreeMemory(ppData); 

     return sReturn; 
    } 
    else 
    { 
     throw new Exception("Unable to get interface connected SSID."); 
    } 

有沒有人看到我做錯了什麼?提前致謝!

回答

1

我終於搞定了。我必須將Layout_Kind設置爲顯式的WLAN_ASSOCIATION_ATTRIBUTES,並將字符集設置爲Unicode。

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)] 
private struct WLAN_ASSOCIATION_ATTRIBUTES 
{ 
    /// <summary> 
    /// A DOT11_SSID structure that contains the SSID of the association. 
    /// </summary> 
    [FieldOffset(0)] 
    public DOT11_SSID dot11Ssid; 
    /// <summary> 
    /// A DOT11_BSS_TYPE value that specifies whether the network is infrastructure or ad hoc. 
    /// </summary> 
    [FieldOffset(0x24)] 
    public DOT11_BSS_TYPE dot11BssType; 
    /// <summary> 
    /// A DOT11_MAC_ADDRESS that contains the BSSID of the association. 
    /// </summary> 
    [FieldOffset(40)] 
    public DOT11_MAC_ADDRESS dot11Bssid; 
    /// <summary> 
    /// A DOT11_PHY_TYPE value that indicates the physical type of the association 
    /// </summary> 
    [FieldOffset(0x30)] 
    public DOT11_PHY_TYPE dot11PhyType; 
    /// <summary> 
    /// The position of the DOT11_PHY_TYPE value in the structure containing the list of PHY types. 
    /// </summary> 
    [FieldOffset(0x34)] 
    public ulong uDot11PhyIndex; 
    /// <summary> 
    /// A percentage value that represents the signal quality of the network. 
    /// </summary> 
    [FieldOffset(0x38)] 
    public Int32 wlanSignalQuality; 
    /// <summary> 
    /// Contains the receiving rate of the association. 
    /// </summary> 
    [FieldOffset(60)] 
    public ulong ulRxRate; 
    /// <summary> 
    /// Contains the transmission rate of the association. 
    /// </summary> 
    [FieldOffset(0x40)] 
    public ulong ulTxRate; 
} 
+0

我想你應該接受這個答案,只要冷卻時間允許你,除非管理員來了。 – 2011-01-05 17:54:17