如何使用GetMonitorCapabilities和GetMonitorBrightness函数

时间:2015-12-04 15:40:28

标签: c++ windows winapi

我试图以编程方式调整我的显示器亮度。经过一些研究,我想出了这个link,并编写了以下代码(主要是从其他链接复制粘贴,一个引导我)。

#include "Windows.h"
#include "WinUser.h"
#include "PhysicalMonitorEnumerationAPI.h"
#include "HighLevelMonitorConfigurationAPI.h"
#include <strsafe.h>

void ShowError(LPTSTR lpszFunction);

int main()
{
    HMONITOR hMonitor = NULL;
    DWORD cPhysicalMonitors;
    LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;

    HWND hWnd = GetDesktopWindow();

    // Get the monitor handle.
    hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);

    // Get the number of physical monitors.
    BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);

    if (bSuccess)
    {
        // Allocate the array of PHYSICAL_MONITOR structures.
        pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));

        if (pPhysicalMonitors != NULL)
        {
            // Get the array.
            bSuccess = GetPhysicalMonitorsFromHMONITOR( hMonitor, cPhysicalMonitors, pPhysicalMonitors);

            // Get physical monitor handle.
            HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;

            LPDWORD pdwMinimumBrightness = NULL;
            LPDWORD pdwCurrentBrightness = NULL;
            LPDWORD pdwMaximumBrightness = NULL;
            bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
            if (bSuccess == FALSE)
            {
                ShowError(TEXT("GetMonitorBrightness"));
            }

            // Close the monitor handles.
            bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);

            // Free the array.
            free(pPhysicalMonitors);
        }
    }
    return 0;
}

void ShowError(LPTSTR lpszFunction)
{
    // Retrieve the system error message for the last-error code
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process
    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
    StringCchPrintf((LPTSTR)lpDisplayBuf,
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"),
        lpszFunction, dw, lpMsgBuf);
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
}

此代码在执行此行时崩溃:

bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);

根据文档,可能不支持该功能。

  

如果支持此功能,则使用GetMonitorCapabilities函数   返回MC_CAPS_BRIGHTNESS标志。

因此,为了检查这一点,我在调用GetMonitorBrightness之前将以下块添加到我的代码中。

LPDWORD pdwMonitorCapabilities = NULL;
LPDWORD pdwSupportedColorTemperatures = NULL;
bSuccess = GetMonitorCapabilities(hPhysicalMonitor, pdwMonitorCapabilities, pdwSupportedColorTemperatures);
if (bSuccess == FALSE)
{
    ShowError(TEXT("GetMonitorCapabilities"));
}

不幸的是,在我添加了该块之后,我收到了以下错误:

enter image description here

同样,根据documentation,如果监视器不支持DDC/CIGetMonitorCapabilities函数将失败。

然后我检查了我的显示器是否支持DDC / CI,并发现它是。此外,当我从显示器设置手动禁用DDC / CI支持时,之前的错误消息切换到下一个,所以现在我非常确定我的显示器具有DDC / CI支持

enter image description here

我觉得我做的一切都是正确的,但显然我不是。简而言之,GetMonitorCapabilities函数失败,并显示一条错误消息,表示我无法理解,GetMonitorBrightness函数会崩溃。

备注:

我的监视器是Dell U2713H

我在64位Windows 7上。

我正在使用Microsoft Visual C ++编译器12.0(x86)

1 个答案:

答案 0 :(得分:7)

您对GetMonitorBrightness()GetMonitorCapabilities()的来电是错误的。您正在传递NULL指针,但他们希望指向实际的DWORD变量:

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

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