Setting brightness on Windows 10 using C++ WinAPI
<p>I am trying to set the brightness on a Windows 10 machine. The display doesn't seem to support <code>setMonitorBrightness</code>, and <code>setDeviceGammaRamp</code> alters the gamma, white point etc, so I would try not to use it.</p>

<p>I am trying to get this to work using <code>IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS</code> control.  When I get the monitor handle using <code>CreateFile()</code>, I check if the handle is invalid and it is fine. But I get <code>ERROR_INVALID_HANDLE</code> (error 6) when I call <code>DeviceIoControl()</code> with <code>IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS</code>.</p>

<pre><code>typedef struct _DISPLAY_BRIGHTNESS {  
   UCHAR ucDisplayPolicy;  
   UCHAR ucACBrightness;  
   UCHAR ucDCBrightness;  
} DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS; 

DISPLAY_BRIGHTNESS _displayBrightness;

_displayBrightness.ucDisplayPolicy = 0;
_displayBrightness.ucACBrightness = 0;  //for testing purposes
_displayBrightness.ucDCBrightness = 0;

DWORD ret = NULL;
OVERLAPPED olp;

DWORD nOutBufferSize = sizeof(_displayBrightness);
HANDLE h = CreateFile(L"\\\\.\\LCD",
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    0, NULL);

if (h == INVALID_HANDLE_VALUE) {
    //Does not reach here
    return false;
}  

if (!DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (DISPLAY_BRIGHTNESS *)&amp;_displayBrightness, nOutBufferSize, NULL, 0, &amp;ret, &amp;olp))
{
    // GetLastError() returns error code 6 - Invalid handle 
    return false;
} 
</code></pre>

<p>Also, should I be using <code>CreateFile()</code> to get the monitor handle, or can I call <code>MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY)</code> instead?</p>