我正在尝试检索系统上存在的所有各种监视器(及其适配器)的信息。到目前为止,我尝试使用 EnumDisplayDevices 列出所有显示器效果很好 - 它发现了两个,第一个是 "\\.\DISPLAY1\Monitor0"
(第二个是分别只是 2 和 1,但这与这个问题无关)。无论如何,然后我尝试对其调用 EnumDisplaySettingsEx
,并按上面的方式传递监视器的名称,但它总是失败。使用空名称参数的调用成功,但返回的 DEVMODE
结构表示 dmDeviceName
是“cdd”,我非常怀疑它是否准确。我做错了什么?
哦,我尝试过使用 EnumDisplaySettings
(非 Ex 的),但它甚至不适用于 null 名称参数。
P/Invoke 签名(Ex 对 pinvoke.net 上的签名进行了轻微修改,尝试使其正常工作):
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool EnumDisplaySettingsEx([MarshalAs(UnmanagedType.LPStr)]string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode, EdsDwFlags dwFlags);
DEVMODE
在 http://www.pinvoke.net/default.aspx/Structures/DEVMODE.html。 EdsDwFlags
目前始终为零。 iModeNum
为 -1(对于当前设置)。
由于某种原因,StackOveflow 不允许我在这篇文章中附加 C# 标签。
I am attempting to retrieve information on all the various monitors (and their adapters) present on a system. So far, my attempts at using EnumDisplayDevices
to list all the monitors has worked great - it found two, the first being "\\.\DISPLAY1\Monitor0"
(the second is just 2 and 1, respectively, but it's irrelevant to this question). Anyway, I then attempted to call EnumDisplaySettingsEx
on it, passing the name of the monitor as above, but it always fails. Calls with a null name parameter succeed, but the DEVMODE
structure returned says the dmDeviceName
is "cdd" which I highly doubt is accurate. What am I doing wrong?
Oh, and I've tried using EnumDisplaySettings
(the non-Ex one) and that doesn't even work with the null name parameter.
P/Invoke signatures (Ex's slightly modified from the one on pinvoke.net in a flailing attempt to get it to work):
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool EnumDisplaySettingsEx([MarshalAs(UnmanagedType.LPStr)]string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode, EdsDwFlags dwFlags);
DEVMODE
is defined at http://www.pinvoke.net/default.aspx/Structures/DEVMODE.html. EdsDwFlags
for now is always zero. iModeNum
is -1 (for current settings).
And for some reason StackOveflow won't let me attach a C# tag to this post.
发布评论
评论(1)
我是个白痴,你应该将显示适配器传递给这个函数,而不是显示器。仅传递
"\\.\DISPLAY1"
效果很好。不过,我仍然收到
dmDeviceName
字段的垃圾信息,因此我很感激任何有关我如何出错的建议。I'm an idiot, you're supposed to pass a display adapter to this function, not a monitor. Passing just
"\\.\DISPLAY1"
worked fine.I'm still getting garbage back for the
dmDeviceName
field, though, so I'd appreciate any suggestions as to how I got that wrong.