4

我需要获取辅助监视器的设备名称。但是,当我只是尝试检索设备名称时,输出是DISPLAY1, DISPLAYV1等等。

但是,当我们检查屏幕分辨率时,我需要显示的名称,例如此处提到的 Displayname:

首先,我不确定从哪里可以获得这个字符串。在阅读了一下我猜这是friendlyname设备的。但是我不确定,因为在调用此函数时调用EnumDisplaySetting()已经给了我。Unhandled Exception: Could not access memory location所以我无法验证友好名称到底是什么。而且我认为未处理的异常是由于 DISPLAY_DEVICE 为 DISPLAY_DEVICE 中的 driverextra 分配内存不正确引起的。我相信这是因为:

如果 iModeNum 大于显示设备的最后一个图形模式的索引,则该函数将失败。

这里提到

我也不明白需要为
DISPLAY_DEVICE->dmDriverExtra 分配多少内存,因为它已在同一链接中提到:

在调用 EnumDisplaySettings 之前,将 dmSize 成员设置为 sizeof(DEVMODE),并设置 dmDriverExtra 成员以指示可用于接收私有驱动程序数据的额外空间的大小(以字节为单位)。

所以我的问题是多方面的:

1) dmDriverExtra 需要分配多少内存?

2)friendlyname 是访问屏幕分辨率中显示选项卡中提供的名称所需的正确参数。或者如果不是我还需要什么其他参数?

3)这个未处理的异常是由于内存分配不当引起的还是有其他原因?

4)还有其他方法可以访问辅助监视器的友好名称吗?

4

1 回答 1

4

更新

我改用 The PhysicalMonitorAPI 而不是 GetMonitorInfo。我已将原始解决方案与第一个解决方案结合起来。这会产生您所期望的更合理的输出(例如“Dell UH2313”而不是“\.\Display1”)。

从技术上讲,您应该分配监视器数组而不是使用硬编码数组 - 但我从未见过 dwCount 将在哪里初始化为大于 1 的任何值。

该程序在 Visual Studio 中编译得很好,但您需要与 dxva2.lib 链接以获取PhysicalMonitor API 的定义。

#include <Windows.h>
#include <PhysicalMonitorEnumerationAPI.h>
#include <string>
#include <iostream>
#include <stdio.h>

BOOL __stdcall MyMonitorEnumProc
(
_In_ HMONITOR hMonitor,
_In_ HDC      hdcMonitor,
_In_ LPRECT   lprcMonitor,
_In_ LPARAM   dwData
)
{
    DWORD dwCount = 0;
    std::wstring strName(L"Unknown monitor name");
    PHYSICAL_MONITOR monitors[100] = {};
    MONITORINFOEX info = {};
    info.cbSize = sizeof(info);

    if (GetMonitorInfo(hMonitor, (LPMONITORINFO)&info))
    {
        strName = info.szDevice;
    }

    if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &dwCount) && (dwCount > 0) && (dwCount < ARRAYSIZE(monitors)))
    {
        if (GetPhysicalMonitorsFromHMONITOR(hMonitor, dwCount, monitors))
        {
            strName = monitors[0].szPhysicalMonitorDescription;

            DestroyPhysicalMonitors(dwCount, monitors);
        }
    }

    std::wcout << L"Monitor: " << strName << std::endl;

    return TRUE;
}

void printMonitorNames()
{
    EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, NULL);
}

int _tmain(int argc, _TCHAR* argv[])
{
    printMonitorNames();
    return 0;
}

可以肯定的是,MyMonitorEnumProc 将首先被主监视器调用。接下来将枚举所有其他监视器。

于 2015-10-01T16:09:36.983 回答