Skip to content

Instantly share code, notes, and snippets.

@JavierJF
Last active September 27, 2017 15:09
Show Gist options
  • Save JavierJF/37a195a8b14118b729c09ef23c3d2c50 to your computer and use it in GitHub Desktop.
Save JavierJF/37a195a8b14118b729c09ef23c3d2c50 to your computer and use it in GitHub Desktop.
Get current windows brightness in Windows (DDC/CI)
#include <Windows.h>
#include <PhysicalMonitorEnumerationAPI.h>
#include <HighLevelMonitorConfigurationAPI.h>
#include <iostream>
#include <cstdint>
#include <string>
// Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if(errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorMessageID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&messageBuffer,
0,
NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
int main(int argc, char* argv[]) {
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
// Get the monitor handle.
hMonitor = MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY);
std::cout << "Virtual Monitor handle: " << (uint64_t)hMonitor << std::endl;
// 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
);
if (!bSuccess) {
std::cout << "Error getting physical monitor from HMONITOR" << std::endl;
goto clean;
}
{
std::cout << "Physical Monitor handle: " << (uint64_t)pPhysicalMonitors->hPhysicalMonitor << std::endl;
std::wcout << L"Physical Monitor Desc: " << pPhysicalMonitors->szPhysicalMonitorDescription << std::endl;
}
DWORD mCaps;
DWORD supColorTemps;
auto mCapsRes =
GetMonitorCapabilities(
pPhysicalMonitors[0].hPhysicalMonitor,
&mCaps,
&supColorTemps
);
if (mCapsRes == FALSE) {
auto err = GetLastErrorAsString();
std::cout << "Error retreiving monitor capabilities: " << err << std::endl;
goto clean;
}
if ((mCaps & MC_CAPS_BRIGHTNESS) == 0) {
std::cout << "Error: Monitor doesn't support brightness" << std::endl;
goto clean;
}
DWORD curBright = 0;
DWORD minBright = 0;
DWORD maxBright = 0;
auto mBrRes =
GetMonitorBrightness(
pPhysicalMonitors[0].hPhysicalMonitor,
&minBright,
&curBright,
&maxBright
);
// Use the monitor handles (not shown).
std::cout << "Min Monitor brightness: " << minBright << std::endl;
std::cout << "Cur Monitor brightness: " << curBright << std::endl;
std::cout << "Max Monitor brightness: " << maxBright << std::endl;
clean:
// Close the monitor handles.
bSuccess = DestroyPhysicalMonitors(
cPhysicalMonitors,
pPhysicalMonitors);
// Free the array.
free(pPhysicalMonitors);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment