WlanGetAvailableNetworkList

原文msdn链接地址:https://docs.microsoft.com/zh-cn/windows/desktop/api/wlanapi/nf-wlanapi-wlangetavailablenetworklist

  1 #ifndef UNICODE
  2 #define UNICODE
  3 #endif
  4 
  5 #include <windows.h>
  6 #include <wlanapi.h>
  7 #include <objbase.h>
  8 #include <wtypes.h>
  9 
 10 #include <stdio.h>
 11 #include <stdlib.h>
 12 
 13 // Need to link with Wlanapi.lib and Ole32.lib
 14 #pragma comment(lib, "wlanapi.lib")
 15 #pragma comment(lib, "ole32.lib")
 16 
 17 
 18 int wmain()
 19 {
 20 
 21     // Declare and initialize variables.
 22 
 23     HANDLE hClient = NULL;
 24     DWORD dwMaxClient = 2;      //    
 25     DWORD dwCurVersion = 0;
 26     DWORD dwResult = 0;
 27     DWORD dwRetVal = 0;
 28     int iRet = 0;
 29     
 30     WCHAR GuidString[39] = {0};
 31 
 32     unsigned int i, j, k;
 33 
 34     /* variables used for WlanEnumInterfaces  */
 35 
 36     PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
 37     PWLAN_INTERFACE_INFO pIfInfo = NULL;
 38 
 39     PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
 40     PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;
 41     
 42     int iRSSI = 0;
 43 
 44     dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
 45     if (dwResult != ERROR_SUCCESS) {
 46         wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
 47         return 1;
 48         // You can use FormatMessage here to find out why the function failed
 49     }
 50 
 51     dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
 52     if (dwResult != ERROR_SUCCESS) {
 53         wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
 54         return 1;
 55         // You can use FormatMessage here to find out why the function failed
 56     } else {
 57         wprintf(L"Num Entries: %lu\n", pIfList->dwNumberOfItems);
 58         wprintf(L"Current Index: %lu\n", pIfList->dwIndex);
 59         for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) {
 60             pIfInfo = (WLAN_INTERFACE_INFO *) &pIfList->InterfaceInfo[i];
 61             wprintf(L"  Interface Index[%u]:\t %lu\n", i, i);
 62             iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString, 
 63                 sizeof(GuidString)/sizeof(*GuidString)); 
 64             // For c rather than C++ source code, the above line needs to be
 65             // iRet = StringFromGUID2(&pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString, 
 66             //     sizeof(GuidString)/sizeof(*GuidString)); 
 67             if (iRet == 0)
 68                 wprintf(L"StringFromGUID2 failed\n");
 69             else {
 70                 wprintf(L"  InterfaceGUID[%d]: %ws\n",i, GuidString);
 71             }    
 72             wprintf(L"  Interface Description[%d]: %ws", i, 
 73                 pIfInfo->strInterfaceDescription);
 74             wprintf(L"\n");
 75             wprintf(L"  Interface State[%d]:\t ", i);
 76             switch (pIfInfo->isState) {
 77             case wlan_interface_state_not_ready:
 78                 wprintf(L"Not ready\n");
 79                 break;
 80             case wlan_interface_state_connected:
 81                 wprintf(L"Connected\n");
 82                 break;
 83             case wlan_interface_state_ad_hoc_network_formed:
 84                 wprintf(L"First node in a ad hoc network\n");
 85                 break;
 86             case wlan_interface_state_disconnecting:
 87                 wprintf(L"Disconnecting\n");
 88                 break;
 89             case wlan_interface_state_disconnected:
 90                 wprintf(L"Not connected\n");
 91                 break;
 92             case wlan_interface_state_associating:
 93                 wprintf(L"Attempting to associate with a network\n");
 94                 break;
 95             case wlan_interface_state_discovering:
 96                 wprintf(L"Auto configuration is discovering settings for the network\n");
 97                 break;
 98             case wlan_interface_state_authenticating:
 99                 wprintf(L"In process of authenticating\n");
100                 break;
101             default:
102                 wprintf(L"Unknown state %ld\n", pIfInfo->isState);
103                 break;
104             }
105             wprintf(L"\n");
106 
107             dwResult = WlanGetAvailableNetworkList(hClient,
108                                              &pIfInfo->InterfaceGuid,
109                                              0, 
110                                              NULL, 
111                                              &pBssList);
112 
113             if (dwResult != ERROR_SUCCESS) {
114                 wprintf(L"WlanGetAvailableNetworkList failed with error: %u\n",
115                         dwResult);
116                 dwRetVal = 1;
117                 // You can use FormatMessage to find out why the function failed
118             } else {
119                 wprintf(L"WLAN_AVAILABLE_NETWORK_LIST for this interface\n");
120 
121                 wprintf(L"  Num Entries: %lu\n\n", pBssList->dwNumberOfItems);
122 
123                 for (j = 0; j < pBssList->dwNumberOfItems; j++) {
124                     pBssEntry =
125                         (WLAN_AVAILABLE_NETWORK *) & pBssList->Network[j];
126 
127                     wprintf(L"  Profile Name[%u]:  %ws\n", j, pBssEntry->strProfileName);
128                     
129                     wprintf(L"  SSID[%u]:\t\t ", j);
130                     if (pBssEntry->dot11Ssid.uSSIDLength == 0)
131                         wprintf(L"\n");
132                     else {   
133                         for (k = 0; k < pBssEntry->dot11Ssid.uSSIDLength; k++) {
134                             wprintf(L"%c", (int) pBssEntry->dot11Ssid.ucSSID[k]);
135                         }
136                         wprintf(L"\n");
137                     }
138                         
139                     wprintf(L"  BSS Network type[%u]:\t ", j);
140                     switch (pBssEntry->dot11BssType) {
141                     case dot11_BSS_type_infrastructure   :
142                         wprintf(L"Infrastructure (%u)\n", pBssEntry->dot11BssType);
143                         break;
144                     case dot11_BSS_type_independent:
145                         wprintf(L"Infrastructure (%u)\n", pBssEntry->dot11BssType);
146                         break;
147                     default:
148                         wprintf(L"Other (%lu)\n", pBssEntry->dot11BssType);
149                         break;
150                     }
151                                 
152                     wprintf(L"  Number of BSSIDs[%u]:\t %u\n", j, pBssEntry->uNumberOfBssids);
153 
154                     wprintf(L"  Connectable[%u]:\t ", j);
155                     if (pBssEntry->bNetworkConnectable)
156                         wprintf(L"Yes\n");
157                     else {
158                         wprintf(L"No\n");
159                         wprintf(L"  Not connectable WLAN_REASON_CODE value[%u]:\t %u\n", j, 
160                             pBssEntry->wlanNotConnectableReason);
161                     }        
162 
163                     wprintf(L"  Number of PHY types supported[%u]:\t %u\n", j, pBssEntry->uNumberOfPhyTypes);
164 
165                     if (pBssEntry->wlanSignalQuality == 0)
166                         iRSSI = -100;
167                     else if (pBssEntry->wlanSignalQuality == 100)   
168                         iRSSI = -50;
169                     else
170                         iRSSI = -100 + (pBssEntry->wlanSignalQuality/2);    
171                         
172                     wprintf(L"  Signal Quality[%u]:\t %u (RSSI: %i dBm)\n", j, 
173                         pBssEntry->wlanSignalQuality, iRSSI);
174 
175                     wprintf(L"  Security Enabled[%u]:\t ", j);
176                     if (pBssEntry->bSecurityEnabled)
177                         wprintf(L"Yes\n");
178                     else
179                         wprintf(L"No\n");
180                         
181                     wprintf(L"  Default AuthAlgorithm[%u]: ", j);
182                     switch (pBssEntry->dot11DefaultAuthAlgorithm) {
183                     case DOT11_AUTH_ALGO_80211_OPEN:
184                         wprintf(L"802.11 Open (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
185                         break;
186                     case DOT11_AUTH_ALGO_80211_SHARED_KEY:
187                         wprintf(L"802.11 Shared (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
188                         break;
189                     case DOT11_AUTH_ALGO_WPA:
190                         wprintf(L"WPA (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
191                         break;
192                     case DOT11_AUTH_ALGO_WPA_PSK:
193                         wprintf(L"WPA-PSK (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
194                         break;
195                     case DOT11_AUTH_ALGO_WPA_NONE:
196                         wprintf(L"WPA-None (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
197                         break;
198                     case DOT11_AUTH_ALGO_RSNA:
199                         wprintf(L"RSNA (%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
200                         break;
201                     case DOT11_AUTH_ALGO_RSNA_PSK:
202                         wprintf(L"RSNA with PSK(%u)\n", pBssEntry->dot11DefaultAuthAlgorithm);
203                         break;
204                     default:
205                         wprintf(L"Other (%lu)\n", pBssEntry->dot11DefaultAuthAlgorithm);
206                         break;
207                     }
208                         
209                     wprintf(L"  Default CipherAlgorithm[%u]: ", j);
210                     switch (pBssEntry->dot11DefaultCipherAlgorithm) {
211                     case DOT11_CIPHER_ALGO_NONE:
212                         wprintf(L"None (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
213                         break;
214                     case DOT11_CIPHER_ALGO_WEP40:
215                         wprintf(L"WEP-40 (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
216                         break;
217                     case DOT11_CIPHER_ALGO_TKIP:
218                         wprintf(L"TKIP (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
219                         break;
220                     case DOT11_CIPHER_ALGO_CCMP:
221                         wprintf(L"CCMP (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
222                         break;
223                     case DOT11_CIPHER_ALGO_WEP104:
224                         wprintf(L"WEP-104 (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
225                         break;
226                     case DOT11_CIPHER_ALGO_WEP:
227                         wprintf(L"WEP (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
228                         break;
229                     default:
230                         wprintf(L"Other (0x%x)\n", pBssEntry->dot11DefaultCipherAlgorithm);
231                         break;
232                     }
233 
234                     wprintf(L"  Flags[%u]:\t 0x%x", j, pBssEntry->dwFlags);
235                     if (pBssEntry->dwFlags) {
236                         if (pBssEntry->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
237                             wprintf(L" - Currently connected");
238                         if (pBssEntry->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
239                             wprintf(L" - Has profile");
240                     }   
241                     wprintf(L"\n");
242                     
243                     wprintf(L"\n");
244                 }
245             }
246         }
247 
248     }
249     if (pBssList != NULL) {
250         WlanFreeMemory(pBssList);
251         pBssList = NULL;
252     }
253 
254     if (pIfList != NULL) {
255         WlanFreeMemory(pIfList);
256         pIfList = NULL;
257     }
258 
259     return dwRetVal;
260 }

 

posted @ 2019-02-27 16:13  wuyuan2011woaini  阅读(1062)  评论(0编辑  收藏  举报