Advertisement
JoshuaB

PingDestroyer

Jun 5th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 <conio.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. int wmain()
  18. {
  19.     HANDLE client_handle = nullptr;
  20.     {
  21.         DWORD max_client_version = 2;
  22.         DWORD current_version = 0;
  23.         auto result = WlanOpenHandle(max_client_version, nullptr, &current_version, &client_handle);
  24.         if (result != ERROR_SUCCESS)  {
  25.             wprintf(L"WlanOpenHandle failed with error: %u\n", result);
  26.             // FormatMessage can be used to find out why the function failed
  27.             return 1;
  28.         }
  29.     }
  30.  
  31.     PWLAN_INTERFACE_INFO_LIST interface_list = nullptr;
  32.     {
  33.         auto result = WlanEnumInterfaces(client_handle, nullptr, &interface_list);
  34.         if (result != ERROR_SUCCESS)  {
  35.             wprintf(L"WlanEnumInterfaces failed with error: %u\n", result);
  36.             // FormatMessage can be used to find out why the function failed
  37.             return 1;
  38.         }
  39.     }
  40.  
  41.     wprintf(L"Num Entries: %lu\n", interface_list->dwNumberOfItems);
  42.     wprintf(L"Current Index: %lu\n", interface_list->dwIndex);
  43.  
  44.     PWLAN_INTERFACE_INFO interface_info = nullptr;
  45.     for (auto i = 0; i < static_cast<int>(interface_list->dwNumberOfItems); i++) {
  46.         interface_info = static_cast<WLAN_INTERFACE_INFO*>(&interface_list->InterfaceInfo[i]);
  47.         wprintf(L"  Interface Index[%d]:\t %lu\n", i, i);
  48.  
  49.         WCHAR guid[40] = { 0 };
  50.         auto result = StringFromGUID2(interface_info->InterfaceGuid, reinterpret_cast<LPOLESTR>(&guid), 39);
  51.         if (result == 0)
  52.             wprintf(L"StringFromGUID2 failed\n");
  53.         else {
  54.             wprintf(L"  InterfaceGUID[%d]: %ws\n", i, guid);
  55.         }
  56.  
  57.         wprintf(L"  Interface Description[%d]: %ws", i,
  58.                 interface_info->strInterfaceDescription);
  59.         wprintf(L"\n");
  60.  
  61.         wprintf(L"  Interface State[%d]:\t ", i);
  62.         switch (interface_info->isState) {
  63.         case wlan_interface_state_not_ready:
  64.             wprintf(L"Not ready\n");
  65.             break;
  66.         case wlan_interface_state_connected:
  67.             wprintf(L"Connected\n");
  68.             break;
  69.         case wlan_interface_state_ad_hoc_network_formed:
  70.             wprintf(L"First node in a ad hoc network\n");
  71.             break;
  72.         case wlan_interface_state_disconnecting:
  73.             wprintf(L"Disconnecting\n");
  74.             break;
  75.         case wlan_interface_state_disconnected:
  76.             wprintf(L"Not connected\n");
  77.             break;
  78.         case wlan_interface_state_associating:
  79.             wprintf(L"Attempting to associate with a network\n");
  80.             break;
  81.         case wlan_interface_state_discovering:
  82.             wprintf(L"Auto configuration is discovering settings for the network\n");
  83.             break;
  84.         case wlan_interface_state_authenticating:
  85.             wprintf(L"In process of authenticating\n");
  86.             break;
  87.         default:
  88.             wprintf(L"Unknown state %ld\n", interface_info->isState);
  89.             break;
  90.         }
  91.         wprintf(L"\n");
  92.     }
  93.  
  94.     if (interface_info != nullptr) {
  95.         auto guid = interface_info->InterfaceGuid;
  96.  
  97.         while (true) {
  98.             auto result = WlanScan(client_handle, &guid, nullptr, nullptr, nullptr);
  99.             if (result != ERROR_SUCCESS)  {
  100.                 wprintf(L"WlanScan failed with error: %u\n", result);
  101.                 // FormatMessage can be used to find out why the function failed
  102.                 return 1;
  103.             }
  104.  
  105.             wprintf(L"Press enter for lag spike (or 'q' to quit)...\n");
  106.             auto input = _getch();
  107.  
  108.             if (input == 'q') {
  109.                 wprintf(L"Exiting...\n");
  110.                 break;
  111.             }
  112.         }
  113.     }
  114.  
  115.     if (interface_list != nullptr) {
  116.         WlanFreeMemory(interface_list);
  117.         interface_list = nullptr;
  118.     }
  119.  
  120.     if (client_handle != nullptr) {
  121.         auto result = WlanCloseHandle(client_handle, nullptr);
  122.         if (result != ERROR_SUCCESS)  {
  123.             wprintf(L"WlanCloseHandle failed with error: %u\n", result);
  124.             // FormatMessage can be used to find out why the function failed
  125.             return 1;
  126.         }
  127.     }
  128.  
  129.     _getch();
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement