none
How to adjust screen brightness level on notebook(OS: Win10 x64)? RRS feed

  • Question

  • Hi, I'm refer "Screen Brightness Control for Laptops and Tablets" to adjust brightness level on my notebook, but the brightness value not hold when reboot OS, why?

    I'm try this API "SetMonitorBrightness" , but does not work.
    #include <HighLevelMonitorConfigurationAPI.h>
    #pragma comment(lib, "Dxva2.lib")
    
    HMONITOR hMonitor = NULL;
    	DWORD cPhysicalMonitors;
    	LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
    
    	hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
    
    	BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(
    		hMonitor,
    		&cPhysicalMonitors
    	);
    
    	if (bSuccess)
    	{
    		pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
    			cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));
    
    		if (pPhysicalMonitors != NULL)
    		{
    			bSuccess = GetPhysicalMonitorsFromHMONITOR(
    				hMonitor, cPhysicalMonitors, pPhysicalMonitors);
    
    
    ASSERT(NULL != pPhysicalMonitors->hPhysicalMonitor); // Error, NULL == pPhysicalMonitors->hPhysicalMonitor
    
    			DWORD dwMin = 0;
    			DWORD dwCur = 0;
    			DWORD dwMax = 0;
    			bSuccess = GetMonitorBrightness(pPhysicalMonitors->hPhysicalMonitor, &dwMin, &dwCur, &dwMax);
    
    bSuccess = SetMonitorBrightness(pPhysicalMonitors->hPhysicalMonitor, dwMax);
    
    			bSuccess = DestroyPhysicalMonitors(
    				cPhysicalMonitors,
    				pPhysicalMonitors);
    
    			
    			free(pPhysicalMonitors);
    		}
    	}


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Wednesday, August 31, 2016 1:22 PM

Answers

  • Check the value of bSuccess after the call to GetPhysicalMonitorsFromHMONITOR.  If the call failed check the error code by calling GetLastError.
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:39 AM
    Wednesday, August 31, 2016 9:44 PM
  • I use

    IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS
    IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS
    IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS

    to adjust the LCD backlight of a Notebook. This control codes access only the build in LCD, not external monitors attached. That makes it very easy only to adjust the Notebook screen, not the additionally attached external screens.

    This works as expected on Notebooks and Tablets if the system supports this feature. In the past not all devices supported this control codes.


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Thursday, September 1, 2016 4:42 AM
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:45 AM
    Thursday, September 1, 2016 4:39 AM
  • Hi VisualEleven,

    >>The  value of the "hPhysicalMonitor" is NULL.

    So what's the last error code of GetPhysicalMonitorsFromHMONITOR?

    How about using GetMonitorInfo function and GetPhysicalMonitors function?

    Or use GetMonitorCapabilities function to find out which high-level monitor configuration functions are supported by the monitor.

    Best Regards,

    Sera YU


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.


    • Edited by Baron Bi Thursday, September 1, 2016 7:21 AM
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:39 AM
    Thursday, September 1, 2016 7:20 AM

All replies

  • Check the value of bSuccess after the call to GetPhysicalMonitorsFromHMONITOR.  If the call failed check the error code by calling GetLastError.
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:39 AM
    Wednesday, August 31, 2016 9:44 PM
  • Sorry, I forgot to add debug info.


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Thursday, September 1, 2016 1:57 AM
  • Hi VisualEleven,

    thanks for posting here.

    What's the value of cPhysicalMonitors?

    pPhysicalMonitors  is actually an array of PHYSICAL_MONITOR structures. Maybe you should use like this, for example the first monitor:

    pPhysicalMonitors[0]->hPhysicalMonitor
    

    Hope this could be help of you.

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Thursday, September 1, 2016 2:03 AM
  • Hi Sera Yu,
    The  value of the "hPhysicalMonitor" is NULL.


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Thursday, September 1, 2016 3:15 AM
  • I'm use "WmiMonitorBrightness" method could adjust screen brightness, but does not save when I reboot system, so I changed using "SetMonitorBrightness" API.

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Thursday, September 1, 2016 3:19 AM
  • If I use system settings to adjust the brightness, reboot the system the value of brightness can be saved.

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Thursday, September 1, 2016 3:24 AM
  • I use

    IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS
    IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS
    IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS

    to adjust the LCD backlight of a Notebook. This control codes access only the build in LCD, not external monitors attached. That makes it very easy only to adjust the Notebook screen, not the additionally attached external screens.

    This works as expected on Notebooks and Tablets if the system supports this feature. In the past not all devices supported this control codes.


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Thursday, September 1, 2016 4:42 AM
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:45 AM
    Thursday, September 1, 2016 4:39 AM
  • Hi VisualEleven,

    >>The  value of the "hPhysicalMonitor" is NULL.

    So what's the last error code of GetPhysicalMonitorsFromHMONITOR?

    How about using GetMonitorInfo function and GetPhysicalMonitors function?

    Or use GetMonitorCapabilities function to find out which high-level monitor configuration functions are supported by the monitor.

    Best Regards,

    Sera YU


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.


    • Edited by Baron Bi Thursday, September 1, 2016 7:21 AM
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:39 AM
    Thursday, September 1, 2016 7:20 AM
  • Finally, this bug is fixed, I'm not use "SetMonitorBrightness" API, because that invalid for me.
    Solution:
    After use "WmiSetBrightness" WMI interface and then call PowerGetActiveScheme / PowerWriteACValueIndex / PowerWriteDCValueIndex/PowerSetAcitveScheme() APIs.

    When call PowerXXX() APIs, and reboot system back, the value of brightness can be saved.
    GUID setting = { 0xaded5e82L, 0xb909, 0x4619, 0x99, 0x49, 0xf5, 0xd7, 0x1d, 0xac, 0x0b, 0xcb };
    		GUID* id = NULL;
    		DWORD dwBrightness = _tcstoul(strBrightness, NULL, 10);
    		PowerGetActiveScheme(NULL, &id);
    		PowerWriteACValueIndex(NULL, id, &GUID_VIDEO_SUBGROUP, &setting, dwBrightness);
    		PowerWriteDCValueIndex(NULL, id, &GUID_VIDEO_SUBGROUP, &setting, dwBrightness);
    		PowerSetActiveScheme(NULL, id);
    		if (NULL != id) { LocalFree(id); }
    It's work.


    Thanks all and thank you for your advice.

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.



    • Edited by VisualEleven Thursday, September 1, 2016 7:38 AM
    • Marked as answer by VisualEleven Thursday, September 1, 2016 7:45 AM
    • Unmarked as answer by VisualEleven Thursday, September 1, 2016 7:49 AM
    Thursday, September 1, 2016 7:36 AM