Seite 1 von 1

wlanapi WlanGetNetworkBssList Function

Verfasst: 19.11.2010 14:02
von jpd
hallo,

beschäftige mich zur zeit mit der Native Wifi funktionen,
anbei ein beispiel der einigermaßen funziniert aus (vb beispiel portiert)


was ich erreichen möchte ist die MAC Addressen der gefundenen Wlan router auszulesen.

die funktion die mir das ermöglichen sollte ist die "WlanGetNetworkBssList" MSDN
leider gibt mir diese aber nicht die richtigen werte zurueck.

Möglicherweise ist die struktur die ich verwende falsch implementiert!

kann jemand weiterhelfen?


anbei ein beispiel die in der lage ist die wlan namen und ggf. profile auszulesen

Vielen Dank
jpd

Code: Alles auswählen

EnableExplicit
; 
; '--------------------------------------------------------------------------------------------------------'
; '  Projet: wLan : Découverte de l'API Microsoft Native Wifi                                              '
; '  Fichier        : wlan.frm                                                                             '
; '--------------------------------------------------------------------------------------------------------'
; '                                                                                                        '
; ' tutoriel associé :  http://bbil.developpez.com/tutoriel/vb6/decouverte-api-microsoft-native-wifi/      '
; '                     http://ram-0000.developpez.com/tutoriels/cpp/decouverte-api-microsoft-native-wifi/ '
; '                                                                                                        '
; ' Auteur(s): ram-0000,bbil                                                                               '
; '                                                                                                        '
; '                                                                                                        '
; ' Historique : - 24/12/2009 Version 1.0.0 Création initiale.                                             '
; '                                                                                                        '
; ' Voir "MSDN Native Wifi : "                                                                             '
; '  http://msdn.microsoft.com/en-us/library/ms706556%28VS.85%29.aspx                                      '
; '                                                                                                        '
; '--------------------------------------------------------------------------------------------------------'
; 
; 'Déclaration des constantes et fonctions relatives à Wlan
;  PB Version created by jpd November 2010

#ERROR_SUCCESS  = 0
#DOT11_SSID_MAX_LENGTH  = 32
#WLAN_MAX_PHY_TYPE_NUMBER = 8
#WLAN_AVAILABLE_NETWORK_CONNECTED = 1
#WLAN_AVAILABLE_NETWORK_HAS_PROFILE = 2






Prototype WlanOpenHandle(dwClientVersion,pReserved,pdwNegotiatedVersion, phClientHandle) 
Prototype WlanCloseHandle(hClientHandle,pReserved)
Prototype WlanEnumInterfaces(hClientHandle,pReserved,ppInterfaceList)
Prototype WlanFreeMemory(pMemory)
Prototype WlanQueryInterface(hClientHandle, pInterfaceGuid, OpCode, pReserved, pdwDataSize, ppData, pWlanOpcodeValueType)
Prototype WlanHostedNetworkQueryStatus(hClientHandle,ppWlanHostedNetworkStatus,pvReserved);
Prototype WlanGetAvailableNetworkList(hClientHandle, pInterfaceGuid, dwFlags, pReserved, ppAvailableNetworkList)
Prototype WlanScan(hClientHandle, pInterfaceGuid, pDot11Ssid, pIeData, pReserved)
Prototype WlanGetNetworkBssList(hClientHandle, pInterfaceGuid,pDot11Ssid, dot11BssType, bSecurityEnabled, pReserved, ppWlanBssList)
Prototype WlanReasonCodeToString(dwReasonCode, dwBufferSize, pStringBuffer, pReserved)



Global WlanOpenHandle.WlanOpenHandle
Global WlanCloseHandle.WlanCloseHandle
Global WlanEnumInterfaces.WlanEnumInterfaces
Global WlanFreeMemory.WlanFreeMemory
Global WlanQueryInterface.WlanQueryInterface
Global WlanHostedNetworkQueryStatus.WlanHostedNetworkQueryStatus
Global WlanGetAvailableNetworkList.WlanGetAvailableNetworkList
Global WlanScan.WlanScan
Global WlanGetNetworkBssList.WlanGetNetworkBssList
Global WlanReasonCodeToString.WlanReasonCodeToString

