From 5234316b7b51a85b98474a150f33fa10051bd2ab Mon Sep 17 00:00:00 2001 From: lx-s Date: Wed, 8 Jul 2026 14:43:57 +0200 Subject: [PATCH] Keep bluetooth device names as wide char end-to-end --- WinMute/BluetoothDetector.cpp | 14 +++++++++----- WinMute/Settings_BluetoothDlg.cpp | 2 +- WinMute/WMSettings.cpp | 28 +--------------------------- WinMute/WMSettings.h | 3 +-- 4 files changed, 12 insertions(+), 35 deletions(-) diff --git a/WinMute/BluetoothDetector.cpp b/WinMute/BluetoothDetector.cpp index 8b045bc..460a53a 100644 --- a/WinMute/BluetoothDetector.cpp +++ b/WinMute/BluetoothDetector.cpp @@ -119,7 +119,7 @@ void BluetoothDetector::Unload() } void BluetoothDetector::SetDeviceList( - const std::vector& devices, + const std::vector& devices, bool useDeviceList) { deviceNames_ = devices; @@ -155,18 +155,22 @@ BluetoothDetector::BluetoothStatus BluetoothDetector::GetBluetoothStatus( } auto& log = WMLog::GetInstance(); + // BTH_DEVICE_INFO.name is UTF-8; convert it once so the comparison and + // the log output are correct for non-ASCII device names. + const std::wstring deviceName = + ConvertStringToWideString(inRangeInfo->deviceInfo.name); if (useDeviceList_) { - auto pos = std::find(begin(deviceNames_), end(deviceNames_), inRangeInfo->deviceInfo.name); + auto pos = std::find(begin(deviceNames_), end(deviceNames_), deviceName); if (pos == end(deviceNames_)) { - log.LogInfo(L"Bluetooth device \"%S\" not in list.", inRangeInfo->deviceInfo.name); + log.LogInfo(L"Bluetooth device \"%s\" not in list.", deviceName.c_str()); return BluetoothStatus::Unknown; } } if ((inRangeInfo->deviceInfo.flags & BDIF_CONNECTED) && !(inRangeInfo->previousDeviceFlags & BDIF_CONNECTED)) { - log.LogInfo(L"Bluetooth Audio device \"%S\" connected.", inRangeInfo->deviceInfo.name); + log.LogInfo(L"Bluetooth Audio device \"%s\" connected.", deviceName.c_str()); return BluetoothStatus::Connected; } else if (!(inRangeInfo->deviceInfo.flags & BDIF_CONNECTED) && (inRangeInfo->previousDeviceFlags & BDIF_CONNECTED)) { - log.LogInfo(L"Bluetooth Audio device \"%S\" disconnected.", inRangeInfo->deviceInfo.name); + log.LogInfo(L"Bluetooth Audio device \"%s\" disconnected.", deviceName.c_str()); return BluetoothStatus::Disconnected; } return BluetoothStatus::Unknown; diff --git a/WinMute/Settings_BluetoothDlg.cpp b/WinMute/Settings_BluetoothDlg.cpp index e28a505..5c6559a 100644 --- a/WinMute/Settings_BluetoothDlg.cpp +++ b/WinMute/Settings_BluetoothDlg.cpp @@ -232,7 +232,7 @@ INT_PTR CALLBACK Settings_BluetoothDlgProc(HWND hDlg, UINT msg, WPARAM wParam, L Button_Enable(GetDlgItem(hDlg, IDC_BLUETOOTH_REMOVE), FALSE); HWND hList = GetDlgItem(hDlg, IDC_BLUETOOTH_LIST); - const auto devices = settings->GetBluetoothDevicesW(); + const auto devices = settings->GetBluetoothDevices(); for (const auto& dev : devices) { ListBox_AddString(hList, dev.c_str()); } diff --git a/WinMute/WMSettings.cpp b/WinMute/WMSettings.cpp index 30daaaa..27e2223 100644 --- a/WinMute/WMSettings.cpp +++ b/WinMute/WMSettings.cpp @@ -646,33 +646,7 @@ bool WMSettings::StoreBluetoothDevices(std::vector& devices) return true; } -std::vector WMSettings::GetBluetoothDevicesA() const -{ - std::vector devices; - for (int valIdx = 0;; ++valIdx) { - char valueName[260] = {0}; - char dataBuf[260] = {0}; - DWORD valueSize = ARRAY_SIZE(valueName); - DWORD valType = 0; - DWORD dataLen = sizeof(dataBuf) - sizeof(char); - - DWORD regError = RegEnumValueA( - hBluetoothKey_, valIdx, valueName, &valueSize, nullptr, &valType, - reinterpret_cast(dataBuf), &dataLen); - if (regError == ERROR_NO_MORE_ITEMS) { - break; - } else if (regError != ERROR_SUCCESS) { - ShowWindowsError(L"RegEnumValue", regError); - return {}; - } else { - devices.push_back(dataBuf); - } - } - NormalizeStringList(devices); - return devices; -} - -std::vector WMSettings::GetBluetoothDevicesW() const +std::vector WMSettings::GetBluetoothDevices() const { std::vector devices; for (int valIdx = 0;; ++valIdx) { diff --git a/WinMute/WMSettings.h b/WinMute/WMSettings.h index be680de..82ccd85 100644 --- a/WinMute/WMSettings.h +++ b/WinMute/WMSettings.h @@ -89,8 +89,7 @@ public: std::vector GetWifiNetworks() const; bool StoreBluetoothDevices(std::vector& networks); - std::vector GetBluetoothDevicesW() const; - std::vector GetBluetoothDevicesA() const; + std::vector GetBluetoothDevices() const; bool StoreManagedAudioEndpoints(std::vector &endpoints); std::vector GetManagedAudioEndpoints() const; -- 2.43.0