2015-12-04 10 views
7

Ich versuche, meine Monitorhelligkeit programmgesteuert anzupassen. Nach ein wenig Recherche, kam ich auf diese link und schrieb den folgenden Code (meist kopiere Paste von anderen Links, die mich führen).Verwendung von GetMonitorCapabilities und GetMonitorBrightness-Funktionen

#include "Windows.h" 
#include "WinUser.h" 
#include "PhysicalMonitorEnumerationAPI.h" 
#include "HighLevelMonitorConfigurationAPI.h" 
#include <strsafe.h> 

void ShowError(LPTSTR lpszFunction); 

int main() 
{ 
    HMONITOR hMonitor = NULL; 
    DWORD cPhysicalMonitors; 
    LPPHYSICAL_MONITOR pPhysicalMonitors = NULL; 

    HWND hWnd = GetDesktopWindow(); 

    // Get the monitor handle. 
    hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY); 

    // Get the number of physical monitors. 
    BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors); 

    if (bSuccess) 
    { 
     // Allocate the array of PHYSICAL_MONITOR structures. 
     pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors* sizeof(PHYSICAL_MONITOR)); 

     if (pPhysicalMonitors != NULL) 
     { 
      // Get the array. 
      bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors); 

      // Get physical monitor handle. 
      HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor; 

      LPDWORD pdwMinimumBrightness = NULL; 
      LPDWORD pdwCurrentBrightness = NULL; 
      LPDWORD pdwMaximumBrightness = NULL; 
      bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness); 
      if (bSuccess == FALSE) 
      { 
       ShowError(TEXT("GetMonitorBrightness")); 
      } 

      // Close the monitor handles. 
      bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors); 

      // Free the array. 
      free(pPhysicalMonitors); 
     } 
    } 
    return 0; 
} 

void ShowError(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code 
    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); 
} 

Dieser Code stürzt ab, wenn diese Zeile ausführen:

bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness); 

Laut Dokumentation kann diese Funktion nicht unterstützt.

If this function is supported, the GetMonitorCapabilities function returns the MC_CAPS_BRIGHTNESS flag.

Also, um das zu überprüfen, füge ich den folgenden Block zu meinem Code, kurz vor GetMonitorBrightness aufrufen.

LPDWORD pdwMonitorCapabilities = NULL; 
LPDWORD pdwSupportedColorTemperatures = NULL; 
bSuccess = GetMonitorCapabilities(hPhysicalMonitor, pdwMonitorCapabilities, pdwSupportedColorTemperatures); 
if (bSuccess == FALSE) 
{ 
    ShowError(TEXT("GetMonitorCapabilities")); 
} 

Leider nachdem ich, dass Block hinzugefügt, erhielt ich folgende Fehlermeldung:

enter image description here

Wieder nach documentation, GetMonitorCapabilities Funktion schlägt fehl, wenn der Monitor nicht DDC/CI unterstützt.

Dann habe ich überprüft, ob mein Monitor DDC/CI unterstützt, und herausgefunden, dass es ist. Wenn ich die DDC/CI-Unterstützung manuell von den Monitoreinstellungen deaktiviere, schaltet die vorherige Fehlermeldung auf die folgende um, so dass ich jetzt ziemlich sicher bin mein Monitor hat DDC/CI-Unterstützung.

enter image description here

Ich fühle mich wie ich alles richtig tue aber anscheinend bin ich nicht. Kurz gesagt, GetMonitorCapabilities Funktion schlägt mit einer Fehlermeldung, die ich keine Bedeutung geben kann, und GetMonitorBrightness Funktion wird abgestürzt.

Hinweise:

Mein Monitor ist Dell U2713H.

Ich bin auf dem 64-Bit-Windows-7

Ich bin mit Microsoft Visual C++ Compiler 12.0 (x86)

Antwort

6

Ihre Anrufe GetMonitorBrightness() und GetMonitorCapabilities() falsch sind. Sie passieren NULL-Zeiger, aber sie erwarten Zeiger auf tatsächliche DWORD Variablen statt:

DWORD dwMinimumBrightness = 0; 
DWORD dwCurrentBrightness = 0; 
DWORD dwMaximumBrightness = 0; 
bSuccess = GetMonitorBrightness(hPhysicalMonitor, &dwMinimumBrightness, &dwCurrentBrightness, &dwMaximumBrightness); 

DWORD dwMonitorCapabilities = 0; 
DWORD dwSupportedColorTemperatures = 0; 
bSuccess = GetMonitorCapabilities(hPhysicalMonitor, &dwMonitorCapabilities, &dwSupportedColorTemperatures); 
+0

Wow, Anfängerfehler. Vielen Dank. Die Dokumentation sagte "LPDWORD", das hat mich verwirrt. Es funktioniert jetzt perfekt. – guneykayim

+0

Die Dokumentation sagt auch, dass sie '_Out_' Parameter sind, also müssen sie Ausgabe * auf etwas * schreiben. NULLs sind nicht erlaubt, es sei denn sie sind '_Out_Opt_'. Siehe [SAL-Anmerkungen] (https://msdn.microsoft.com/library/ms235402.aspx). –

+0

Ja, mein Schlechter. wie gesagt, Anfängerfehler. – guneykayim

Verwandte Themen