USBUtils: Replace GetDeviceNameUsingSetupAPI with GetDeviceNameUsingCfgMgr32. SetupAPI seems to be no longer recommended.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "Core/USBUtils.h"
|
#include "Core/USBUtils.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@@ -21,9 +22,11 @@
|
|||||||
#include <libusb.h>
|
#include <libusb.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <SetupAPI.h>
|
|
||||||
#include <cfgmgr32.h>
|
#include <cfgmgr32.h>
|
||||||
#include <devpkey.h>
|
#include <devpkey.h>
|
||||||
|
#include <initguid.h>
|
||||||
|
// initguid.h must be included before usbiodef.h
|
||||||
|
#include <usbiodef.h>
|
||||||
|
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
#include "Common/WindowsDevice.h"
|
#include "Common/WindowsDevice.h"
|
||||||
@@ -119,43 +122,36 @@ static std::optional<std::string> GetDeviceNameUsingKnownDevices(u16 vid, u16 pi
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static std::optional<std::string> GetDeviceNameUsingSetupAPI(u16 vid, u16 pid)
|
static std::optional<std::string> GetDeviceNameUsingCfgMgr32(u16 vid, u16 pid)
|
||||||
{
|
{
|
||||||
std::optional<std::string> device_name;
|
auto class_guid = GUID_DEVINTERFACE_USB_DEVICE;
|
||||||
|
const ULONG flags = CM_GET_DEVICE_INTERFACE_LIST_PRESENT;
|
||||||
const std::wstring filter = fmt::format(L"VID_{:04X}&PID_{:04X}", vid, pid);
|
const std::wstring filter = fmt::format(L"VID_{:04X}&PID_{:04X}", vid, pid);
|
||||||
|
|
||||||
HDEVINFO dev_info =
|
for (auto* iface : Common::GetDeviceInterfaceList(&class_guid, nullptr, flags))
|
||||||
SetupDiGetClassDevs(nullptr, nullptr, nullptr, DIGCF_PRESENT | DIGCF_ALLCLASSES);
|
|
||||||
if (dev_info == INVALID_HANDLE_VALUE)
|
|
||||||
return std::nullopt;
|
|
||||||
SP_DEVINFO_DATA dev_info_data;
|
|
||||||
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
||||||
|
|
||||||
for (DWORD i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); ++i)
|
|
||||||
{
|
{
|
||||||
TCHAR instance_id[MAX_DEVICE_ID_LEN];
|
if (!std::basic_string_view{iface}.contains(filter))
|
||||||
if (CM_Get_Device_ID(dev_info_data.DevInst, instance_id, MAX_DEVICE_ID_LEN, 0) != CR_SUCCESS)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const std::wstring_view id_wstr(instance_id);
|
auto dev_inst_id = Common::GetDeviceInterfaceStringProperty(iface, &DEVPKEY_Device_InstanceId);
|
||||||
if (id_wstr.find(filter) == std::wstring::npos)
|
if (!dev_inst_id.has_value())
|
||||||
continue;
|
return std::nullopt;
|
||||||
|
|
||||||
std::wstring property_value =
|
DEVINST dev_inst{};
|
||||||
Common::GetDeviceProperty(dev_info, &dev_info_data, &DEVPKEY_Device_FriendlyName);
|
if (CM_Locate_DevNode(&dev_inst, dev_inst_id->data(), CM_LOCATE_DEVNODE_NORMAL) != CR_SUCCESS)
|
||||||
if (property_value.empty())
|
|
||||||
{
|
{
|
||||||
property_value =
|
ERROR_LOG_FMT(COMMON, "CM_Locate_DevNode");
|
||||||
Common::GetDeviceProperty(dev_info, &dev_info_data, &DEVPKEY_Device_DeviceDesc);
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!property_value.empty())
|
// FYI: BusReportedDeviceDesc seems to be more of a friendly name
|
||||||
device_name = WStringToUTF8(property_value);
|
// while DeviceDesc tends to be something more like "USB Input Device".
|
||||||
break;
|
return Common::GetDevNodeStringProperty(dev_inst, &DEVPKEY_Device_BusReportedDeviceDesc)
|
||||||
|
.or_else(std::bind(Common::GetDevNodeStringProperty, dev_inst, &DEVPKEY_Device_DeviceDesc))
|
||||||
|
.transform(WStringToUTF8);
|
||||||
}
|
}
|
||||||
|
|
||||||
SetupDiDestroyDeviceInfoList(dev_info);
|
return std::nullopt;
|
||||||
return device_name;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -245,7 +241,7 @@ std::optional<std::string> GetDeviceNameFromVIDPID(u16 vid, u16 pid)
|
|||||||
&GetDeviceNameUsingHWDB,
|
&GetDeviceNameUsingHWDB,
|
||||||
#endif
|
#endif
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
&GetDeviceNameUsingSetupAPI,
|
&GetDeviceNameUsingCfgMgr32,
|
||||||
#endif
|
#endif
|
||||||
#if defined(__LIBUSB__) && !defined(_WIN32)
|
#if defined(__LIBUSB__) && !defined(_WIN32)
|
||||||
&GetDeviceNameUsingLibUSB,
|
&GetDeviceNameUsingLibUSB,
|
||||||
|
|||||||
Reference in New Issue
Block a user