【问题标题】:(WMI) How do I know which monitor is connected to which graphics card? (c++ or c#)(WMI) 我如何知道哪个显示器连接到哪个显卡? (C++ 或 C#)
【发布时间】:2021-03-31 19:02:21
【问题描述】:

我可以用Win32_DesktopMonitor查询所有显示器,用Win32_VideoController查询所有显卡,但是我怎么知道哪个显示器连接了哪个显卡呢?

假设我使用 Win32_VideoController 获得两个桌面分辨率,19201080 和 1600900。我有两台显示器,戴尔和惠普。我怎么知道哪台显示器的分辨率是多少?

使用 Win32_DesktopMonitor 可以获得 ScreenWidth 和 ScreenHeight,但如果驱动程序不支持 WDDM,这两个值将不存在。而且即使我获得了分辨率,我也无法获得更多信息,例如 Win10 设置。我怎么知道 Dell 连接到 Display1 而 VE 连接到 Display2?

【问题讨论】:

  • 控制面板可能没有使用 WMI。
  • 请注意,如果您显示用于执行这些查询的 wmic 或 Powershell 命令,会更容易为您提供帮助。
  • 我的问题是我想知道哪个 WMI 可以得到这些信息?我尝试过的 WMI 都无法获得相关信息。而且我也接受没有 WMI 的 Windows API(C++ 或 C#)解决方案。

标签: winapi wmi


【解决方案1】:

我怎么知道 Dell 连接到 Display1 而 VE 连接到 连接到 Display2 了吗?

EnumDisplayMonitors 似乎正是您所需要的。

一个简单的C++代码示例,

#include <Windows.h>
#include <iostream>
#include <vector>
#include <cstdio>

std::wstring getMonitorName(HMONITOR monitor) {
    DISPLAYCONFIG_TOPOLOGY_ID currentTopologyId;
    MONITORINFOEXW info;
    info.cbSize = sizeof(info);
    GetMonitorInfoW(monitor, &info);

    UINT32 requiredPaths, requiredModes;
    GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &requiredPaths, &requiredModes);
    std::vector<DISPLAYCONFIG_PATH_INFO> paths(requiredPaths);
    std::vector<DISPLAYCONFIG_MODE_INFO> modes(requiredModes);
    QueryDisplayConfig(QDC_DATABASE_CURRENT, &requiredPaths, paths.data(), &requiredModes, modes.data(), &currentTopologyId);
   
    for (auto& p : paths) {
        DISPLAYCONFIG_SOURCE_DEVICE_NAME sourceName;
        sourceName.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
        sourceName.header.size = sizeof(sourceName);
        sourceName.header.adapterId = p.sourceInfo.adapterId;
        sourceName.header.id = p.sourceInfo.id;
      
        DisplayConfigGetDeviceInfo(&sourceName.header);
        if (wcscmp(info.szDevice, sourceName.viewGdiDeviceName) == 0) {
            DISPLAYCONFIG_TARGET_DEVICE_NAME name;
            name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
            name.header.size = sizeof(name);
            name.header.adapterId = p.sourceInfo.adapterId;
            name.header.id = p.targetInfo.id;
            DisplayConfigGetDeviceInfo(&name.header);
            return std::wstring(name.monitorFriendlyDeviceName);
        }
    }
    return L"";
}


BOOL CALLBACK MyInfoEnumProc(
    HMONITOR hMonitor,
    HDC hdcMonitor,
    LPRECT lprcMonitor,
    LPARAM dwData
)
{
    MONITORINFOEX mi;
    ZeroMemory(&mi, sizeof(mi));
    mi.cbSize = sizeof(mi);
    GetMonitorInfo(hMonitor, &mi);
    wprintf(L"DisplayDevice: %s\n", mi.szDevice);

    std::wcout << L"Name: " << getMonitorName(hMonitor) << L"\n";
    return TRUE;
}

int main()
{
    EnumDisplayMonitors(NULL, NULL, MyInfoEnumProc, 0);
    return 0;
}

调试:

【讨论】:

  • 非常感谢,这正是我想要的。
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多