我正在尝试制作一个显示所有显示器和监视器名称的函数。但是我第二次调用 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;
}
}