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 ?
Forum Updated to NodeBB v4.3 + New Features

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.6k Views 2 Watching
  • 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

    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

    AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by
    #8

    @AlterX not working it crash on first if statement :

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

    what is a signature ?? Lol

    jsulmJ 1 Reply Last reply
    0
    • AhtiA Ahti

      @AlterX not working it crash on first if statement :

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      AhtiA 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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.

        AhtiA Offline
        AhtiA Offline
        Ahti
        wrote on 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

        jsulmJ A 2 Replies Last reply
        0
        • AhtiA Ahti

          @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) ;
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          • AhtiA Ahti

            @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 last edited by AlterX
            #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/...

            AhtiA 1 Reply Last reply
            0
            • A AlterX

              @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!!!

              AhtiA Offline
              AhtiA Offline
              Ahti
              wrote on last edited by Ahti
              #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

              jsulmJ 1 Reply Last reply
              0
              • AhtiA Ahti

                @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);
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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

                AhtiA 1 Reply Last reply
                0
                • jsulmJ jsulm

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

                  MonitorFromWindow

                  You should check what GetDesktopWindow and MonitorFromWindow returned.

                  AhtiA Offline
                  AhtiA Offline
                  Ahti
                  wrote on 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

                  AhtiA 1 Reply Last reply
                  0
                  • AhtiA Ahti

                    @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);
                    }
                    
                    
                    AhtiA Offline
                    AhtiA Offline
                    Ahti
                    wrote on 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

                    jsulmJ 1 Reply Last reply
                    0
                    • AhtiA Ahti

                      @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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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