From 1b99023460668b7b07e91b1a0d3960c7672a1c50 Mon Sep 17 00:00:00 2001 From: lx-s Date: Thu, 14 Apr 2022 08:58:06 +0200 Subject: [PATCH] Improved WLAN detection. --- WinMute/Settings_WifiDlg.cpp | 22 ++++++++++++++++++++++ WinMute/WifiDetector.cpp | 4 +++- WinMute/WinMute.cpp | 10 ++++++---- WinMute/WinMute.rc | 4 ++-- WinMute/resource.h | 3 ++- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/WinMute/Settings_WifiDlg.cpp b/WinMute/Settings_WifiDlg.cpp index 73b31f8..9629fe9 100644 --- a/WinMute/Settings_WifiDlg.cpp +++ b/WinMute/Settings_WifiDlg.cpp @@ -122,6 +122,17 @@ static std::vector ExportSsidListItems(HWND hList) return items; } +static bool IsWlanAvailable() +{ + HANDLE wlanHandle; + DWORD vers = 2; + if (WlanOpenHandle(vers, NULL, &vers, &wlanHandle) != ERROR_SUCCESS) { + return false; + } + WlanCloseHandle(wlanHandle, NULL); + return true; +} + INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { @@ -152,6 +163,17 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, } Button_Enable(GetDlgItem(hDlg, IDC_WIFI_REMOVEALL), ListBox_GetCount(hList) > 0); + + if (!IsWlanAvailable()) { + ShowWindow(GetDlgItem(hDlg, IDC_STATIC_WLAN_NOT_AVAILABLE), SW_SHOW); + Button_SetCheck(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE), BST_UNCHECKED); + Button_Enable(GetDlgItem(hDlg, IDC_ENABLE_WIFI_MUTE), FALSE); + Button_SetCheck(GetDlgItem(hDlg, IDC_IS_PERMITLIST), BST_UNCHECKED); + Button_Enable(GetDlgItem(hDlg, IDC_IS_PERMITLIST), FALSE); + } + else { + ShowWindow(GetDlgItem(hDlg, IDC_STATIC_WLAN_NOT_AVAILABLE), SW_HIDE); + } return TRUE; } case WM_COMMAND: diff --git a/WinMute/WifiDetector.cpp b/WinMute/WifiDetector.cpp index 448207b..44e5fbc 100644 --- a/WinMute/WifiDetector.cpp +++ b/WinMute/WifiDetector.cpp @@ -73,7 +73,9 @@ bool WifiDetector::Init(HWND hNotifyWnd) hNotifyWnd_ = hNotifyWnd; DWORD wlErr = WlanOpenHandle(vers, NULL, &vers, &wlanHandle_); if (wlErr != ERROR_SUCCESS) { - PrintWindowsError(_T("WlanOpenHandle"), wlErr); + if (wlErr != ERROR_SERVICE_NOT_ACTIVE) { + PrintWindowsError(_T("WlanOpenHandle"), wlErr); + } } else { wlErr = WlanRegisterNotification( wlanHandle_, WLAN_NOTIFICATION_SOURCE_ACM, TRUE, diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index be6ec13..5a400a6 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -333,10 +333,12 @@ bool WinMute::LoadSettings() if (muteConfig_.muteOnWlan) { if (!wifiDetector_.Init(hWnd_)) { TaskDialog(nullptr, nullptr, PROGRAM_NAME, - _T("WLAN Detection"), - _T("Failed to initialize WLAN detection. ") - _T("This feature will be unavailable for this session"), - TDCBF_OK_BUTTON, TD_ERROR_ICON, nullptr); + _T("WLAN not available"), + _T("Either the WLAN service has not been started, ") + _T("or your device is not capable of using wireless networks. ") + _T("This feature will be disabled for this computer"), + TDCBF_OK_BUTTON, TD_INFORMATION_ICON, nullptr); + settings_.SetValue(SettingsKey::MUTE_ON_WLAN, FALSE); } else { bool isMuteList = !settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST); wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(), isMuteList); diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index ebac156..db4b880 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -146,12 +146,12 @@ IDD_SETTINGS_WIFI DIALOGEX 0, 0, 242, 204 STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD FONT 8, "MS Shell Dlg", 400, 0, 0x1 BEGIN - LISTBOX IDC_WIFI_LIST,7,66,170,115,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + LISTBOX IDC_WIFI_LIST,7,66,170,110,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP PUSHBUTTON "Add",IDC_WIFI_ADD,179,65,56,16 PUSHBUTTON "Edit",IDC_WIFI_EDIT,179,84,56,16 PUSHBUTTON "Remove",IDC_WIFI_REMOVE,179,103,56,16 PUSHBUTTON "Remove all",IDC_WIFI_REMOVEALL,179,123,56,16 - LTEXT "Note: This feature is currently experimental",IDC_STATIC,7,186,171,11 + LTEXT "Note: WLAN is not available on your device, so some options have been disabled.",IDC_STATIC_WLAN_NOT_AVAILABLE,7,179,228,18 CONTROL "Enable ""WLAN Mute"".",IDC_ENABLE_WIFI_MUTE,"Button",BS_AUTOCHECKBOX | BS_MULTILINE | WS_TABSTOP,7,26,228,14 CONTROL "Mute workstation when WLAN network name is NOT in list\n(default: mute if in list).",IDC_IS_PERMITLIST, "Button",BS_AUTOCHECKBOX | BS_MULTILINE | WS_TABSTOP,7,41,228,18 diff --git a/WinMute/resource.h b/WinMute/resource.h index 0ad7729..7a59f93 100644 --- a/WinMute/resource.h +++ b/WinMute/resource.h @@ -57,6 +57,7 @@ #define IDC_WIFI_REMOVEALL 1027 #define IDC_CHECK2 1028 #define IDC_IS_PERMITLIST 1028 +#define IDC_STATIC_WLAN_NOT_AVAILABLE 1029 #define ID_TRAYMENU_INFO 40001 #define ID_TRAYMENU_ 40002 #define ID_TRAYMENU_MUTEON 40003 @@ -92,7 +93,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 131 #define _APS_NEXT_COMMAND_VALUE 40029 -#define _APS_NEXT_CONTROL_VALUE 1029 +#define _APS_NEXT_CONTROL_VALUE 1030 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif -- 2.43.0