Repository navigation
Expand file tree
/
Copy pathMonitorWmiHelper.cs
More file actions
254 lines (230 loc) · 11.2 KB
/
Copy pathMonitorWmiHelper.cs
File metadata and controls
254 lines (230 loc) · 11.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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 using Win32_PnPEntity, WmiMonitorID, and Win32_DesktopMonitor.
/// </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);
// 1. Primary modern source: Win32_PnPEntity for PNPClass = 'Monitor'
try
{
Debug.WriteLine("Executing WMI query: Win32_PnPEntity for PNPClass = 'Monitor'...");
using var pnpSearcher = new ManagementObjectSearcher(
@"root\cimv2",
"SELECT DeviceID, Name FROM Win32_PnPEntity WHERE PNPClass = 'Monitor'");
foreach (ManagementObject mo in pnpSearcher.Get().Cast<ManagementObject>())
{
string pnpDeviceID = mo["DeviceID"]?.ToString();
string name = mo["Name"]?.ToString();
string shortId = ParsePnP_ID(pnpDeviceID);
Debug.WriteLine($"Win32_PnPEntity Result: Name='{name}', DeviceID='{pnpDeviceID}', ShortId='{shortId}'");
if (!string.IsNullOrEmpty(shortId))
{
if (!string.IsNullOrEmpty(name))
{
pnpToDesc[shortId] = name;
descToPnp[name] = shortId;
}
descToPnp[shortId] = shortId; // Self-reference for direct ID lookup
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"WMI Query Error (Win32_PnPEntity): {ex.Message}");
}
// 2. High-fidelity EDID friendly name source: root\wmi WmiMonitorID
try
{
Debug.WriteLine("Executing WMI query: root\\wmi WmiMonitorID...");
using var wmiSearcher = new ManagementObjectSearcher(
@"root\wmi",
"SELECT InstanceName, ManufacturerName, ProductCodeID, UserFriendlyName FROM WmiMonitorID");
foreach (ManagementObject mo in wmiSearcher.Get().Cast<ManagementObject>())
{
string instanceName = mo["InstanceName"]?.ToString();
string shortId = ParsePnP_ID(instanceName);
string friendlyName = string.Empty;
if (mo["UserFriendlyName"] is ushort[] uNameChars)
{
friendlyName = new string(uNameChars.Select(c => (char)c).ToArray()).Trim('\0').Trim();
}
else if (mo["UserFriendlyName"] is byte[] bNameChars)
{
friendlyName = Encoding.ASCII.GetString(bNameChars).Trim('\0').Trim();
}
if (string.IsNullOrEmpty(shortId))
{
string mfg = string.Empty;
if (mo["ManufacturerName"] is ushort[] uMfg) mfg = new string(uMfg.Select(c => (char)c).ToArray()).Trim('\0').Trim();
else if (mo["ManufacturerName"] is byte[] bMfg) mfg = Encoding.ASCII.GetString(bMfg).Trim('\0').Trim();
string prod = string.Empty;
if (mo["ProductCodeID"] is ushort[] uProd) prod = new string(uProd.Select(c => (char)c).ToArray()).Trim('\0').Trim();
else if (mo["ProductCodeID"] is byte[] bProd) prod = Encoding.ASCII.GetString(bProd).Trim('\0').Trim();
if (!string.IsNullOrEmpty(mfg) || !string.IsNullOrEmpty(prod))
{
shortId = $"{mfg}{prod}".Trim();
}
}
Debug.WriteLine($"WmiMonitorID Result: Friendly='{friendlyName}', ShortId='{shortId}'");
if (!string.IsNullOrEmpty(shortId))
{
if (!string.IsNullOrEmpty(friendlyName))
{
pnpToDesc[shortId] = friendlyName;
descToPnp[friendlyName] = shortId;
}
descToPnp[shortId] = shortId;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"WMI Query Error (WmiMonitorID): {ex.Message}");
}
// 3. Fallback: Win32_DesktopMonitor
try
{
Debug.WriteLine("Executing WMI query: Win32_DesktopMonitor...");
using var searcher = new ManagementObjectSearcher(
@"root\cimv2",
"SELECT PNPDeviceID, Name FROM Win32_DesktopMonitor");
foreach (ManagementObject mo in searcher.Get().Cast<ManagementObject>())
{
string pnpId = mo["PNPDeviceID"]?.ToString();
string name = mo["Name"]?.ToString();
string shortId = ParsePnP_ID(pnpId);
if (!string.IsNullOrEmpty(shortId))
{
if (!string.IsNullOrEmpty(name) && !pnpToDesc.ContainsKey(shortId))
{
pnpToDesc[shortId] = name;
if (!descToPnp.ContainsKey(name))
{
descToPnp[name] = shortId;
}
}
descToPnp[shortId] = shortId;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"WMI Query Error (Win32_DesktopMonitor): {ex.Message}");
}
_pnpToDescMapCache = pnpToDesc;
_descToPnpMapCache = descToPnp;
Debug.WriteLine($"Cache population complete. Total PnP mappings: {pnpToDesc.Count}");
}
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 ---
}
}
}