April 26, 2008

Call Windows API in Ruby

Sample Code:
# Close WMP by Window Message
require 'Win32API'

FindWindow = Win32API.new('user32', 'FindWindow', ["P", "P"], "L")
SendMessage = Win32API.new('user32', 'SendMessage', ["L", "L", "P", "P"], "L")
WM_CLOSE = 0x0010

def CloseWMP()
  hwnd = FindWindow.call(nil, "Windows Media Player")
  if hwnd != 0 then
    id = SendMessage.call(hwnd, WM_CLOSE, nil, nil)
  end
end
# End of Close WMP by Window Message


Reference:
http://www.rubycentral.com/pickaxe/lib_windows.html