Improve WMI monitor name extraction using length property

Refactor GetUserFriendlyName to use UserFriendlyNameLength for accurate string extraction from the WMI UserFriendlyName buffer. This ensures only valid characters are included, improving reliability when parsing monitor names.
This commit is contained in:
Yu Leng
2025-12-10 10:15:40 +08:00
parent 095ae2bebd
commit db15380fcf

View File

@@ -405,20 +405,22 @@ namespace PowerDisplay.Common.Drivers.WMI
}
/// <summary>
/// Get user-friendly name from WMI object
/// Get user-friendly name from WMI object.
/// WmiMonitorID returns UserFriendlyName as a fixed-size uint16 array buffer,
/// with UserFriendlyNameLength indicating the actual character count.
/// </summary>
private static string? GetUserFriendlyName(WmiObject monitorObject)
{
try
{
// WmiLight returns arrays as object arrays
var userFriendlyNameObj = monitorObject.GetPropertyValue<object>("UserFriendlyName");
var userFriendlyName = monitorObject.GetPropertyValue<ushort[]>("UserFriendlyName");
var nameLength = monitorObject.GetPropertyValue<ushort>("UserFriendlyNameLength");
if (userFriendlyNameObj is ushort[] userFriendlyName && userFriendlyName.Length > 0)
if (userFriendlyName != null && nameLength > 0 && nameLength <= userFriendlyName.Length)
{
// Convert UINT16 array to string
// Use UserFriendlyNameLength to extract only valid characters
var chars = userFriendlyName
.Where(c => c != 0)
.Take(nameLength)
.Select(c => (char)c)
.ToArray();
@@ -430,7 +432,6 @@ namespace PowerDisplay.Common.Drivers.WMI
}
catch (Exception ex)
{
// Ignore conversion errors
Logger.LogDebug($"Failed to parse UserFriendlyName: {ex.Message}");
}