Skip to content

Instantly share code, notes, and snippets.

@sherief
Forked from 3735943886/SetMonitorPower.c
Created August 4, 2023 17:26
Show Gist options
  • Save sherief/d7ba6a8d8ed82984b6206bf2b97986ba to your computer and use it in GitHub Desktop.
Save sherief/d7ba6a8d8ed82984b6206bf2b97986ba to your computer and use it in GitHub Desktop.
Turn On / Off display monitor
/* dependencies */
#pragma comment(lib, "dxva2")
#pragma comment(lib, "user32")
#include <lowlevelmonitorconfigurationapi.h>
typedef struct _MONITORPOWERPARAM
{
LPCWSTR szPhysicalMonitorDescription;
BOOL bPowerOn;
} MONITOR_POWER_PARAM, *PMONITOR_POWER_PARAM;
BOOL CALLBACK _MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM lParam)
{
UNREFERENCED_PARAMETER(hdcMonitor);
UNREFERENCED_PARAMETER(lprcMonitor);
DWORD dwMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &dwMonitorCount))
{
PHYSICAL_MONITOR* pMons;
if (pMons = HeapAlloc(GetProcessHeap(), 0, sizeof(PHYSICAL_MONITOR) * dwMonitorCount))
{
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, dwMonitorCount, pMons))
{
for (DWORD i = 0; i < dwMonitorCount; i++)
{
if (lstrcmpiW(((PMONITOR_POWER_PARAM)lParam)->szPhysicalMonitorDescription, pMons[i].szPhysicalMonitorDescription) == 0)
{
SetVCPFeature(pMons[i].hPhysicalMonitor, 0xd6, ((PMONITOR_POWER_PARAM)lParam)->bPowerOn ? 1 : 4);
}
DestroyPhysicalMonitor(pMons[i].hPhysicalMonitor);
}
}
HeapFree(GetProcessHeap(), 0, pMons);
}
}
return TRUE;
}
VOID SetMonitorPower(LPCWSTR lpszPhysicalMonitorDescription, BOOL bPowerOn)
{
MONITOR_POWER_PARAM lparam;
lparam.szPhysicalMonitorDescription = lpszPhysicalMonitorDescription;
lparam.bPowerOn = bPowerOn;
EnumDisplayMonitors(NULL, NULL, _MonitorEnumProc, (LPARAM)&lparam);
}
void main()
{
SetMonitorPower(L"Generic PnP Monitor", FALSE);
Sleep(1000);
SetMonitorPower(L"Generic PnP Monitor", TRUE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment