Reputation: 55
I'm trying to set up a small programm to adjust Monitor Brightness by the current room brightness.
I followed the instructions from MSDN and set up this:
cout << "Legen Sie das Fenster bitte auf den zu steuernden Monitor.\n";
system("PAUSE");
HMONITOR hMon = NULL;
char OldConsoleTitle[1024];
char NewConsoleTitle[1024];
GetConsoleTitle(OldConsoleTitle, 1024);
SetConsoleTitle("CMDWindow7355608");
Sleep(40);
HWND hWnd = FindWindow(NULL, "CMDWindow7355608");
SetConsoleTitle(OldConsoleTitle);
hMon = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(
hMon,
&cPhysicalMonitors
);
if(bSuccess)
{
pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));
if(pPhysicalMonitors!=NULL)
{
LPDWORD min = NULL, max = NULL, current = NULL;
GetPhysicalMonitorsFromHMONITOR(hMon, cPhysicalMonitors, pPhysicalMonitors);
HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor;
if(!GetMonitorBrightness(pmh, min, current, max))
{
cout << "Fehler: " << GetLastError() << endl;
system("PAUSE");
return 0;
}
//cout << "Minimum: " << min << endl << "Aktuell: " << current << endl << "Maximum: " << max << endl;
system("PAUSE");
}
}
But the problem: everytime I try to use GetMonitorBrightness() the program crashes with Access Violation while writing at Position 0x00000000
(I translated this error from German)
While trying to debug I saw that pPhysicalMonitors actually contains the monitor I want to use, but pPhysicalMonitors[0].hPhysicalMonitor
contains 0x0000000 only. Could this be part of the problem?
Upvotes: 1
Views: 222
Reputation: 595711
everytime I try to use GetMonitorBrightness() the program crashes with Access Violation while writing at Position 0x00000000 (I translated this error from German)
You are passing NULL pointers to GetMonitorBrightness()
, so it crashes when trying to write its output values to invalid memory.
Just like GetNumberOfPhysicalMonitorsFromHMONITOR()
, GetMonitorBrightness()
expects you to pass the address of actual variables, eg:
DWORD min, max, current;
if (!GetMonitorBrightness(pmh, &min, ¤t, &max))
While trying to debug I saw that pPhysicalMonitors actually contains the monitor I want to use, but pPhysicalMonitors[0].hPhysicalMonitor contains 0x0000000 only. Could this be part of the problem?
No. However, you are not checking to make sure that cPhysicalMonitors
is > 0, and you are ignoring the return value of GetPhysicalMonitorsFromHMONITOR()
to make sure it is actually populating the PHYSICAL_MONITOR
array with data.
Upvotes: 2