Skip to content

Instantly share code, notes, and snippets.

@vvs
Created January 16, 2010 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvs/279004 to your computer and use it in GitHub Desktop.
Save vvs/279004 to your computer and use it in GitHub Desktop.
# requiring rubygems is not needed for JRuby
require 'rubygems' unless defined?(JRUBY_VERSION)
require 'ffi'
# Simple FFI module for some low level system commands (on Windows only!):
# * Turn monitor(s) power off
# * Show the Start menu
module SysCommands
extend FFI::Library
MONITOR_OFF = 2
WM_SYSCOMMAND = 0x112
SC_MONITORPOWER = 0xF170
SC_TASKLIST = 0xF130
ffi_lib 'user32'
ffi_convention :stdcall
attach_function :FindWindow, :FindWindowA, [:pointer, :pointer], :pointer
attach_function :SendMessage, :SendMessageA, [:pointer, :int, :int, :int], :int
attach_function :lock_screen, :LockWorkStation, [], :bool
def self.turn_off
hwnd = FindWindow(nil, nil)
ret = SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF)
puts "Can't shut down the monitor: Error Status #{ret}" unless ret == 0
end
def self.start_menu
hwnd = FindWindow(nil, nil)
ret = SendMessage(hwnd, WM_SYSCOMMAND, SC_TASKLIST, 0)
puts "Can't show start menu: Error Status #{ret}" unless ret == 0
end
def self.lock_and_turn_off
lock_screen
turn_off
end
end
# SysCommands::start_menu
# SysCommands::lock_screen
# SysCommands::turn_off
SysCommands::lock_and_turn_off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment