Skip to content

Instantly share code, notes, and snippets.

@aasmith
Last active October 19, 2020 02:58
Show Gist options
  • Save aasmith/4315549 to your computer and use it in GitHub Desktop.
Save aasmith/4315549 to your computer and use it in GitHub Desktop.
Turns off all monitors in Windows using the Win32 API.
# Send all monitors to power saving mode (standby) using the WIN32 API.
#
# Documentation for WM_SYSCOMMAND & SC_MONITORPOWER:
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx
require 'fiddle'
require 'fiddle/import'
require 'fiddle/types'
module User32
extend Fiddle::Importer
dlload 'user32'
include Fiddle::Win32Types
extern 'ULONG SendMessage(HWND, UINT, UINT, ULONG)'
end
HWND_BROADCAST = 0xFFFF
WM_SYSCOMMAND = 0x0112
SC_MONITORPOWER = 0xf170
POWER_LOW = 1
POWER_OFF = 2
User32.SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, POWER_OFF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment