EnumDisplayDevices vs WMI Win32_DesktopMonitor,如何检测活动监视器? [英] EnumDisplayDevices vs WMI Win32_DesktopMonitor, how to detect active monitors?
问题描述
对于我目前的C ++项目,我需要检测一个唯一的字符串,每个监视器连接和活动在大量的计算机上。
For my current C++ project I need to detect a unique string for every monitor that is connected and active on a large number of computers.
研究指出了两个选项
-
使用WMI和查询所有活动监视器的Win32_DesktopMonitor。
Use WMI and query the Win32_DesktopMonitor for all active monitors. Use the PNPDeviceID for unique identification of monitors.
使用EnumDisplayDevices API,向下钻取以获取设备ID。
Use the EnumDisplayDevices API, and dig down to get the device ID.
我有兴趣使用设备ID进行唯一的型号识别,因为使用默认即插即用驱动程序的监视器会报告一个通用字符串作为监视器名称默认即插即用监视器
I'm interested in using the device id for unique model identification because monitors using the default plug and play driver will report a generic string as the monitor name "default plug and play monitor"
我遇到了WMI方法的问题,似乎只有在Vista机器上返回1个监视器,它在非WDDM设备上不能按预期工作。
I have been experiencing issues with the WMI method, it seems to be only returning 1 monitor on my Vista machine, looking at the doco it turns out it does not work as expected on non WDDM devices.
EnumDisplayDevices在从后台服务运行时(尤其是在Vista上)似乎有点问题,如果它在会话0中,它将不返回任何信息。
The EnumDisplayDevices seems to be a little problematic to get going when it runs from a background service (especially on Vista), If it's in session 0 it will return no info.
-
有没有人要做类似的事情(为所有连接的活动监视器找到唯一的模型字符串?)
Has anyone else had to do something similar (find unique model string for all connected active monitors?)
哪种方法最有效?
推荐答案
这是我当前的工作进程代码,用于可靠地检测监视设备ID。
This is my current work-in-progress code for detecting the monitor device id, reliably.
CString DeviceID;
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
DWORD dev = 0;
// device index
int id = 1;
// monitor number, as used by Display Properties > Settings
while (EnumDisplayDevices(0, dev, &dd, 0))
{
DISPLAY_DEVICE ddMon;
ZeroMemory(&ddMon, sizeof(ddMon));
ddMon.cb = sizeof(ddMon);
DWORD devMon = 0;
while (EnumDisplayDevices(dd.DeviceName, devMon, &ddMon, 0))
{
if (ddMon.StateFlags & DISPLAY_DEVICE_ACTIVE &&
!(ddMon.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
{
DeviceID.Format (L"%s", ddMon.DeviceID);
DeviceID = DeviceID.Mid (8, DeviceID.Find (L"\\", 9) - 8);
}
devMon++;
ZeroMemory(&ddMon, sizeof(ddMon));
ddMon.cb = sizeof(ddMon);
}
ZeroMemory(&dd, sizeof(dd));
dd.cb = sizeof(dd);
dev++;
}
这篇关于EnumDisplayDevices vs WMI Win32_DesktopMonitor,如何检测活动监视器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!