Define.l AskedVersion = 0

If OSVersion() =#PB_OS_Windows_XP
  AskedVersion=1
ElseIf    OSVersion() =#PB_OS_Windows_Vista Or OSVersion() = #PB_OS_Windows_7 Or OSVersion()=#PB_OS_Windows_Server_2008 
  AskedVersion=2
EndIf

Procedure LoadWlanapi(Library) 
  Protected Result 

  Result = OpenLibrary(Library, "wlanapi.dll") 
  
  If Result 
    If Library = #PB_Any 
      Library = Result 
    EndIf 
    
    WlanOpenHandle                = GetFunction(Library, "WlanOpenHandle") 
    WlanCloseHandle               = GetFunction(Library, "WlanCloseHandle") 
    WlanEnumInterfaces            = GetFunction(Library, "WlanEnumInterfaces") 
    WlanFreeMemory                = GetFunction(Library, "WlanFreeMemory") 
    WlanQueryInterface            = GetFunction(Library, "WlanQueryInterface") 
    WlanHostedNetworkQueryStatus  = GetFunction(Library, "WlanHostedNetworkQueryStatus") 
    WlanGetAvailableNetworkList   = GetFunction(Library, "WlanGetAvailableNetworkList")
    WlanScan                      = GetFunction(Library, "WlanScan")
    WlanGetNetworkBssList         = GetFunction(Library, "WlanGetNetworkBssList")
    WlanReasonCodeToString        = GetFunction(Library, "WlanReasonCodeToString")
    
    If  WlanOpenHandle = 0 Or  WlanCloseHandle = 0 Or WlanEnumInterfaces = 0 Or WlanFreeMemory = 0 Or WlanQueryInterface = 0 Or WlanHostedNetworkQueryStatus = 0 Or WlanGetAvailableNetworkList = 0 Or  WlanScan = 0 Or WlanGetNetworkBssList = 0 Or WlanReasonCodeToString = 0
      CloseLibrary(Library) 
      Result = 0 
    EndIf      
  EndIf 

  ProcedureReturn Result 
EndProcedure 

