p/invoke 和 EnumDisplaySettingsEx 的困难 [英] Difficulties with p/invoke and EnumDisplaySettingsEx

查看:54
本文介绍了p/invoke 和 EnumDisplaySettingsEx 的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索系统上存在的所有各种监视器(及其适配器)的信息.到目前为止,我尝试使用 EnumDisplayDevices 列出所有显示器效果很好 - 它找到了两个,第一个是 "\\.\DISPLAY1\Monitor0"(第二个是分别为 2 和 1,但这与此问题无关).无论如何,我然后尝试在其上调用 EnumDisplaySettingsEx ,如上所述传递监视器的名称,但它总是失败.使用空名称参数调用成功,但返回的 DEVMODE 结构表示 dmDeviceName 是cdd",我非常怀疑它是否准确.我做错了什么?

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?

哦,我已经尝试过使用 EnumDisplaySettings(非 Ex 的),但它甚至不适用于空名称参数.

Oh, and I've tried using EnumDisplaySettings (the non-Ex one) and that doesn't even work with the null name parameter.

P/Invoke 签名(Ex 稍微修改了 pinvoke.net 上的签名,以使其正常工作):

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 定义在 http://www.pinvoke.net/default.aspx/Structures/DEVMODE.html.EdsDwFlags 目前始终为零.iModeNum 为 -1(对于当前设置).

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).

出于某种原因,StackOveflow 不允许我在这篇文章中附加 C# 标签.

And for some reason StackOveflow won't let me attach a C# tag to this post.

推荐答案

我是个白痴,你应该给这个函数传递一个显示适配器,而不是一个监视器.仅传递 "\\.\DISPLAY1" 工作正常.

I'm an idiot, you're supposed to pass a display adapter to this function, not a monitor. Passing just "\\.\DISPLAY1" worked fine.

不过,我仍然收到 dmDeviceName 字段的垃圾,所以我很感激任何关于我是如何出错的建议.

I'm still getting garbage back for the dmDeviceName field, though, so I'd appreciate any suggestions as to how I got that wrong.

这篇关于p/invoke 和 EnumDisplaySettingsEx 的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