获取 Windows 显示器的唯一标识符 [英] Get unique identifier for Windows monitors

查看:60
本文介绍了获取 Windows 显示器的唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置有两个常规显示器和三个连接到 Windows 电脑的投影仪.在我的 win32 程序中我需要唯一标识每个显示器并为每个显示器存储信息,以便即使在计算机重新启动后我也可以检索存储的信息.

I have a setup with two regular displays and three projectors connected to a windows pc. In my win32 program I need to uniquely identify each monitor and store information for each such that I can retrieve the stored information even after computer restart.

EnumDisplayDevices 似乎在重新启动计算机后返回不同的设备顺序.还有 GetPhysicalMonitorsFromHMONITOR 至少给了我显示器的名称.但是,我的投影机需要序列号之类的东西,因为它们是相同的型号.我怎样才能获得这样一个唯一标识符?

The EnumDisplayDevices seems to return different device orders after restarting the computer. There is also GetPhysicalMonitorsFromHMONITOR which at least gives me the display's name. However, I need something like a serial number for my projectors, since they are the same model. How can I get such a unique identifier?

这是我在阅读用户 Anders 的回答后想出的解决方案(谢谢!):

This is the solution I came up with after reading the answer from user Anders (thanks!):

DISPLAY_DEVICEA dispDevice;
ZeroMemory(&dispDevice, sizeof(dispDevice));
dispDevice.cb = sizeof(dispDevice);

DWORD screenID;
while (EnumDisplayDevicesA(NULL, screenID, &dispDevice, 0))
{
    // important: make copy of DeviceName
    char name[sizeof(dispDevice.DeviceName)];
    strcpy(name, dispDevice.DeviceName);

    if (EnumDisplayDevicesA(name, 0, &dispDevice, EDD_GET_DEVICE_INTERFACE_NAME))
    {
        // at this point dispDevice.DeviceID contains a unique identifier for the monitor
    }

    ++screenID;
}

推荐答案

EnumDisplayDevices 带有 EDD_GET_DEVICE_INTERFACE_NAME 标志应该给你一个可用的字符串.如果没有,您可以将此字符串与 SetupAPI 一起使用,以获取硬件 ID 或驱动程序密钥或任何足以满足您目的的唯一内容.

EnumDisplayDevices with the EDD_GET_DEVICE_INTERFACE_NAME flag should give you a usable string. And if not, you can use this string with the SetupAPI to get the hardware id or driver key or whatever is unique enough for your purpose.

将此标志设置为 EDD_GET_DEVICE_INTERFACE_NAME (0x00000001) 以检索 GUID_DEVINTERFACE_MONITOR 的设备接口名称,该名称由操作系统在每个监视器的基础上注册.该值位于 lpDisplayDevice 中返回的 DISPLAY_DEVICE 结构的 DeviceID 成员中.生成的设备接口名称可与 SetupAPI 函数一起使用,并用作 GDI 监视器设备和 SetupAPI 监视器设备之间的链接.

Set this flag to EDD_GET_DEVICE_INTERFACE_NAME (0x00000001) to retrieve the device interface name for GUID_DEVINTERFACE_MONITOR, which is registered by the operating system on a per monitor basis. The value is placed in the DeviceID member of the DISPLAY_DEVICE structure returned in lpDisplayDevice. The resulting device interface name can be used with SetupAPI functions and serves as a link between GDI monitor devices and SetupAPI monitor devices.

这篇关于获取 Windows 显示器的唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