0

I want to adjust the brightness of my monitor on Windows.

I follow this page:

How to use GetMonitorCapabilities and GetMonitorBrightness functions

but, the GetMonitorCapabilities function returns false.

I printed using qDebug, hPhysicalMonitor is NULL. Is this point wrong?

(The GetPhysicalMonitorsFromHMONITOR function returns true)

How can I fix this code?

HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors = 1;
LPPHYSICAL_MONITOR pPhysicalMonitors;

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);
        if (bSuccess == FALSE)
        {
            qDebug("GetPhysicalMonitorsFromHMONITOR");
        }
        // Get physical monitor handle.
        HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;

        DWORD pdwMonitorCapabilities = 0;
        DWORD pdwSupportedColorTemperatures = 0;
        bSuccess = GetMonitorCapabilities(hPhysicalMonitor, &pdwMonitorCapabilities, &pdwSupportedColorTemperatures);
        if (bSuccess == FALSE)
        {
            qDebug("GetMonitorCapabilities");
        }

        DWORD pdwMinimumBrightness = 0;
        DWORD pdwCurrentBrightness = 0;
        DWORD pdwMaximumBrightness = 0;
        bSuccess = GetMonitorBrightness(hPhysicalMonitor, &pdwMinimumBrightness, &pdwCurrentBrightness, &pdwMaximumBrightness);
        if (bSuccess == FALSE)
        {
            qDebug("GetMonitorBrightness");
        }

        qDebug(QByteArray::number(bSuccess));

        qDebug(QByteArray::number((WORD)pdwMinimumBrightness));
        qDebug(QByteArray::number((WORD)pdwCurrentBrightness));
        qDebug(QByteArray::number((WORD)pdwMaximumBrightness));
        // Close the monitor handles.
        bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);

        // Free the array.
        free(pPhysicalMonitors);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • [GetMonitorCapabilities](https://msdn.microsoft.com/en-us/library/windows/desktop/dd692940.aspx): *"If the function fails, the return value is FALSE. **To get extended error information, call GetLastError.**"* – IInspectable May 20 '17 at 09:16
  • 1
    From that same topic. *The function fails if the monitor does not support DDC/CI.* – David Heffernan May 20 '17 at 10:02
  • this code periodically not worked and then worked. error - `ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA` *An error occurred while transmitting data to the device on the I2C bus.* – RbMm May 20 '17 at 10:52
  • say first call to `GetMonitorCapabilities` can fail with `ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA` but second call with same data and parameters - ok – RbMm May 20 '17 at 10:56
  • laptop computer does not work this funcion... if i fix using wmiSetBrighteness, how to change this code? – Eunjeong Choi May 22 '17 at 07:53

1 Answers1

0

If you want to adjust the brightness using WmiMonitorBrightnessMethods use the code below.

     public static void SetWmiMonitorBrightness(byte targetBrightness) 
    {
        ManagementScope scope = new ManagementScope("root\\WMI");
        SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            using (ManagementObjectCollection objectCollection = searcher.Get())
            {
                foreach (ManagementObject mObj in objectCollection)
                {
                    mObj.InvokeMethod("WmiSetBrightness",
                        new Object[] { UInt32.MaxValue, targetBrightness });
                    break;
                }
            }
        }
    }

Doing GetMonitorBrightness on the specific monitor I have causes ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA.

So I used WmiSetBrightness instead. It works fine.

If you got a tip related to ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA, please share it with me.

help
  • 87
  • 1
  • 6