1

我在 C# 中使用 EnumDisplayDevices 时遇到问题。我使用了此处发布的代码,并且可以无缝运行。但是最近我转而在台式计算机上编写应用程序。代码不再有效,没有给我任何东西。

如果有帮助,我的显示器是在 AMD Radeon HD 5770 上运行的 LG W2753VC。

这是我当前的代码:

        var device = new DISPLAY_DEVICE();

        device.cb = Marshal.SizeOf(device);
        try {
            for (uint id = 0; EnumDisplayDevices(null, id, ref device, 0); id++) {
                device.cb = Marshal.SizeOf(device);
                EnumDisplayDevices(device.DeviceName, 0, ref device, 0);
                device.cb = Marshal.SizeOf(device);

                Console.WriteLine("id={0}, name={1}, devicestring={2}", id, device.DeviceName, device.DeviceString);
                if (device.DeviceName == null || device.DeviceName == "") continue;
            }
        } catch (Exception ex) { }

我在笔记本电脑上得到了什么:

id=0, name=\\.\DISPLAY1\Monitor0, devicestring=Generic PnP Monitor

(那应该是“Mobile PC Display”,但这不是问题所在)

我在桌面上得到了什么:

id=0, name=, devicestring=
id=1, name=, devicestring=

我做错了吗,如果没有,有没有其他方法可以让所有连接的显示器?我已经尝试过 WMI 和注册表方法。

4

3 回答 3

9

如果不发布所有 p/invoke 代码,很难说。这是适用于我的工作站的代码:

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

    [Flags()]
    public enum DisplayDeviceStateFlags : int
    {
        /// <summary>The device is part of the desktop.</summary>
        AttachedToDesktop = 0x1,
        MultiDriver = 0x2,
        /// <summary>The device is part of the desktop.</summary>
        PrimaryDevice = 0x4,
        /// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
        MirroringDriver = 0x8,
        /// <summary>The device is VGA compatible.</summary>
        VGACompatible = 0x10,
        /// <summary>The device is removable; it cannot be the primary display.</summary>
        Removable = 0x20,
        /// <summary>The device has more display modes than its output devices support.</summary>
        ModesPruned = 0x8000000,
        Remote = 0x4000000,
        Disconnect = 0x2000000
    }

    [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;
    }

    public void Display()
    {
        DISPLAY_DEVICE d = new DISPLAY_DEVICE();
        d.cb = Marshal.SizeOf(d);
        try
        {
            for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
            {
                if (d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop))
                {
                    Console.WriteLine(
                        String.Format("{0}, {1}, {2}, {3}, {4}, {5}",
                                 id,
                                 d.DeviceName,
                                 d.DeviceString,
                                 d.StateFlags,
                                 d.DeviceID,
                                 d.DeviceKey
                                 )
                                 );
                    d.cb = Marshal.SizeOf(d);
                    EnumDisplayDevices(d.DeviceName, 0, ref d, 0);
                    Console.WriteLine(
                        String.Format("{0}, {1}",
                                 d.DeviceName,
                                 d.DeviceString
                                 )
                                 );
                }
                d.cb = Marshal.SizeOf(d);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(String.Format("{0}", ex.ToString()));
        }
    }
}

上面的代码在我的机器上按照 OP 的要求工作,不知道为什么它不能在他的笔记本电脑上工作。

下面列出了获取信息的另一种方法,取自此处

foreach (var display in DisplayDetails.GetMonitorDetails())
{
    Console.WriteLine(display.Model);
}

public class DisplayDetails
{
    public string PnPID { get; set; }

    public string SerialNumber { get; set; }

    public string Model { get; set; }

    public string MonitorID { get; set; }

    /// <summary>
    /// The Constructor to create a new instances of the DisplayDetails class...
    /// </summary>
    public DisplayDetails(string sPnPID, string sSerialNumber, string sModel, string sMonitorID)
    {
        PnPID = sPnPID;
        SerialNumber = sSerialNumber;
        Model = sModel;
        MonitorID = sMonitorID;
    }

    /// <summary>
    /// This Function returns all Monitor Details
    /// </summary>
    /// <returns></returns>
    static public IEnumerable<DisplayDetails> GetMonitorDetails()
    {
        //Open the Display Reg-Key
        RegistryKey Display = Registry.LocalMachine;
        Boolean bFailed = false;
        try
        {
            Display = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\DISPLAY");
        }
        catch
        {
            bFailed = true;
        }

        if (!bFailed & (Display != null))
        {

            //Get all MonitorIDss
            foreach (string sMonitorID in Display.GetSubKeyNames())
            {
                RegistryKey MonitorID = Display.OpenSubKey(sMonitorID);

                if (MonitorID != null)
                {
                    //Get all Plug&Play ID's
                    foreach (string sPNPID in MonitorID.GetSubKeyNames())
                    {
                        RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
                        if (PnPID != null)
                        {
                            string[] sSubkeys = PnPID.GetSubKeyNames();

                            //Check if Monitor is active
                            if (sSubkeys.Contains("Control"))
                            {
                                if (sSubkeys.Contains("Device Parameters"))
                                {
                                    RegistryKey DevParam = PnPID.OpenSubKey("Device Parameters");
                                    string sSerial = "";
                                    string sModel = "";

                                    //Define Search Keys
                                    string sSerFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xff });
                                    string sModFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xfc });

                                    //Get the EDID code
                                    byte[] bObj = DevParam.GetValue("EDID", null) as byte[];
                                    if (bObj != null)
                                    {
                                        //Get the 4 Vesa descriptor blocks
                                        string[] sDescriptor = new string[4];
                                        sDescriptor[0] = Encoding.Default.GetString(bObj, 0x36, 18);
                                        sDescriptor[1] = Encoding.Default.GetString(bObj, 0x48, 18);
                                        sDescriptor[2] = Encoding.Default.GetString(bObj, 0x5A, 18);
                                        sDescriptor[3] = Encoding.Default.GetString(bObj, 0x6C, 18);

                                        //Search the Keys
                                        foreach (string sDesc in sDescriptor)
                                        {
                                            if (sDesc.Contains(sSerFind))
                                            {
                                                sSerial = sDesc.Substring(4).Replace("\0", "").Trim();
                                            }
                                            if (sDesc.Contains(sModFind))
                                            {
                                                sModel = sDesc.Substring(4).Replace("\0", "").Trim();
                                            }
                                        }


                                    }
                                    if (!string.IsNullOrEmpty(sPNPID + sSerFind + sModel + sMonitorID))
                                    {
                                        yield return new DisplayDetails(sPNPID, sSerial, sModel, sMonitorID);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
于 2013-09-16T17:39:34.670 回答
2

device.cb当不等于 840 或 224 (sizeof DISPLAY_DEVICEWDISPLAY_DEVICEA分别)之一时,我完全有这种行为。就我而言,我只是在您的两台计算机上错误地写了device.cb = sizeof(device.cb); Check value of 。Marshal.SizeOf(device)

于 2015-08-20T13:37:03.517 回答
0

来自的代码示例GalacticJello对我有用。我强制为 x86 构建程序集。

在其他情况下,可以尝试: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct DISPLAY_DEVICE`

但这不适用于 x86!

于 2017-01-19T07:33:28.180 回答