首页 > 解决方案 > C++ DXVA 库“操作失败,因为 DDC/CI 消息在其命令字段中具有无效值。”

问题描述

我正在尝试创建一个 C++ 控制台应用程序,它将根据屏幕上显示的像素的平均亮度自动调整显示器的亮度。不幸的是,在我进入第二部分之前,我什至在使用 DXVA 库时遇到了麻烦。

这是我当前的代码(我从这里获取了一些代码:如何使用 GetMonitorCapabilities 和 GetMonitorBrightness 函数):

#include "stdafx.h"
#include <iostream>

#include <Windows.h>
#include <WinUser.h>
#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>
#include <strsafe.h>

std::string GetLastErrorAsString()
{
    DWORD errorMessageID = ::GetLastError();
    if (errorMessageID == 0)
        return std::string();

    LPSTR messageBuffer = nullptr;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);

    std::string message(messageBuffer, size);
    LocalFree(messageBuffer);

    return message;
}

int main()
{
    HWND hWnd = GetDesktopWindow();
    HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
    DWORD cPhysicalMonitors = 0;

    BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
    if (!bSuccess) {
        std::cout << "An error occured: " << GetLastErrorAsString().c_str() << std::endl;
    }
    LPPHYSICAL_MONITOR pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));

    if (bSuccess)
    {
        if (pPhysicalMonitors != NULL)
        {
            bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);

            if (!bSuccess) {
                std::cout << "An error occured: " << GetLastErrorAsString().c_str() << std::endl;
            }

            HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;

            DWORD dwMinimumBrightness = 0;
            DWORD dwCurrentBrightness = 0;
            DWORD dwMaximumBrightness = 0;

            DWORD dwMonitorCapabilities = 0;
            DWORD dwSupportedColorTemperatures = 0;
            bSuccess = GetMonitorCapabilities(hPhysicalMonitor, &dwMonitorCapabilities, &dwSupportedColorTemperatures);

            if (bSuccess) {
                std::cout << "Capabilities: " << dwMonitorCapabilities << std::endl << "Supported color temperatures: " << dwSupportedColorTemperatures << std::endl;
            }
            else {
                std::cout << "An error occured while getting monitor capabilities: " << GetLastErrorAsString().c_str() << std::endl;
            }

            bSuccess = GetMonitorBrightness(hPhysicalMonitor, &dwMinimumBrightness, &dwCurrentBrightness, &dwMaximumBrightness);

            if (bSuccess) {
                std::cout << "Minimum brightness: " << dwMinimumBrightness << std::endl << "Maximum brightness: " << dwMaximumBrightness << std::endl << "Current brightness: " << dwCurrentBrightness << std::endl;
            }
            else {
                std::cout << "An error occured while getting brightness: " << GetLastErrorAsString().c_str() << std::endl;
            }

            bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);

            free(pPhysicalMonitors);
        }
    }
    return 0;
}

我得到的亮度/能力功能的错误是这样的:An operation failed because a DDC/CI message had an invalid value in its command field.

我已经在谷歌上搜索了几个小时,但我还没有找到解决我问题的方法。我有一张 AMD 显卡,我正在使用内置驱动程序(Google Lenovo e545 AMD 驱动程序),其中包括 Catalyst 版本15.7.1、Direct3D9.14.10.01128和驱动程序版本15.20.1062.1004-150803a1-187674C

对于这个混乱的问题和我缺乏经验,我深表歉意。

标签: c++dxva

解决方案


推荐阅读