C#如何获取Windows监视器名称

我试图做一个显示所有显示和监视器名称的函数。 但是第二次我调用enumDisplayDevices时,如果第一个参数是lpDisplayDevice.DeviceName,则会出现错误。 我知道这是第一个参数,如果它被设置为“IntPtr.Zero”,而不是错误。

我找不到如何在C#中执行此操作的另一个示例。

public static void show_display_devices() { DISPLAY_DEVICE lpDisplayDevice = new DISPLAY_DEVICE(0); // OUT int devNum = 0; while(true) { bool success = EnumDisplayDevices( IntPtr.Zero, devNum, ref lpDisplayDevice, 0); if( !success ) break; Console.WriteLine("\ndevNum =" + devNum); Console.WriteLine("cb =" + lpDisplayDevice.cb); Console.WriteLine("DeviceID =" + lpDisplayDevice.DeviceID); Console.WriteLine("DeviceKey =" + lpDisplayDevice.DeviceKey); Console.WriteLine("DeviceName =" + lpDisplayDevice.DeviceName.Trim()); Console.WriteLine("DeviceString =" + lpDisplayDevice.DeviceString.Trim()); // Show monitor name: DISPLAY_DEVICE monitor_name = new DISPLAY_DEVICE(0); // OUT >>>>>>>>>>> NEXT LINE HAS ERROR: <<<<<<<<<<<<<<< EnumDisplayDevices(lpDisplayDevice.DeviceName, 0, ref monitor_name, 0); <<<<<<<<<<<<<<<<< "lpDisplayDevice.DeviceName" causes error <<<<<< ^^^^^^^^^^^^^^^^^^^^^^^^^^ - CAUSES ERROR <<<<<< Console.WriteLine("Monitor name =" + lpDisplayDevice.DeviceString.Trim()); ++devNum; } } 

Solutions Collecting From Web of "C#如何获取Windows监视器名称"

问题是在声明“私人静态外部BOOL EnumDisplayDevices()”。 我把IntPtr中的lpDevice改成了字符串,对我来说很有用。

以下源代码在“输出”窗口中生成以下内容:

devNum = 0 cb = 424 DeviceID = PCI \ VEN_10DE&DEV_0402&SUBSYS_0440196E&REV_A1 DeviceKey = \ Registry \ Machine \ System \ CurrentControlSet \ Control \ Video {B757E378-AF2E-4826-BA9E-927C5B81B139} \ 0000 DeviceName = \。\ DISPLAY1 DeviceString = NVIDIA GeForce 8600 GT线程'RunPowerStatusMonitor线程'(0x2108)退出代码0(0x0)。 监视器名称=通用即插即用监视器

devNum = 1 cb = 424 DeviceID = PCI \ VEN_10DE&DEV_0402&SUBSYS_0440196E&REV_A1 DeviceKey = \ Registry \ Machine \ System \ CurrentControlSet \ Control \ Video {B757E378-AF2E-4826-BA9E-927C5B81B139} \ 0001 DeviceName = \。\ DISPLAY2 DeviceString = NVIDIA GeForce 8600 GT监视器名称=通用即插即用监视器

  public static void show_display_devices() { DISPLAY_DEVICE lpDisplayDevice = new DISPLAY_DEVICE(0); // OUT DISPLAY_DEVICE monitor_name = new DISPLAY_DEVICE(0); // OUT int devNum = 0; while (EnumDisplayDevices(null, devNum, ref lpDisplayDevice, 0)) { Console.WriteLine("\ndevNum =" + devNum); Console.WriteLine("cb =" + lpDisplayDevice.cb); Console.WriteLine("DeviceID =" + lpDisplayDevice.DeviceID); Console.WriteLine("DeviceKey =" + lpDisplayDevice.DeviceKey); Console.WriteLine("DeviceName =" + lpDisplayDevice.DeviceName.Trim()); Console.WriteLine("DeviceString =" + lpDisplayDevice.DeviceString.Trim()); // Show monitor name: EnumDisplayDevices( lpDisplayDevice.DeviceName, 0, ref monitor_name, 0); Console.WriteLine("Monitor name =" + monitor_name.DeviceString.Trim()); ++devNum; } } [DllImport("User32.dll")] private static extern bool EnumDisplayDevices( string lpDevice, int iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags); [StructLayout(LayoutKind.Sequential)] public struct DISPLAY_DEVICE { public int cb; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString; public int StateFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceID; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey; public DISPLAY_DEVICE(int flags) { cb = 0; StateFlags = flags; DeviceName = new string((char)32, 32); DeviceString = new string((char)32, 128); DeviceID = new string((char)32, 128); DeviceKey = new string((char)32, 128); cb = Marshal.SizeOf(this); } }