diff --git a/WLANManager/Main.cpp b/WLANManager/Main.cpp index c978fd2..02ad1d3 100644 --- a/WLANManager/Main.cpp +++ b/WLANManager/Main.cpp @@ -60,6 +60,10 @@ static int WLANManager() return -1; } + wstring interface_name; + if (nArgs >= 5 && _wcsicmp(pwcArglist[3], L"interface") == 0) + interface_name = pwcArglist[4]; + // Open WLAN handle. DWORD dwNegotiatedVersion; wlan_handle wlan; @@ -83,20 +87,54 @@ static int WLANManager() interfaces.reset(pInterfaceList); } + tstring_guid devclass_net(GUID_DEVCLASS_NET); + bool profile_found = false; for (DWORD i = 0; i < interfaces->dwNumberOfItems; i++) { if (interfaces->InterfaceInfo[i].isState == wlan_interface_state_not_ready) { // This interface is not ready. continue; } + if (!interface_name.empty()) { + // Read the interface name from registry. + reg_key key; + if (key.open(HKEY_LOCAL_MACHINE, tstring_printf(_T("SYSTEM\\CurrentControlSet\\Control\\Network\\%s\\%s\\Connection"), devclass_net.c_str(), tstring_guid(interfaces->InterfaceInfo[i].InterfaceGuid).c_str()).c_str(), 0, KEY_READ)) { + wstring name; + if (RegQueryStringValue(key, _T("Name"), name) == ERROR_SUCCESS && _wcsicmp(interface_name.c_str(), name.c_str()) != 0) { + // Not the interface we are interested in. + continue; + } + } + } + + unique_ptr > profiles; + { + // Get a list of profiles. + WLAN_PROFILE_INFO_LIST *pProfileList; + DWORD dwResult = WlanGetProfileList(wlan, &(interfaces->InterfaceInfo[i].InterfaceGuid), NULL, &pProfileList); + if (dwResult != ERROR_SUCCESS) { + DisplayError(_T("%s function failed (error %u)."), _T("WlanGetProfileList"), dwResult); + return 4; + } + profiles.reset(pProfileList); + } + + for (DWORD j = 0; j < profiles->dwNumberOfItems; j++) + if (_wcsicmp(profiles->ProfileInfo[j].strProfileName, pwcArglist[2]) == 0) { + profile_found = true; + break; + } + + if (!profile_found) + continue; + // Launch WLAN profile config dialog. - WLAN_REASON_CODE wlrc; + WLAN_REASON_CODE wlrc = L2_REASON_CODE_SUCCESS; DWORD dwResult = WlanUIEditProfile(WLAN_UI_API_VERSION, pwcArglist[2], &(interfaces->InterfaceInfo[i].InterfaceGuid), NULL, WLSecurityPage, NULL, &wlrc); if (dwResult != ERROR_SUCCESS) { DisplayError(_T("%s function failed (error %u)."), _T("WlanUIEditProfile"), dwResult); return 5; - } - if (wlrc != WLAN_REASON_CODE_SUCCESS) { + } else if (wlrc != WLAN_REASON_CODE_SUCCESS) { tstring reason; if (WlanReasonCodeToString(wlrc, reason, NULL) == ERROR_SUCCESS) DisplayError(_T("%s function failed: %s"), _T("WlanUIEditProfile"), reason.c_str()); @@ -107,6 +145,9 @@ static int WLANManager() break; } + if (!profile_found) + DisplayError(_T("%ls profile not found."), pwcArglist[2]); + return 0; } diff --git a/WLANManager/README.md b/WLANManager/README.md index 1d74770..dd9bf9d 100644 --- a/WLANManager/README.md +++ b/WLANManager/README.md @@ -3,7 +3,7 @@ Invokes standard Windows Wireless Network Properties dialog ##Usage ``` -WLANManager profile +WLANManager profile [interface ] ``` - `name` - The name of the network profile (not neccessarely the same as SSID) diff --git a/WLANManager/StdAfx.h b/WLANManager/StdAfx.h index 92aeb9e..bff2585 100644 --- a/WLANManager/StdAfx.h +++ b/WLANManager/StdAfx.h @@ -28,6 +28,7 @@ #include #include +#include #include #include