gpt4 book ai didi

c++ - 获取辅助监视器的友好名称

转载 作者:可可西里 更新时间:2023-11-01 14:13:34 35 4
gpt4 key购买 nike

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

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

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

The function fails if iModeNum is greater than the index of the display device's last graphics mode.

提到here

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

Before calling EnumDisplaySettings, set the dmSize member to sizeof(DEVMODE), and set the dmDriverExtra member to indicate the size, in bytes, of the additional space available to receive private driver data.

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

1) How much memory needs to be allocated to dmDriverExtra?

2) Is friendlyname the right parameter I need for accessing the name provided in the Display Tab in screen resolution. Or if not what other parameter do I need?

3) Is this unhandled exception caused due to improper memory allocation or is there some other reason for this?

4) Are there any other ways to get access to friendlyname of the secondary monitor?

最佳答案

已更新

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

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

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

#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 将首先为主监视器调用是一个不错的选择。接下来枚举所有其他监视器。

关于c++ - 获取辅助监视器的友好名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32891939/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com