Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why GetMonitorBrightness function is returning false in the following code ?
QtWS25 Last Chance

Why GetMonitorBrightness function is returning false in the following code ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 5.7api
17 Posts 5 Posters 9.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AlterX
    30 Aug 2016, 17:19

    It is not working because at least you have to respect the kind of parameters!
    Please ready carefully the msdn...
    GetPhysicalMonitorsFromHMONITOR as 3rd parameter wants a pointer to PHYSICAL_MONITOR
    and GetMonitorBrightness wants a handle, that is: realMonitorHandle.hwnd

    A Offline
    A Offline
    Ahti
    wrote on 30 Aug 2016, 18:31 last edited by
    #8

    @AlterX not working it crash on first if statement :

    https://postimg.org/image/p0w2ku3o5/

    what is a signature ?? Lol

    J 1 Reply Last reply 31 Aug 2016, 04:35
    0
    • A Ahti
      30 Aug 2016, 18:31

      @AlterX not working it crash on first if statement :

      https://postimg.org/image/p0w2ku3o5/

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 31 Aug 2016, 04:35 last edited by
      #9

      @Ahti Why don't you read the documentation?
      Here you can find this information https://msdn.microsoft.com/en-us/library/windows/desktop/dd692950(v=vs.85).aspx
      pPhysicalMonitorArray [out]
      Pointer to an array of PHYSICAL_MONITOR structures. The caller must allocate the array.

      You currently pass NULL! Check the above link - there is even an example.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 31 Aug 2016, 20:19
      1
      • J jsulm
        31 Aug 2016, 04:35

        @Ahti Why don't you read the documentation?
        Here you can find this information https://msdn.microsoft.com/en-us/library/windows/desktop/dd692950(v=vs.85).aspx
        pPhysicalMonitorArray [out]
        Pointer to an array of PHYSICAL_MONITOR structures. The caller must allocate the array.

        You currently pass NULL! Check the above link - there is even an example.

        A Offline
        A Offline
        Ahti
        wrote on 31 Aug 2016, 20:19 last edited by Ahti
        #10

        @jsulm

        now GetMonitorCapabilities is returning false.

        My Code:

        void EyeCare::on_startcaringButton_clicked()
        {
            QString style = ui->startcaringButton->styleSheet() ;
        
            HMONITOR monitorHandler = NULL ;
            DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
            curBrightnessLevel = 0 ;
            HWND windowHandler ;    
        
            DWORD numberOfMonitors = 1 ;
            LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
        
            windowHandler = GetDesktopWindow() ; // is equal to FindWindow(NULL,NULL) ;
            monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
        
            BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
        
            if (bSuccess) {
        
                pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                          numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                if (pointerToPhysicalMonitors != NULL) {
        
                    bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                monitorHandler,numberOfMonitors,
                                pointerToPhysicalMonitors) ;
        
                    LPDWORD MonitorCapabilities = NULL, SupportedColorTemperature = NULL;
        
                    if (GetMonitorCapabilities(monitorHandler, MonitorCapabilities, SupportedColorTemperature)) {
        
                      if (GetMonitorBrightness(windowHandler,&minBrightnessLevel,
                                 &curBrightnessLevel, &maxBrightnessLevel) ){
                          DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                          SetMonitorBrightness(monitorHandler,newBrightnessLevel) ;
                          SaveCurrentMonitorSettings(monitorHandler) ;
        
                          style.append("color: green ;") ;
                       }else
                          style.append("color: red ;") ;
                     }else{
                         style.append("color: purple ;") ;                
                     }
                  }else
                      style.append("color: orange ;") ;
        
              }else
                  style.append("color: yellow ;") ;
        
            ui->startcaringButton->setStyleSheet (style) ;
        }
        

        what is a signature ?? Lol

        J A 2 Replies Last reply 1 Sept 2016, 04:17
        0
        • A Ahti
          31 Aug 2016, 20:19

          @jsulm

          now GetMonitorCapabilities is returning false.

          My Code:

          void EyeCare::on_startcaringButton_clicked()
          {
              QString style = ui->startcaringButton->styleSheet() ;
          
              HMONITOR monitorHandler = NULL ;
              DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
              curBrightnessLevel = 0 ;
              HWND windowHandler ;    
          
              DWORD numberOfMonitors = 1 ;
              LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
          
              windowHandler = GetDesktopWindow() ; // is equal to FindWindow(NULL,NULL) ;
              monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
          
              BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
          
              if (bSuccess) {
          
                  pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                            numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                  if (pointerToPhysicalMonitors != NULL) {
          
                      bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                  monitorHandler,numberOfMonitors,
                                  pointerToPhysicalMonitors) ;
          
                      LPDWORD MonitorCapabilities = NULL, SupportedColorTemperature = NULL;
          
                      if (GetMonitorCapabilities(monitorHandler, MonitorCapabilities, SupportedColorTemperature)) {
          
                        if (GetMonitorBrightness(windowHandler,&minBrightnessLevel,
                                   &curBrightnessLevel, &maxBrightnessLevel) ){
                            DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                            SetMonitorBrightness(monitorHandler,newBrightnessLevel) ;
                            SaveCurrentMonitorSettings(monitorHandler) ;
          
                            style.append("color: green ;") ;
                         }else
                            style.append("color: red ;") ;
                       }else{
                           style.append("color: purple ;") ;                
                       }
                    }else
                        style.append("color: orange ;") ;
          
                }else
                    style.append("color: yellow ;") ;
          
              ui->startcaringButton->setStyleSheet (style) ;
          }
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 1 Sept 2016, 04:17 last edited by
          #11

          @Ahti said in Why GetMonitorBrightness function is returning false in the following code ?:

          LPDWORD MonitorCapabilities = NULL, SupportedColorTemperature = NULL;

                  if (GetMonitorCapabilities(monitorHandler, MonitorCapabilities, SupportedColorTemperature))
          

          Shouldn't it be:

          DWORD MonitorCapabilities = 0, SupportedColorTemperature = 0;
          
          if (GetMonitorCapabilities(monitorHandler, &MonitorCapabilities, &SupportedColorTemperature))
          

          The documentation says: "To get extended error information, call GetLastError"
          So, you should call GetLastError to see what went wrong.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Ahti
            31 Aug 2016, 20:19

            @jsulm

            now GetMonitorCapabilities is returning false.

            My Code:

            void EyeCare::on_startcaringButton_clicked()
            {
                QString style = ui->startcaringButton->styleSheet() ;
            
                HMONITOR monitorHandler = NULL ;
                DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
                curBrightnessLevel = 0 ;
                HWND windowHandler ;    
            
                DWORD numberOfMonitors = 1 ;
                LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
            
                windowHandler = GetDesktopWindow() ; // is equal to FindWindow(NULL,NULL) ;
                monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
            
                BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
            
                if (bSuccess) {
            
                    pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                              numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                    if (pointerToPhysicalMonitors != NULL) {
            
                        bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                    monitorHandler,numberOfMonitors,
                                    pointerToPhysicalMonitors) ;
            
                        LPDWORD MonitorCapabilities = NULL, SupportedColorTemperature = NULL;
            
                        if (GetMonitorCapabilities(monitorHandler, MonitorCapabilities, SupportedColorTemperature)) {
            
                          if (GetMonitorBrightness(windowHandler,&minBrightnessLevel,
                                     &curBrightnessLevel, &maxBrightnessLevel) ){
                              DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                              SetMonitorBrightness(monitorHandler,newBrightnessLevel) ;
                              SaveCurrentMonitorSettings(monitorHandler) ;
            
                              style.append("color: green ;") ;
                           }else
                              style.append("color: red ;") ;
                         }else{
                             style.append("color: purple ;") ;                
                         }
                      }else
                          style.append("color: orange ;") ;
            
                  }else
                      style.append("color: yellow ;") ;
            
                ui->startcaringButton->setStyleSheet (style) ;
            }
            
            A Offline
            A Offline
            AlterX
            wrote on 1 Sept 2016, 07:24 last edited by AlterX 9 Jan 2016, 07:27
            #12

            @Ahti
            You should really understand that LPXXXX means pointer and is used when you pass a parameter to avoid that the object you are passing is copied by value (so all items inside in case of struct/class); passing a pointer only the address is copied in a new variable; that is:

            DWORD pdwMonitorCapabilities, pdwSupportedColorTemperatures; // a real allocated object or DWORD in this case
            
            GetMonitorCapabilities(monitorHandler, &pdwMonitorCapabilities, &pdwSupportedColorTemperatures) // address of allocated objects
            
            

            If you pass a pointer sets to NULL, you are calling:

            GetMonitorCapabilities(monitorHandler, NULL, NULL)
            

            it is normal it returns FALSE because where should the funtion put the values you are asking for??
            thus the function is not able to do the job it has been created for!!!

            Qt Ambassador
            Real-time cooperative teams: http://www.softairrealfight.net
            Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

            https://codereview.qt-project.org/...

            A 1 Reply Last reply 1 Sept 2016, 15:32
            0
            • A AlterX
              1 Sept 2016, 07:24

              @Ahti
              You should really understand that LPXXXX means pointer and is used when you pass a parameter to avoid that the object you are passing is copied by value (so all items inside in case of struct/class); passing a pointer only the address is copied in a new variable; that is:

              DWORD pdwMonitorCapabilities, pdwSupportedColorTemperatures; // a real allocated object or DWORD in this case
              
              GetMonitorCapabilities(monitorHandler, &pdwMonitorCapabilities, &pdwSupportedColorTemperatures) // address of allocated objects
              
              

              If you pass a pointer sets to NULL, you are calling:

              GetMonitorCapabilities(monitorHandler, NULL, NULL)
              

              it is normal it returns FALSE because where should the funtion put the values you are asking for??
              thus the function is not able to do the job it has been created for!!!

              A Offline
              A Offline
              Ahti
              wrote on 1 Sept 2016, 15:32 last edited by Ahti 9 Jan 2016, 15:40
              #13

              @jsulm @AlterX

              This is what i get after using GetLastError function and fixing the bug in my code :

              https://postimg.org/image/tq6eabl95/

              why it says "invalid monitor handler was passed to it" even though i copied the code from the document ??

              My Code:

              void EyeCare::on_startcaringButton_clicked()
              {
                  QString style = ui->startcaringButton->styleSheet() ;
              
                  QLibrary userLib ("user32.dll") ;
              
                  if ( userLib.load() ){
              
                  HMONITOR monitorHandler = NULL ;
                  DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
                  curBrightnessLevel = 0 ;
                  HWND windowHandler ;    
              
                  DWORD numberOfMonitors = 1 ;
                  LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
              
                  windowHandler = GetDesktopWindow() ;
                  monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
              
                  BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
              
                  if (bSuccess) {
              
                      pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                                numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                      if (pointerToPhysicalMonitors != NULL) {
              
                          bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                      monitorHandler,numberOfMonitors,
                                      pointerToPhysicalMonitors) ;
              
                          DWORD MonitorCapabilities = 0, SupportedColorTemperature = 0 ;
              
                          if (GetMonitorCapabilities(monitorHandler, &MonitorCapabilities, &SupportedColorTemperature)) {
              
                            if (GetMonitorBrightness(windowHandler,&minBrightnessLevel,
                                       &curBrightnessLevel, &maxBrightnessLevel) ){
                                DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                                SetMonitorBrightness(monitorHandler,newBrightnessLevel) ;
                                SaveCurrentMonitorSettings(monitorHandler) ;
              
                                style.append("color: green ;") ;
                            }else{
                                style.append("color: red ;") ;
                                GetLastErrorString(TEXT("GetMonitorBrightness")) ;
                            }
                          }else{
                              style.append("color: purple ;") ;
                              GetLastErrorString(TEXT("GetMonitorCapabilities")) ;
                          }
                        }else {
                            style.append("color: orange ;") ;
                            GetLastErrorString(TEXT("PhysicalMonitor")) ;
                        }
                    }else{
                        style.append("color: yellow ;") ;
                        GetLastErrorString(TEXT("GetNumberOfPhysicalMonitorsFromHMONITOR")) ;
                    }
                  ui->startcaringButton->setStyleSheet (style) ;
              
              
                }else{
              
                    QErrorMessage *errorMessage = new QErrorMessage ;
              
                    errorMessage->showMessage( userLib.errorString() );
                }
              }
              
              void EyeCare::GetLastErrorString(LPTSTR lpszFunction)
              {    
                     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);
                     ExitProcess(dw);
              }
              

              what is a signature ?? Lol

              J 1 Reply Last reply 2 Sept 2016, 04:19
              0
              • A Ahti
                1 Sept 2016, 15:32

                @jsulm @AlterX

                This is what i get after using GetLastError function and fixing the bug in my code :

                https://postimg.org/image/tq6eabl95/

                why it says "invalid monitor handler was passed to it" even though i copied the code from the document ??

                My Code:

                void EyeCare::on_startcaringButton_clicked()
                {
                    QString style = ui->startcaringButton->styleSheet() ;
                
                    QLibrary userLib ("user32.dll") ;
                
                    if ( userLib.load() ){
                
                    HMONITOR monitorHandler = NULL ;
                    DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
                    curBrightnessLevel = 0 ;
                    HWND windowHandler ;    
                
                    DWORD numberOfMonitors = 1 ;
                    LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
                
                    windowHandler = GetDesktopWindow() ;
                    monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
                
                    BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
                
                    if (bSuccess) {
                
                        pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                                  numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                        if (pointerToPhysicalMonitors != NULL) {
                
                            bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                        monitorHandler,numberOfMonitors,
                                        pointerToPhysicalMonitors) ;
                
                            DWORD MonitorCapabilities = 0, SupportedColorTemperature = 0 ;
                
                            if (GetMonitorCapabilities(monitorHandler, &MonitorCapabilities, &SupportedColorTemperature)) {
                
                              if (GetMonitorBrightness(windowHandler,&minBrightnessLevel,
                                         &curBrightnessLevel, &maxBrightnessLevel) ){
                                  DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                                  SetMonitorBrightness(monitorHandler,newBrightnessLevel) ;
                                  SaveCurrentMonitorSettings(monitorHandler) ;
                
                                  style.append("color: green ;") ;
                              }else{
                                  style.append("color: red ;") ;
                                  GetLastErrorString(TEXT("GetMonitorBrightness")) ;
                              }
                            }else{
                                style.append("color: purple ;") ;
                                GetLastErrorString(TEXT("GetMonitorCapabilities")) ;
                            }
                          }else {
                              style.append("color: orange ;") ;
                              GetLastErrorString(TEXT("PhysicalMonitor")) ;
                          }
                      }else{
                          style.append("color: yellow ;") ;
                          GetLastErrorString(TEXT("GetNumberOfPhysicalMonitorsFromHMONITOR")) ;
                      }
                    ui->startcaringButton->setStyleSheet (style) ;
                
                
                  }else{
                
                      QErrorMessage *errorMessage = new QErrorMessage ;
                
                      errorMessage->showMessage( userLib.errorString() );
                  }
                }
                
                void EyeCare::GetLastErrorString(LPTSTR lpszFunction)
                {    
                       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);
                       ExitProcess(dw);
                }
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 2 Sept 2016, 04:19 last edited by
                #14

                @Ahti said in Why GetMonitorBrightness function is returning false in the following code ?:

                MonitorFromWindow

                You should check what GetDesktopWindow and MonitorFromWindow returned.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 2 Sept 2016, 15:05
                0
                • J jsulm
                  2 Sept 2016, 04:19

                  @Ahti said in Why GetMonitorBrightness function is returning false in the following code ?:

                  MonitorFromWindow

                  You should check what GetDesktopWindow and MonitorFromWindow returned.

                  A Offline
                  A Offline
                  Ahti
                  wrote on 2 Sept 2016, 15:05 last edited by
                  #15

                  @jsulm @AlterX

                  I have fixed the invalid handle problem just like the person here did solved it

                  http://stackoverflow.com/questions/14214290/invalid-monitor-handle-error-when-executing-setmonitorbrightness-function-in-win

                  but now i get this error message :

                  https://postimg.org/image/y7gwif7r5/

                  and GetDesktopWindow never fails like the document says:

                  https://postimg.org/image/t2kslkg6t/

                  and for the MonitorFromWindow the document says it depends on the second parameter if you have only one monitor ( and that is what i have ). And it also says that if the second parameter is MONITOR_DEFAULTTOPRIMARY ( that is what i have set in the code ) it will return a handle to the primary display monitor.
                  so there is no need to check but i am still checking ( as you can see in the code )

                  so the question is how can i fix that new error message i get (
                  https://postimg.org/image/y7gwif7r5/ ) ?

                  My Code:

                  void EyeCare::on_startcaringButton_clicked()
                  {
                      QString style = ui->startcaringButton->styleSheet() ;
                  
                      QLibrary userLib ("user32.dll") ;
                  
                      if ( userLib.load() ){
                  
                      HMONITOR monitorHandler = NULL ;
                      DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
                      curBrightnessLevel = 0 ;
                      HWND windowHandler ;
                  
                      DWORD numberOfMonitors = 1 ;
                      LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
                  
                      windowHandler = GetDesktopWindow() ;
                      monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
                  
                      if (monitorHandler == NULL) {
                  
                          QErrorMessage *showError = new QErrorMessage ;
                          showError->showMessage("Failed to get the monitor handle!");
                          return ;
                      }else {
                  
                      BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
                  
                      if (bSuccess) {
                  
                          pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                                    numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                          if (pointerToPhysicalMonitors != NULL) {
                  
                              bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                          monitorHandler,numberOfMonitors,
                                          pointerToPhysicalMonitors) ;
                  
                              HANDLE handle = pointerToPhysicalMonitors[0].hPhysicalMonitor ;
                  
                              DWORD MonitorCapabilities = 0, SupportedColorTemperature = 0 ;
                  
                              if (GetMonitorCapabilities(handle, &MonitorCapabilities, &SupportedColorTemperature)) {
                  
                                if (GetMonitorBrightness(handle,&minBrightnessLevel,
                                           &curBrightnessLevel, &maxBrightnessLevel) ){
                                    DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                                    SetMonitorBrightness(handle,newBrightnessLevel) ;
                                    SaveCurrentMonitorSettings(handle) ;
                  
                                    style.append("color: green ;") ;
                                }else{
                                    style.append("color: red ;") ;
                                    GetLastErrorString(TEXT("GetMonitorBrightness")) ;
                                }
                              }else{
                                  style.append("color: purple ;") ;
                                  GetLastErrorString(TEXT("GetMonitorCapabilities")) ;
                              }
                            }else {
                                style.append("color: orange ;") ;
                                GetLastErrorString(TEXT("PhysicalMonitor")) ;
                            }
                        }else{
                            style.append("color: yellow ;") ;
                            GetLastErrorString(TEXT("GetNumberOfPhysicalMonitorsFromHMONITOR")) ;
                        }
                      ui->startcaringButton->setStyleSheet (style) ;
                  
                  
                    }
                    }else{
                  
                        QErrorMessage *errorMessage = new QErrorMessage ;
                  
                        errorMessage->showMessage( userLib.errorString() );
                    }
                  }
                  
                  void EyeCare::GetLastErrorString(LPTSTR lpszFunction)
                  {
                         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);
                         ExitProcess(dw);
                  }
                  
                  

                  what is a signature ?? Lol

                  A 1 Reply Last reply 2 Sept 2016, 15:40
                  0
                  • A Ahti
                    2 Sept 2016, 15:05

                    @jsulm @AlterX

                    I have fixed the invalid handle problem just like the person here did solved it

                    http://stackoverflow.com/questions/14214290/invalid-monitor-handle-error-when-executing-setmonitorbrightness-function-in-win

                    but now i get this error message :

                    https://postimg.org/image/y7gwif7r5/

                    and GetDesktopWindow never fails like the document says:

                    https://postimg.org/image/t2kslkg6t/

                    and for the MonitorFromWindow the document says it depends on the second parameter if you have only one monitor ( and that is what i have ). And it also says that if the second parameter is MONITOR_DEFAULTTOPRIMARY ( that is what i have set in the code ) it will return a handle to the primary display monitor.
                    so there is no need to check but i am still checking ( as you can see in the code )

                    so the question is how can i fix that new error message i get (
                    https://postimg.org/image/y7gwif7r5/ ) ?

                    My Code:

                    void EyeCare::on_startcaringButton_clicked()
                    {
                        QString style = ui->startcaringButton->styleSheet() ;
                    
                        QLibrary userLib ("user32.dll") ;
                    
                        if ( userLib.load() ){
                    
                        HMONITOR monitorHandler = NULL ;
                        DWORD minBrightnessLevel = 0, maxBrightnessLevel = 0,
                        curBrightnessLevel = 0 ;
                        HWND windowHandler ;
                    
                        DWORD numberOfMonitors = 1 ;
                        LPPHYSICAL_MONITOR pointerToPhysicalMonitors = NULL ;
                    
                        windowHandler = GetDesktopWindow() ;
                        monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;
                    
                        if (monitorHandler == NULL) {
                    
                            QErrorMessage *showError = new QErrorMessage ;
                            showError->showMessage("Failed to get the monitor handle!");
                            return ;
                        }else {
                    
                        BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler,&numberOfMonitors) ;
                    
                        if (bSuccess) {
                    
                            pointerToPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
                                      numberOfMonitors* sizeof(PHYSICAL_MONITOR) ) ;
                            if (pointerToPhysicalMonitors != NULL) {
                    
                                bSuccess = GetPhysicalMonitorsFromHMONITOR(
                                            monitorHandler,numberOfMonitors,
                                            pointerToPhysicalMonitors) ;
                    
                                HANDLE handle = pointerToPhysicalMonitors[0].hPhysicalMonitor ;
                    
                                DWORD MonitorCapabilities = 0, SupportedColorTemperature = 0 ;
                    
                                if (GetMonitorCapabilities(handle, &MonitorCapabilities, &SupportedColorTemperature)) {
                    
                                  if (GetMonitorBrightness(handle,&minBrightnessLevel,
                                             &curBrightnessLevel, &maxBrightnessLevel) ){
                                      DWORD newBrightnessLevel = maxBrightnessLevel - 50 ;
                                      SetMonitorBrightness(handle,newBrightnessLevel) ;
                                      SaveCurrentMonitorSettings(handle) ;
                    
                                      style.append("color: green ;") ;
                                  }else{
                                      style.append("color: red ;") ;
                                      GetLastErrorString(TEXT("GetMonitorBrightness")) ;
                                  }
                                }else{
                                    style.append("color: purple ;") ;
                                    GetLastErrorString(TEXT("GetMonitorCapabilities")) ;
                                }
                              }else {
                                  style.append("color: orange ;") ;
                                  GetLastErrorString(TEXT("PhysicalMonitor")) ;
                              }
                          }else{
                              style.append("color: yellow ;") ;
                              GetLastErrorString(TEXT("GetNumberOfPhysicalMonitorsFromHMONITOR")) ;
                          }
                        ui->startcaringButton->setStyleSheet (style) ;
                    
                    
                      }
                      }else{
                    
                          QErrorMessage *errorMessage = new QErrorMessage ;
                    
                          errorMessage->showMessage( userLib.errorString() );
                      }
                    }
                    
                    void EyeCare::GetLastErrorString(LPTSTR lpszFunction)
                    {
                           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);
                           ExitProcess(dw);
                    }
                    
                    
                    A Offline
                    A Offline
                    Ahti
                    wrote on 2 Sept 2016, 15:40 last edited by
                    #16

                    @jsulm @AlterX

                    I was wondering what if i use this class :

                    https://msdn.microsoft.com/library/windows/apps/windows.devices.i2c.i2cdevice.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

                    :D

                    what is a signature ?? Lol

                    J 1 Reply Last reply 5 Sept 2016, 04:21
                    0
                    • A Ahti
                      2 Sept 2016, 15:40

                      @jsulm @AlterX

                      I was wondering what if i use this class :

                      https://msdn.microsoft.com/library/windows/apps/windows.devices.i2c.i2cdevice.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

                      :D

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 5 Sept 2016, 04:21 last edited by
                      #17

                      @Ahti The error message looks more like an issue with the driver, so I don't know how to solve it.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      17/17

                      5 Sept 2016, 04:21

                      • Login

                      • Login or register to search.
                      17 out of 17
                      • First post
                        17/17
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved