枚举显示设备不返回任何内容
本文关键字:任何内 返回 显示设备 枚举 | 更新日期: 2023-09-27 18:31:33
我在使用 C# 中使用 EnumDisplayDevices 时遇到问题。我使用了这里发布的代码,它无缝运行。但是最近我切换到在台式计算机上编写应用程序。代码不再有效,没有给我任何东西。
如果有帮助,我的显示器是运行在AMD Radeon HD 5770上的LG W2753VC。
这是我当前的代码:
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;
}
} catch (Exception ex) { }
我在笔记本电脑上得到什么:
id=0, name=''.'DISPLAY1'Monitor0, devicestring=Generic PnP Monitor
(那应该是"移动PC显示器",但这不是这里的问题)
我在桌面上得到什么:
id=0, name=, devicestring=
id=1, name=, devicestring=
我是否做错了,如果没有,有没有其他方法可以获取所有连接的显示器?我已经尝试了 WMI 和注册表方法。
如果不发布所有 p/调用代码,很难分辨。 以下是适用于我的工作站的代码:
public class EnumDisplayDevicesTest
{
[DllImport("user32.dll")]
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 = 0x10,
/// <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;
}
public void Display()
{
DISPLAY_DEVICE d = new DISPLAY_DEVICE();
d.cb = Marshal.SizeOf(d);
try
{
for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
{
if (d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop))
{
Console.WriteLine(
String.Format("{0}, {1}, {2}, {3}, {4}, {5}",
id,
d.DeviceName,
d.DeviceString,
d.StateFlags,
d.DeviceID,
d.DeviceKey
)
);
d.cb = Marshal.SizeOf(d);
EnumDisplayDevices(d.DeviceName, 0, ref d, 0);
Console.WriteLine(
String.Format("{0}, {1}",
d.DeviceName,
d.DeviceString
)
);
}
d.cb = Marshal.SizeOf(d);
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("{0}", ex.ToString()));
}
}
}
上面的代码在我的机器上按照 OP 想要的运行,不知道为什么它不能在他的笔记本电脑上工作。
下面列出了获取信息的另一种方法,从这里获取:
foreach (var display in DisplayDetails.GetMonitorDetails())
{
Console.WriteLine(display.Model);
}
public class DisplayDetails
{
public string PnPID { get; set; }
public string SerialNumber { get; set; }
public string Model { get; set; }
public string MonitorID { get; set; }
/// <summary>
/// The Constructor to create a new instances of the DisplayDetails class...
/// </summary>
public DisplayDetails(string sPnPID, string sSerialNumber, string sModel, string sMonitorID)
{
PnPID = sPnPID;
SerialNumber = sSerialNumber;
Model = sModel;
MonitorID = sMonitorID;
}
/// <summary>
/// This Function returns all Monitor Details
/// </summary>
/// <returns></returns>
static public IEnumerable<DisplayDetails> GetMonitorDetails()
{
//Open the Display Reg-Key
RegistryKey Display = Registry.LocalMachine;
Boolean bFailed = false;
try
{
Display = Registry.LocalMachine.OpenSubKey(@"SYSTEM'CurrentControlSet'Enum'DISPLAY");
}
catch
{
bFailed = true;
}
if (!bFailed & (Display != null))
{
//Get all MonitorIDss
foreach (string sMonitorID in Display.GetSubKeyNames())
{
RegistryKey MonitorID = Display.OpenSubKey(sMonitorID);
if (MonitorID != null)
{
//Get all Plug&Play ID's
foreach (string sPNPID in MonitorID.GetSubKeyNames())
{
RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
if (PnPID != null)
{
string[] sSubkeys = PnPID.GetSubKeyNames();
//Check if Monitor is active
if (sSubkeys.Contains("Control"))
{
if (sSubkeys.Contains("Device Parameters"))
{
RegistryKey DevParam = PnPID.OpenSubKey("Device Parameters");
string sSerial = "";
string sModel = "";
//Define Search Keys
string sSerFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xff });
string sModFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xfc });
//Get the EDID code
byte[] bObj = DevParam.GetValue("EDID", null) as byte[];
if (bObj != null)
{
//Get the 4 Vesa descriptor blocks
string[] sDescriptor = new string[4];
sDescriptor[0] = Encoding.Default.GetString(bObj, 0x36, 18);
sDescriptor[1] = Encoding.Default.GetString(bObj, 0x48, 18);
sDescriptor[2] = Encoding.Default.GetString(bObj, 0x5A, 18);
sDescriptor[3] = Encoding.Default.GetString(bObj, 0x6C, 18);
//Search the Keys
foreach (string sDesc in sDescriptor)
{
if (sDesc.Contains(sSerFind))
{
sSerial = sDesc.Substring(4).Replace("'0", "").Trim();
}
if (sDesc.Contains(sModFind))
{
sModel = sDesc.Substring(4).Replace("'0", "").Trim();
}
}
}
if (!string.IsNullOrEmpty(sPNPID + sSerFind + sModel + sMonitorID))
{
yield return new DisplayDetails(sPNPID, sSerial, sModel, sMonitorID);
}
}
}
}
}
}
}
}
}
}
当device.cb
不等于 840 或 224 中的一个(分别为 sizeof
DISPLAY_DEVICEW
和 DISPLAY_DEVICEA
)时,我就有了这种行为。就我而言,我只是错误地写了device.cb = sizeof(device.cb);
检查两台计算机上的Marshal.SizeOf(device)
值。
我遇到了完全相同的问题,我发现我正在使用以下内容来导入EnumDisplayDevices
:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
而不是
[DllImport("user32.dll")]
GalacticJello
的代码示例对我有用。我强制为 x86 构建程序集。
在其他情况下,可以尝试:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DISPLAY_DEVICE`
但这不适用于 x86!