-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorWmiHelper.cs
More file actions
172 lines (159 loc) · 7.04 KB
/
MonitorWmiHelper.cs
File metadata and controls
172 lines (159 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;
namespace MultiDisplayVCPServer
{
/// <summary>
/// A static helper class for querying WMI to get monitor hardware IDs.
/// This class bridges the gap between the DDC/CI monitor name and the stable PnP (Plug and Play) ID.
/// </summary>
public static class MonitorWmiHelper
{
private static Dictionary<string, string> _pnpToDescMapCache = null;
private static Dictionary<string, string> _descToPnpMapCache = null;
private static readonly object _wmiLock = new object();
/// <summary>
/// Parses a full PnP Device ID string to extract the short Model ID.
/// </summary>
/// <param name="fullId">The full PnP ID (e.g., "DISPLAY\ACR0D1D\4&...").</param>
/// <returns>The short Model ID (e.g., "ACR0D1D"), or null if parsing fails.</returns>
private static string ParsePnP_ID(string fullId)
{
Debug.WriteLine($"ParsePnP_ID() called with: {fullId}");
if (string.IsNullOrEmpty(fullId))
{
Debug.WriteLine("ParsePnP_ID: fullId is null or empty, returning null.");
return null;
}
string[] parts = fullId.Split('\\');
if (parts.Length > 1)
{
Debug.WriteLine($"ParsePnP_ID: Found {parts.Length} parts. Returning part 1: {parts[1]}");
return parts[1]; // The second part is the Model ID
}
Debug.WriteLine("ParsePnP_ID: fullId did not contain '\\', returning null.");
return null;
}
public static void ClearWmiCache()
{
Debug.WriteLine("ClearWmiCache() started.");
lock (_wmiLock)
{
Debug.WriteLine("WMI lock acquired.");
_pnpToDescMapCache = null;
_descToPnpMapCache = null;
Debug.WriteLine("WMI cache cleared (_pnpToDescMapCache = null, _descToPnpMapCache = null).");
}
Debug.WriteLine("WMI lock released. ClearWmiCache() finished.");
}
/// <summary>
/// Populates *both* WMI maps at the same time.
/// </summary>
private static void PopulateWmiCaches()
{
Debug.WriteLine("PopulateWmiCaches() started.");
// Double-check lock to ensure only one thread populates
if (_pnpToDescMapCache != null)
{
Debug.WriteLine("Cache is already populated. Skipping.");
return;
}
Debug.WriteLine("Cache is null, acquiring WMI lock...");
lock (_wmiLock)
{
Debug.WriteLine("WMI lock acquired.");
// Check again inside the lock
if (_pnpToDescMapCache != null)
{
Debug.WriteLine("Cache was populated by another thread. Skipping.");
Debug.WriteLine("WMI lock released.");
return;
}
Debug.WriteLine("Initializing new cache dictionaries.");
var pnpToDesc = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var descToPnp = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
try
{
Debug.WriteLine("Executing WMI query: 'SELECT * FROM Win32_DesktopMonitor'...");
ManagementObjectSearcher searcher = new(
@"root\cimv2",
"SELECT * FROM Win32_DesktopMonitor");
foreach (ManagementObject mo in searcher.Get().Cast<ManagementObject>())
{
string pnpId = mo["PNPDeviceID"]?.ToString();
string name = mo["Name"]?.ToString();
Debug.WriteLine($"WMI Result: Name='{name}', PnPDeviceID='{pnpId}'");
string shortId = ParsePnP_ID(pnpId);
if (!string.IsNullOrEmpty(shortId) && !string.IsNullOrEmpty(name))
{
if (!pnpToDesc.ContainsKey(shortId))
{
pnpToDesc[shortId] = name;
Debug.WriteLine($"Added to pnpToDesc map: [{shortId}] = {name}");
}
if (!descToPnp.ContainsKey(name))
{
descToPnp[name] = shortId;
Debug.WriteLine($"Added to descToPnp map: [{name}] = {shortId}");
}
}
else
{
Debug.WriteLine("Skipping WMI result (missing name or shortId).");
}
}
Debug.WriteLine("WMI query finished.");
}
catch (Exception ex)
{
Debug.WriteLine($"WMI Query Error (PopulateWmiCaches): {ex.Message}");
}
_pnpToDescMapCache = pnpToDesc;
_descToPnpMapCache = descToPnp;
Debug.WriteLine("Cache population complete. Assigning new dictionaries to static cache fields.");
}
Debug.WriteLine("WMI lock released. PopulateWmiCaches() finished.");
}
/// <summary>
/// Gets a map of [DDC/CI Description] -> [Short Model ID].
/// </summary>
public static Dictionary<string, string> GetMonitorPnPMap()
{
Debug.WriteLine("GetMonitorPnPMap() called.");
// --- MODIFIED: Use the cache ---
if (_descToPnpMapCache == null)
{
Debug.WriteLine("Cache miss. Calling PopulateWmiCaches().");
PopulateWmiCaches();
}
else
{
Debug.WriteLine("Cache hit. Returning existing _descToPnpMapCache.");
}
return _descToPnpMapCache;
// --- END MODIFIED ---
}
/// <summary>
/// Gets a map of [Short Model ID] -> [DDC/CI Description].
/// </summary>
public static Dictionary<string, string> GetPnPMonitorMap()
{
Debug.WriteLine("GetPnPMonitorMap() called.");
// --- MODIFIED: Use the cache ---
if (_pnpToDescMapCache == null)
{
Debug.WriteLine("Cache miss. Calling PopulateWmiCaches().");
PopulateWmiCaches();
}
else
{
Debug.WriteLine("Cache hit. Returning existing _pnpToDescMapCache.");
}
return _pnpToDescMapCache;
// --- END MODIFIED ---
}
}
}