I'm trying to create a C++ console application which will automatically adjust my monitor's brightness based on the average brightness of pixels being displayed on my screen. Unfortunately before I've even gotten to the second part I'm having trouble even using the DXVA library.
Here is my current code (which I've taken some of from here: How to use GetMonitorCapabilities and GetMonitorBrightness functions):
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>
#include <strsafe.h>
std::string GetLastErrorAsString()
{
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string();
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
LocalFree(messageBuffer);
return message;
}
int main()
{
HWND hWnd = GetDesktopWindow();
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
DWORD cPhysicalMonitors = 0;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
if (!bSuccess) {
std::cout << "An error occured: " << GetLastErrorAsString().c_str() << std::endl;
}
LPPHYSICAL_MONITOR pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));
if (bSuccess)
{
if (pPhysicalMonitors != NULL)
{
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
if (!bSuccess) {
std::cout << "An error occured: " << GetLastErrorAsString().c_str() << std::endl;
}
HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;
DWORD dwMinimumBrightness = 0;
DWORD dwCurrentBrightness = 0;
DWORD dwMaximumBrightness = 0;
DWORD dwMonitorCapabilities = 0;
DWORD dwSupportedColorTemperatures = 0;
bSuccess = GetMonitorCapabilities(hPhysicalMonitor, &dwMonitorCapabilities, &dwSupportedColorTemperatures);
if (bSuccess) {
std::cout << "Capabilities: " << dwMonitorCapabilities << std::endl << "Supported color temperatures: " << dwSupportedColorTemperatures << std::endl;
}
else {
std::cout << "An error occured while getting monitor capabilities: " << GetLastErrorAsString().c_str() << std::endl;
}
bSuccess = GetMonitorBrightness(hPhysicalMonitor, &dwMinimumBrightness, &dwCurrentBrightness, &dwMaximumBrightness);
if (bSuccess) {
std::cout << "Minimum brightness: " << dwMinimumBrightness << std::endl << "Maximum brightness: " << dwMaximumBrightness << std::endl << "Current brightness: " << dwCurrentBrightness << std::endl;
}
else {
std::cout << "An error occured while getting brightness: " << GetLastErrorAsString().c_str() << std::endl;
}
bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);
free(pPhysicalMonitors);
}
}
return 0;
}
The error I am getting for the brightness/capability functions is this: An operation failed because a DDC/CI message had an invalid value in its command field.
I've been Googling for hours and I have yet to find some sort of fix for my problem. I have an AMD Graphics card and I am using the built in drivers (Google Lenovo e545 AMD drivers) which include Catalyst version 15.7.1
, Direct3D 9.14.10.01128
, and the driver version is 15.20.1062.1004-150803a1-187674C
.
I apologize for the messy question and my lack of experience.