1

I'm trying to create a simple Monitor Brightness adjustion. SetMonitorBrightness() doesn't work on the desired monitor somehow (ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA) so I'm trying IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS.

To get the handle, I used CreateFile:

HANDLE hDisplay = ::CreateFile(_T("\\.\LCD"),
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    CREATE_NEW,
    FILE_FLAG_BACKUP_SEMANTICS, NULL);

Now when I try to acces the handle in DeviceIoControl()

DWORD ret = NULL;
OVERLAPPED olp;
if (!DeviceIoControl(hDisplay, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, &brgt, sizeof(brgt), NULL, 0, &ret, &olp))
{
    //ErrorMessageblabla
}

I get Error 6: Invalid Handle

What am I doing wrong?

  • Did you check the result from `CreateFile`? I suspect that some of your flags (`CREATE_NEW` and `...BACKUP...`) are wrong. Also: close the handle when you're done, otherwise display switching (e.g. for presentations) misbehaves. Trust me on that. – Roger Lipscombe Mar 21 '16 at 11:46
  • Not enough error checking, surely you did not find out that CreateFile() failed. CREATE_NEW makes no sense. Use the monitor device name you got from EnumMonitors or an alias like "DISPLAY1". Do keep in mind that the IOCTL is just as likely to fail, if the I2C communication with the monitor doesn't work then there is no hope. Setting the monitor brightness programmatically is in general is a big fail whale and best avoided. – Hans Passant Mar 21 '16 at 11:46
  • Oh, and you've not sufficiently escaped the `\\` in the device name either. (oh, irony: I didn't escape it in this comment the first time either...) – Roger Lipscombe Mar 21 '16 at 11:46
  • Possible duplicate of [Trying to open display device handle to change brightness on Windows XP using C++](http://stackoverflow.com/questions/11874615/trying-to-open-display-device-handle-to-change-brightness-on-windows-xp-using-c) – Roger Lipscombe Mar 21 '16 at 11:48
  • Is this a laptop display or external monitor? You can use the EnumDisplayDevicesW() with EDD_GET_DEVICE_INTERFACE_NAME set to get the DeviceID which you can pass to CreateFile, which should give you a valid handle, that you can use with DeviceIoControl, but this is only for internal/laptop screens (that you can control through the control panel). – jacob_pro Dec 26 '21 at 02:44

0 Answers0