Procedure.s GetGUID(guid)
  Protected  lpsz.s{78}
  ProcedureReturn PeekS(@lpsz, StringFromGUID2_(guid, @lpsz, 78), #PB_Unicode)
EndProcedure

#wlan_interface_state_not_ready               = 0
#wlan_interface_state_connected               = 1
#wlan_interface_state_ad_hoc_network_formed   = 2
#wlan_interface_state_disconnecting           = 3
#wlan_interface_state_disconnected            = 4
#wlan_interface_state_associating             = 5
#wlan_interface_state_discovering             = 6
#wlan_interface_state_authenticating          = 7 


Procedure.s  WLAN_INTERFACE_STATE(Value.l)
  Protected Rets.s
  Select Value
    Case  #wlan_interface_state_not_ready ;              = 0,
      Rets="wlan interface state not ready"
    Case #wlan_interface_state_connected ;              = 1,
      Rets="wlan interface state connected"
    Case #wlan_interface_state_ad_hoc_network_formed;   = 2,
      Rets="wlan interface state ad hoc network formed"
    Case #wlan_interface_state_disconnecting;           = 3,
      Rets="wlan interface state disconnecting"
    Case #wlan_interface_state_disconnected;            = 4,
      Rets="wlan interface state disconnected"
    Case #wlan_interface_state_associating;             = 5,
      Rets="wlan interface state associating"
    Case #wlan_interface_state_discovering;             = 6,
      Rets="wlan interface state discovering"
    Case #wlan_interface_state_authenticating;          = 7 
      Rets="wlan interface state authenticating"
  EndSelect
  
  ProcedureReturn Rets
EndProcedure


Structure  WLAN_INTERFACE_INFO
  InterfaceGuid.guid
  InterfaceDescription.b[512]
  IsState.l
EndStructure

Structure WLAN_INTERFACE_INFO_LIST
  dwNumberOfItems.l
  dwIndex.l
  InterfaceInfo.WLAN_INTERFACE_INFO
EndStructure
  
Structure WLAN_INTERFACE_INFO_LIST_NB
  dwNumberOfItems.l
  dwIndex.l
EndStructure

#dot11_BSS_type_infrastructure   = 1
#dot11_BSS_type_independent      = 2
#dot11_BSS_type_any              = 3 

Structure  DOT11_SSID
  uSSIDLength.l
  ucSSID.b[#DOT11_SSID_MAX_LENGTH]
EndStructure

#DOT11_RATE_SET_MAX_LENGTH = 126

;#DOT11_SSID_MAX_LENGTH  = 32
Structure WLAN_RATE_SET; {
  uRateSetLength.l;ULONG  uRateSetLength;
  usRateSet.l[#DOT11_RATE_SET_MAX_LENGTH];USHORT usRateSet[DOT11_RATE_SET_MAX_LENGTH];
EndStructure;} WLAN_RATE_SET, *PWLAN_RATE_SET;


Structure WLAN_BSS_ENTRY; {
  dot11Ssid.DOT11_SSID;        dot11Ssid;
  uPhyId.l;ULONG             uPhyId;
  dot11Bssid.b[6]
  dot11BssType.l;DOT11_BSS_TYPE    dot11BssType;
  dot11BssPhyType.l;DOT11_PHY_TYPE    dot11BssPhyType;
  lRssi.l;LONG              lRssi;
  uLinkQuality.l;ULONG             uLinkQuality;
  bInRegDomain.l;BOOLEAN           bInRegDomain;
  usBeaconPeriod.l;USHORT            usBeaconPeriod;
  ullTimestamp.q;ULONGLONG         ullTimestamp;
  ullHostTimestamp.q;ULONGLONG         ullHostTimestamp;
  usCapabilityInformation.l;USHORT            usCapabilityInformation;
  ulChCenterFrequency.l;ULONG             ulChCenterFrequency;
  wlanRateSet.WLAN_RATE_SET;_WLAN_RATE_SET   wlanRateSet;
  ulIeOffset.l;ULONG             ulIeOffset;
  ulIeSize.l;ULONG             ulIeSize;
EndStructure;} WLAN_BSS_ENTRY, *PWLAN_BSS_ENTRY;

Structure _WLAN_BSS_LIST; {
  dwTotalSize.l;DWORD          dwTotalSize;
  dwNumberOfItems.l;DWORD          dwNumberOfItems;
  ;Array wlanBssEntries.WLAN_BSS_ENTRY(1);WLAN_BSS_ENTRY wlanBssEntries[1];
EndStructure;} WLAN_BSS_LIST, *PWLAN_BSS_LIST;




Structure WLAN_AVAILABLE_NETWORK
  strProfileName.b[512]
  dot11Ssid.DOT11_SSID
  dot11BssType.l
  uNumberOfBssids.l
  bNetworkConnectable.l
  wlanNotConnectableReason.l
  uNumberOfPhyTypes.l
  dot11PhyTypes.l[#WLAN_MAX_PHY_TYPE_NUMBER ]
  bMorePhyTypes.l
  wlanSignalQuality.l
  bSEcurityEnabled.l
  dot11DefaultAuthAlgorithm.l
  dot11DefaultCipherAlgorithm.l
  dwflags.l
  dwreserved.l
EndStructure
  

Structure  WLAN_AVAILABLE_NETWORK_LIST
  dwNumberOfItems.l
  dwIndex.l
  Array Network.WLAN_AVAILABLE_NETWORK(0)
EndStructure
  
Structure WLAN_AVAILABLE_NETWORK_LIST_NB
  dwNumberOfItems.l
  dwIndex.l
EndStructure
  
Global ClientHandle.l=0

Procedure.s MacToString(*membuffer)
  Protected MAC.s=""
  Protected i
  For i=0 To 5
    MAC+RSet(Hex(PeekB(*membuffer+i)&$FF),2,"0")
    If i<5
      MAC+":"
    EndIf
  Next
  ProcedureReturn MAC
 
EndProcedure

Procedure  Terminate_enumeration()
  Protected lRet.l
  lRet = WlanCloseHandle(ClientHandle, #Null)
  If lRet <> #ERROR_SUCCESS
    Debug "Error on function WlanCloseHandle :" +Str(lRet)
  EndIf
  End 
EndProcedure

Procedure Enumerate_NetworkBssList(pInterfaceGuid)
  Protected lRet.l=0
  Protected pDot11Ssid.DOT11_SSID ; [optional]
  Protected ppw.l=0
  Protected ppWlanBssList._WLAN_BSS_LIST
  Protected NBSSEntry.WLAN_BSS_ENTRY
  Protected NEntry.l=0
  
  
  lRet = WlanGetNetworkBssList(ClientHandle, pInterfaceGuid, #Null, #dot11_BSS_type_any, #False, #Null, @ppw) 
  
  Debug GetLastError_()

  If lRet <> #ERROR_SUCCESS
    Debug "Error function  WlanGetNetworkBssList:" +Str(lRet)
    ProcedureReturn 0
  EndIf
  CopyMemory(ppw, ppWlanBssList, SizeOf(_WLAN_BSS_LIST))
  If ppWlanBssList\dwNumberOfItems > 0
     Protected Dim nbss.WLAN_BSS_ENTRY(ppWlanBssList\dwNumberOfItems )
    CopyMemory(ppWlanBssList, nbss(0), ppWlanBssList\dwNumberOfItems * SizeOf(WLAN_BSS_ENTRY))
    For NEntry = 0 To    ppWlanBssList\dwNumberOfItems -1
      Debug "------------------------------------------------"
      Debug "BSS Nr: " +Str(ppWlanBssList\dwNumberOfItems)
      
      NBSSEntry=nbss(NEntry)
      Debug PeekS(@NBSSEntry\dot11Ssid\ucSSID,-1,#PB_Ascii)
      ;PeekS(@wAN_info\dot11Ssid\ucSSID)
      Debug "Mac:  "+MacToString(@NBSSEntry\dot11Bssid)
      Protected bsstype.s=""
      Select NBSSEntry\dot11BssType
        Case  #dot11_BSS_type_infrastructure 
          bsstype="infrastructure"
        Case #dot11_BSS_type_independent
          bsstype="indipendent"
        Case #dot11_BSS_type_any        
          bsstype ="BSS_type_any"
        Default
          bsstype ="unknow"
      EndSelect
      Debug "BSS Type: " +bsstype
   Next
  EndIf
  
    WlanFreeMemory(ppw)
EndProcedure

Procedure Enumerate_Network(pInterfaceGuid)
  Protected lRet.l
  Protected flag.l=0
  Protected iBoucle.l
  Protected pList_NetWork.l
  Protected List_NetWork.WLAN_AVAILABLE_NETWORK_LIST_NB
  Protected wAN_info.WLAN_AVAILABLE_NETWORK
  Protected  stSSID.s
  Protected stType.s
  
  lRet = WlanGetAvailableNetworkList(ClientHandle, pInterfaceGuid, flag, #Null, @pList_NetWork)
  
  
  If lRet <> #ERROR_SUCCESS
    Debug "Error function WlanGetAvailableNetworkList :" +Str(lRet)
    ProcedureReturn 0
  EndIf
  Protected Dim mNetwork.WLAN_AVAILABLE_NETWORK(0)
  CopyMemory(pList_NetWork, List_NetWork, SizeOf(WLAN_AVAILABLE_NETWORK_LIST_NB))
  Debug "Number of Wifi Networks : " +Str(List_NetWork\dwNumberOfItems)
  ReDim mNetwork(List_NetWork\dwNumberOfItems-1)
  CopyMemory(pList_NetWork + SizeOf(WLAN_AVAILABLE_NETWORK_LIST_NB),mNetwork(0), List_NetWork\dwNumberOfItems * SizeOf(WLAN_AVAILABLE_NETWORK))
  
  For iBoucle = 0 To List_NetWork\dwNumberOfItems -1
    wAN_info = mNetwork(iBoucle)
    Debug "ProfileName: "+PeekS(@wAN_info\strProfileName,-1,#PB_Unicode)
    Debug "SSID length: "+Str(wAN_info\dot11Ssid\uSSIDLength)
    Debug "SSID: "+ PeekS(@wAN_info\dot11Ssid\ucSSID,-1,#PB_Ascii)
    Select wAN_info\dot11BssType
      Case 1: stType = "dot11_BSS_type_infrastructure"
      Case 2: stType = "dot11_BSS_type_independent"
      Case 3: stType = "dot11_BSS_type_any"
    EndSelect
    Debug "Type = :" +stType 
    Debug "Number of BSSID : " +Str(wAN_info\uNumberOfBssids)
    If wAN_info\bNetworkConnectable = 0
      Protected lNbCar.l=1024
      Protected st.s
      st = Space(lNbCar)
      If WlanReasonCodeToString(wAN_info\wlanNotConnectableReason, lNbCar, @st, #Null) = #ERROR_SUCCESS
        Debug PeekS(@st)
        Debug "WlanReasonCode = [0x" + Hex(wAN_info\wlanNotConnectableReason) + "] : " + PeekS(@st)
      Else
        Debug"WlanReasonCode = [0x" + Hex(wAN_info\wlanNotConnectableReason) + "] (The reason for failure is unknown."
      EndIf
    Else
      Debug "the resource is connectable"
    EndIf
    Debug "type physique number:" + Str(wAN_info\uNumberOfPhyTypes)
    Protected idx.l
    Protected stPhyType.s
    For idx = 0 To wAN_info\uNumberOfPhyTypes - 1
      Select wAN_info\dot11PhyTypes[idx]
        Case 0: stPhyType = "dot11_phy_type_any"
        Case 1: stPhyType = "dot11_phy_type_fhss"
        Case 2: stPhyType = "dot11_phy_type_dsss"
        Case 3: stPhyType = "dot11_phy_type_irbaseband"
        Case 4: stPhyType = "dot11_phy_type_ofdm"
        Case 5: stPhyType = "dot11_phy_type_hrdsss"
        Case 6: stPhyType = "dot11_phy_type_erp"
        Case 7: stPhyType = "dot11_phy_type_ht"
        Case $80000000: stPhyType = "dot11_phy_type_IHV_start"
        Case $FFFFFFFF: stPhyType = "dot11_phy_type_IHV_end"
        Default :stPhyType = "Unknow"
      EndSelect
      Debug "type physique: " +Str(idx) + " is " + stPhyType
    Next 
    If wAN_info\bMorePhyTypes = #True
      Debug "somthing To do here To get the complete List With WlanGetNetworkBssList()"
    EndIf
    Debug "signal quality : " + Str(wAN_info\wlanSignalQuality) + " %"
    
    If wAN_info\bSEcurityEnabled 
      Debug "security is activated"
    Else
      Debug "security is deactivated"
    EndIf
    Protected stAuth_algo.s
    Select wAN_info\dot11DefaultAuthAlgorithm
      Case 1: stAuth_algo = "DOT11_AUTH_ALGO_80211_OPEN"
      Case 2: stAuth_algo = "DOT11_AUTH_ALGO_80211_SHARED_KEY"
      Case 3: stAuth_algo = "DOT11_AUTH_ALGO_WPA"
      Case 4: stAuth_algo = "DOT11_AUTH_ALGO_WPA_PSK"
      Case 5: stAuth_algo = "DOT11_AUTH_ALGO_WPA_NONE"
      Case 6: stAuth_algo = "DOT11_AUTH_ALGO_RSNA"
      Case 7: stAuth_algo = "DOT11_AUTH_ALGO_RSNA_PSK"
      Case $80000000: stAuth_algo = "DOT11_AUTH_ALGO_IHV_START"
      Case $FFFFFFFF: stAuth_algo = "DOT11_AUTH_ALGO_IHV_END "
      Default:stAuth_algo = "Unknow"
    EndSelect
    Debug "authentification algorithm: " + stAuth_algo
    Protected stCipher_algo.s
    Select wAN_info\dot11DefaultCipherAlgorithm
      Case 0: stCipher_algo = "DOT11_CIPHER_ALGO_NONE"
      Case 1: stCipher_algo = "DOT11_CIPHER_ALGO_WEP40"
      Case 2: stCipher_algo = "DOT11_CIPHER_ALGO_TKIP "
      Case 4: stCipher_algo = "DOT11_CIPHER_ALGO_CCMP"
      Case 5: stCipher_algo = "DOT11_CIPHER_ALGO_WEP104"
      Case $100: stCipher_algo = "DOT11_CIPHER_ALGO_WPA_USE_GROUP"
      Case $100: stCipher_algo = "DOT11_CIPHER_ALGO_RSN_USE_GROUP"
      Case $101: stCipher_algo = "DOT11_CIPHER_ALGO_WEP"
      Case $80000000: stCipher_algo = "DOT11_CIPHER_ALGO_IHV_START"
      Case $FFFFFFFF: stCipher_algo = "DOT11_CIPHER_ALGO_IHV_END"
      Default : stCipher_algo = "Unknow"
    EndSelect
    
    Debug "crypto Algorithm: "+ stCipher_algo 
    Debug "flags: "+Str(wAN_info\dwflags)
    If wAN_info\dwflags & #WLAN_AVAILABLE_NETWORK_CONNECTED = #WLAN_AVAILABLE_NETWORK_CONNECTED 
      Debug "Flags WLAN_AVAILABLE_NETWORK_CONNECTED is activ"
    EndIf
      If wAN_info\dwflags & #WLAN_AVAILABLE_NETWORK_HAS_PROFILE = #WLAN_AVAILABLE_NETWORK_HAS_PROFILE 
      Debug "Flags WLAN_AVAILABLE_NETWORK_HAS_PROFILE is activ"
      Debug "reserverd : " + Str(wAN_info\dwreserved)
    EndIf  
  Next
  WlanFreeMemory(pList_NetWork)
EndProcedure

Procedure Enumerate_interface()
    Protected list_interface.WLAN_INTERFACE_INFO_LIST_NB
    Protected pList_interface.l
    Protected lRet.l
    Protected wI_info.WLAN_INTERFACE_INFO
    Protected stGuid.s
    Protected stEtat.s
    Protected Dim wiInterfaces.WLAN_INTERFACE_INFO(0)
    Protected iInterface.l
    lRet = WlanEnumInterfaces(ClientHandle, #Null, @pList_interface)


    If lRet <> #ERROR_SUCCESS
        Debug "Error function WlanEnumInterfaces :" +Str(lRet)
        ProcedureReturn 0
    EndIf
    
    CopyMemory(pList_interface, list_interface,SizeOf(WLAN_INTERFACE_INFO_LIST_NB))
    Debug "Number of Wifi interface: " +Str(list_interface\dwNumberOfItems)
    
    If list_interface\dwNumberOfItems = 0 
      Debug "---------------- No Wifi interface ---------------"
      ProcedureReturn 0
    EndIf
    
    ReDim wiInterfaces(list_interface\dwNumberOfItems - 1)
    CopyMemory(pList_interface + SizeOf(WLAN_INTERFACE_INFO_LIST_NB), wiInterfaces(0), SizeOf(WLAN_INTERFACE_INFO) * list_interface\dwNumberOfItems)
    For iInterface = 0 To list_interface\dwNumberOfItems - 1
      Debug "------------------------------------------------"
      Debug "interface Nr: " +Str(iInterface)
      wI_info = wiInterfaces(iInterface)
      Debug "GUID :"+GetGUID(wI_info\InterfaceGuid)
      Debug "interface Wifi Description :" + PeekS(@wI_info\InterfaceDescription,-1,#PB_Unicode)
      Debug "Interface Wifi State: "+WLAN_INTERFACE_STATE(wI_info\IsState)
      Enumerate_Network(wI_info\InterfaceGuid)
      Enumerate_NetworkBssList(wI_info\InterfaceGuid)
      WlanFreeMemory(pList_interface)
    Next
EndProcedure

If LoadWlanapi(0) 
  Define lRet.l=0
  Define    NegotiatedVersion.l=0
  lRet = WlanOpenHandle(AskedVersion, #Null, @NegotiatedVersion, @ClientHandle)
  If lRet <> #ERROR_SUCCESS
    Debug "Error function WlanOpenHandle :" +Str(lRet)
    End
  EndIf
  Debug "version of WLAN asked is: " + Str(AskedVersion) + "version  obtained : " + Str(NegotiatedVersion)
  Enumerate_interface()
  Terminate_enumeration()
    ;
  CloseLibrary(0) 
  
Else
  Debug "load failed"
EndIf