EnumDisplayDevices return value is Generic PnP Monitor - c#
Asked Answered
S

1

1

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 : enter image description here

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()));
            }



}
Sienese answered 1/5, 2014 at 11:46 Comment(0)
S
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.

Superannuation answered 1/5, 2014 at 11:59 Comment(3)
I looked now at the device manager and saw that as well. but If I look at the screen resolution (in the control panel) I can see these names. Is there any way to get these names?Sienese
@Sienese I don't think so, at least I can find it.Superannuation
Some have luck with https://mcmap.net/q/672247/-how-do-i-get-the-number-of-displays-in-windowsGrayce

© 2022 - 2024 — McMap. All rights reserved.