Difficulties with p/invoke and EnumDisplaySettingsEx

断了今生、忘了曾经 提交于 2021-01-29 04:25:32

问题


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:


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.



来源:https://stackoverflow.com/questions/3383970/difficulties-with-p-invoke-and-enumdisplaysettingsex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!