Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.5 kB
1
Indexable
Never
#include <windows.h>
#include <wlanapi.h>
#include <iostream>

int main() {
    HANDLE hClient = NULL;
    DWORD dwMaxClient = 2;
    DWORD dwCurVersion = 0;
    DWORD dwResult = 0;

    dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
    if (dwResult != ERROR_SUCCESS) {
        std::cerr << "WlanOpenHandle failed with error: " << dwResult << std::endl;
        return 1;
    }

    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
    if (dwResult != ERROR_SUCCESS) {
        std::cerr << "WlanEnumInterfaces failed with error: " << dwResult << std::endl;
        WlanCloseHandle(hClient, NULL);
        return 1;
    }

    if (pIfList->dwNumberOfItems > 0) {
        GUID guid = pIfList->InterfaceInfo[0].InterfaceGuid;
        PWLAN_AVAILABLE_NETWORK_LIST pNetworkList = NULL;
        dwResult = WlanGetAvailableNetworkList(hClient, &guid, 0, NULL, &pNetworkList);
        if (dwResult == ERROR_SUCCESS) {
            for (DWORD i = 0; i < pNetworkList->dwNumberOfItems; i++) {
                if (pNetworkList->Network[i].bNetworkConnectable) {
                    std::wcout << L"Currently connected network: " << pNetworkList->Network[i].strProfileName << std::endl;
                    break;
                }
            }
        }

        if (pNetworkList != NULL) {
            WlanFreeMemory(pNetworkList);
        }
    }

    if (pIfList != NULL) {
        WlanFreeMemory(pIfList);
    }

    WlanCloseHandle(hClient, NULL);

    return 0;
}