我正在尝试创建一个显示所有显示和监视器名称的函数。 但是第二次调用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;
}
}
答案 0 :(得分:0)
问题在于声明“private static extern bool EnumDisplayDevices()”。我将lpDevice从IntPtr更改为string,它对我有用。
下面的源代码在“输出”窗口中生成以下内容:
devNum = 0 cb = 424 DeviceID = PCI \ VEN_10DE&amp; DEV_0402&amp; SUBSYS_0440196E&amp; REV_A1 DeviceKey = \ Registry \ Machine \ System \ CurrentControlSet \ Control \ Video {B757E378-AF2E-4826-BA9E-927C5B81B139} \ 0000 DeviceName = \。\ DISPLAY1 DeviceString = NVIDIA GeForce 8600 GT 线程'RunPowerStatusMonitor Thread'(0x2108)已退出,代码为0(0x0)。 Monitor name = Generic PnP Monitor
devNum = 1 cb = 424 DeviceID = PCI \ VEN_10DE&amp; DEV_0402&amp; SUBSYS_0440196E&amp; REV_A1 DeviceKey = \ Registry \ Machine \ System \ CurrentControlSet \ Control \ Video {B757E378-AF2E-4826-BA9E-927C5B81B139} \ 0001 DeviceName = \。\ DISPLAY2 DeviceString = NVIDIA GeForce 8600 GT Monitor name = Generic PnP Monitor
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);
}
}