現在接続しているWi-Fiのシグナルの強度を取得するサンプルプログラムです。

プロジェクトのダウンロード

[C++コード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#pragma comment(lib, "wlanapi.lib")

#include <windows.h>
#include <wlanapi.h>
#include "resource.h"

TCHAR szClassName[] = TEXT("Window");

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  static HWND hList;
  switch (msg)
  {
  case WM_CREATE:
    hList = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("LISTBOX"), 0, WS_VISIBLE | WS_CHILD | LBS_NOINTEGRALHEIGHT, 0, 0, 0, 0, hWnd, (HMENU)IDOK, ((LPCREATESTRUCT)lParam)->hInstance, 0);
    SendMessage(hWnd, WM_APP, 0, 0);
    SetTimer(hWnd, 0x1234, 1000, 0);
    break;
  case WM_TIMER:
    KillTimer(hWnd, 0x1234);
    SendMessage(hWnd, WM_APP, 0, 0);
    SetTimer(hWnd, 0x1234, 1000, 0);
    break;
  case WM_SIZE:
    MoveWindow(hList, 10, 10, LOWORD(lParam) - 20, HIWORD(lParam) - 20, TRUE);
    break;
  case WM_APP:
    {
      SendMessage(hList, LB_RESETCONTENT, 0, 0);
      DWORD dwCurVersion = 0;
      DWORD dwResult = 0;
      WCHAR GuidString[39] = { 0 };
      HANDLE hClient = NULL;
      dwResult = WlanOpenHandle(2, NULL, &dwCurVersion, &hClient);
      if (dwResult == ERROR_SUCCESS)
      {
        PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
        dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
        if (dwResult == ERROR_SUCCESS)
        {
          for (int i = 0; i < (int)pIfList->dwNumberOfItems; ++i)
          {
            PWLAN_INTERFACE_INFO pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[i];
            StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString, sizeof(GuidString) / sizeof(*GuidString));
            if (pIfInfo->isState == wlan_interface_state_connected)
            {
              PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
              dwResult = WlanGetAvailableNetworkList(hClient, &pIfInfo->InterfaceGuid, 0, NULL, &pBssList);
              if (dwResult == ERROR_SUCCESS)
              {
                for (int j = 0; j < (int)pBssList->dwNumberOfItems; ++j)
                {
                  PWLAN_AVAILABLE_NETWORK pBssEntry = (WLAN_AVAILABLE_NETWORK *)& pBssList->Network[j];
                  if (pBssEntry->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
                  {
                    TCHAR szText[1024];
                    wsprintf(szText, TEXT("%ws, %u%%"), pBssEntry->strProfileName, pBssEntry->wlanSignalQuality);
                    SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)szText);
                  }
                }
              }
              if (pBssList != NULL)
              {
                WlanFreeMemory(pBssList);
                pBssList = NULL;
              }
            }
          }
          if (pIfList != NULL)
          {
            WlanFreeMemory(pIfList);
            pIfList = NULL;
          }
        }
        WlanCloseHandle(hClient, NULL);
      }
    }
    break;
  case WM_DESTROY:
    KillTimer(hWnd, 0x1234);
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hWnd, msg, wParam, lParam);
  }
  return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR pCmdLine, int nCmdShow)
{
  MSG msg;
  WNDCLASS wndclass = {
    CS_HREDRAW | CS_VREDRAW,
    WndProc,
    0,
    0,
    hInstance,
    LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)),
    LoadCursor(0,IDC_ARROW),
    (HBRUSH)(COLOR_WINDOW + 1),
    0,
    szClassName
  };
  RegisterClass(&wndclass);
  HWND hWnd = CreateWindow(
    szClassName,
    TEXT("接続中のWi-Fiのシグナルの強度を表示"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    0,
    CW_USEDEFAULT,
    0,
    0,
    0,
    hInstance,
    0
  );
  ShowWindow(hWnd, SW_SHOWDEFAULT);
  UpdateWindow(hWnd);
  while (GetMessage(&msg, 0, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int)msg.wParam;
}

参考