EnumDisplayDevices return value is Generic PnP Monitor - c#

依然范特西╮ 提交于 2019-12-08 07:51:06

问题


I have several monitors connected, when I want to return their actual names - for example : LEN L192p, IBM 190p.

I've looked here at this question :

How do I get the actual Monitor name? as seen in the resolution dialog

but when I run it, I get wrong identification. It detects my laptop, but the other two screens (LEN L192p, IBM 190p )that I'm using are not detected and it writes Generic PnP Monitor instead.

anyone knows what can be the issue?

here is the output :

and the code:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace Screens
{
    class Program
    {


        [DllImport("user32.dll")]
        public 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 = 0x16,
            /// <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;
        }


        static void Main(string[] args)
        {

            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;
                    }
                    string x = Console.ReadLine();
                }






            catch (Exception ex)
            {
                Console.WriteLine(String.Format("{0}", ex.ToString()));
            }



}

回答1:


This happens because the screen's have no driver installed and therefore the actual device itself is running with the Generic PnP Monitor Driver. Look in Device Manager and you will see.

Im pretty sure there is no way to achieve what you are trying to achieve without install the monitor's drivers from the manufacturer.



来源:https://stackoverflow.com/questions/23407024/enumdisplaydevices-return-value-is-generic-pnp-monitor-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!