diff options
| author | Jordan Woyak <jordan.woyak@gmail.com> | 2025-10-02 04:11:27 -0500 |
|---|---|---|
| committer | Jordan Woyak <jordan.woyak@gmail.com> | 2025-10-12 12:33:13 -0500 |
| commit | 3978e1eb2cb0a25ff5c9d2a6bf47999f8843330d (patch) | |
| tree | ded7ae807ed65dd1756efe7bce751dca1ec7b242 /Source/Core/Common/WindowsDevice.cpp | |
| parent | e8d22923c66f6a6a580b5095bffa7768183f40cb (diff) | |
WindowsDevice: Add some utility functions for getting device properties using CfgMgr32.
Diffstat (limited to 'Source/Core/Common/WindowsDevice.cpp')
| -rw-r--r-- | Source/Core/Common/WindowsDevice.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Source/Core/Common/WindowsDevice.cpp b/Source/Core/Common/WindowsDevice.cpp index 8ecc346187..770aa3861a 100644 --- a/Source/Core/Common/WindowsDevice.cpp +++ b/Source/Core/Common/WindowsDevice.cpp @@ -7,7 +7,10 @@ #include <string> +#include "Hidclass.h" + #include "Common/CommonFuncs.h" +#include "Common/Logging/Log.h" namespace Common { @@ -33,6 +36,52 @@ std::wstring GetDeviceProperty(const HDEVINFO& device_info, const PSP_DEVINFO_DA return std::wstring(unicode_buffer.data()); } + +std::optional<std::wstring> GetPropertyHelper(auto function, auto dev, + const DEVPROPKEY* requested_property, + DEVPROPTYPE expected_type) +{ + DEVPROPTYPE type{}; + ULONG buffer_size{}; + + if (const auto result = function(dev, requested_property, &type, nullptr, &buffer_size, 0); + result != CR_SUCCESS && result != CR_BUFFER_SMALL) + { + WARN_LOG_FMT(COMMON, "CM_Get_DevNode_Property returned: {}", result); + return std::nullopt; + } + if (type != expected_type) + { + WARN_LOG_FMT(COMMON, "CM_Get_DevNode_Property unexpected type: 0x{:x}", type); + return std::nullopt; + } + + std::optional<std::wstring> property; + // FYI: It's legal to write the null terminator at data()[size()] of std::basic_string. + property.emplace(buffer_size / sizeof(WCHAR) - 1, L'\0'); + if (const auto result = function(dev, requested_property, &type, + reinterpret_cast<BYTE*>(property->data()), &buffer_size, 0); + result != CR_SUCCESS) + { + ERROR_LOG_FMT(COMMON, "CM_Get_DevNode_Property returned: {}", result); + return std::nullopt; + } + return property; +} + +std::optional<std::wstring> GetDevNodeStringProperty(DEVINST dev, + const DEVPROPKEY* requested_property) +{ + return GetPropertyHelper(CM_Get_DevNode_Property, dev, requested_property, DEVPROP_TYPE_STRING); +} + +std::optional<std::wstring> GetDeviceInterfaceStringProperty(LPCWSTR iface, + const DEVPROPKEY* requested_property) +{ + return GetPropertyHelper(CM_Get_Device_Interface_Property, iface, requested_property, + DEVPROP_TYPE_STRING); +} + } // namespace Common #endif |
