通过Win32_DesktopMonitor检测连接的监视器的串行

时间:2015-09-17 19:10:07

标签: c# multiple-monitors

我一直在努力解决问题。

我试图获取所有连接的显示器的序列号。

我已经尝试过Win32_DesktopMonitor甚至CIM_DesktopMonitor都没有成功。

例如,我已经连接并运行了10台电视(向上),但这些电话只返回其中的4台。 (见下面的截图)

See screenshot

我最好的猜测是关于WDDM但是如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试其他方法,例如原生EnumDisplayDevices winapi函数。示例代码:

public class Program {
    private static void Main(string[] args) {
        var dd = new DISPLAY_DEVICE();
        dd.cb = Marshal.SizeOf(dd);
        uint devNum = 0;
        while (EnumDisplayDevices(null, devNum, ref dd, 0)) {
            uint devMon = 0;
            var ddMon = new DISPLAY_DEVICE();
            ddMon.cb = Marshal.SizeOf(ddMon);
            while (EnumDisplayDevices(dd.DeviceName, devMon, ref ddMon, 0)) {
                Console.WriteLine(ddMon.DeviceName);
                Console.WriteLine(ddMon.DeviceID);
                Console.WriteLine(ddMon.DeviceKey);
                Console.WriteLine(ddMon.DeviceString);
                Console.WriteLine();
                devMon++;
                ddMon.cb = Marshal.SizeOf(ddMon);
            }
            devNum++;
            dd.cb = Marshal.SizeOf(dd);
        }
        Console.ReadKey();
    }

    [DllImport("user32.dll")]
    public static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct DISPLAY_DEVICE {
        [MarshalAs(UnmanagedType.U4)] public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString;
        [MarshalAs(UnmanagedType.U4)] public DisplayDeviceStateFlags StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey;
    }

    [Flags()]
    public enum DisplayDeviceStateFlags : int {
        AttachedToDesktop = 0x1,
        MultiDriver = 0x2,
        PrimaryDevice = 0x4,
        MirroringDriver = 0x8,
        VGACompatible = 0x16,
        Removable = 0x20,
        ModesPruned = 0x8000000,
        Remote = 0x4000000,
        Disconnect = 0x2000000
    }
}

如果您对如何使用这种奇怪的方式感兴趣,请阅读上面链接中该功能的文档。