require 'Win32API'
require 'irb'
require 'optparse'
#require 'rdoc/usage'
require 'zlib'
# From winuser.h
#http://doc.ddart.net/msdn/header/include/winuser.h.html
WM_ENABLE = 0x000A
WM_PAINT = 0x000F
WM_KEYDOWN = 0x0100
WM_KEYUP = 0x0101
WM_CHAR = 0x0102
WM_USER = 0x0400
WM_LBUTTONDOWN = 0x0201
WM_LBUTTONUP = 0x0202
WM_MOUSEACTIVATE = 0x0021
WM_NCPAINT=0x0085
RDW_INVALIDATE = 0x0001
RDW_INTERNALPAINT = 0x0002
GWL_STYLE = -16
WS_DISABLED = 0x08000000
EnableWindow = Win32API.new("user32", "EnableWindow", 'LL', 'L')
SendMessage = Win32API.new("user32","SendMessage", ['L'] * 4, 'L')
RedrawWindow = Win32API.new("user32","RedrawWindow", ['L'] * 4, 'L')
FindWindowEx = Win32API.new("user32","FindWindowEx", 'LLPP','L')
IsWindowEnabled = Win32API.new("user32","IsWindowEnabled",'L','L')
UpdateWindow = Win32API.new("user32","UpdateWindow",'L','L')
GetWindowLong = Win32API.new("user32", "GetWindowLong", 'LL','L')
SetWindowLong = Win32API.new("user32", "SetWindowLong", 'LLL','L')
def click_button(sm, button)
sm.call(button, 0x0021, 1, 513)
sm.call(button,0x0201,0,0x000A0021)
sm.call(button,0x202,0,0x000A0021)
end
# Open our backpack...
# Made with: puts [File::open("winuser_test.cpp.gz").read()].pack('u')
def displayC()
stuff = DATA.read()
thingz = stuff.unpack('u')
gz = Zlib::GzipReader.new(StringIO.new(thingz.join())) #trick to convert string
#to input stream
thing = gz.read()
puts thing
end
options={}
options[:e] = false
opts = OptionParser.new()
opts.on("-h", "--help", "You're looking at it."){puts opts.to_s;Kernel.exit(0)}
opts.on("-e", "--extract", "Display a C example of user32 interaction."){|blah| options[:e] = true}
opts.parse(ARGV) rescue puts opts.to_s
if options[:e] then
displayC()
Kernel.exit(0)
end
puts "Finding the Start Menu..."
sleep(1)
startbar = FindWindowEx.call(0,0,"Shell_TrayWnd",0)
puts "Finding Start Button..."
start_button = FindWindowEx.call(startbar,0,0,"start")
click_button(SendMessage, start_button)
puts("I cant find what I am looking for there, so I am launching the Calculator.")
sleep(1)
click_button(SendMessage, start_button)
crap = IO.popen('calc.exe')
sleep(2)
puts("Finding Calculator window...")
calculator = FindWindowEx.call(0,0,"SciCalc",0)
#edit_area = FindWindowEx.call(notepad,0,0,"Edit")
#calc_win = FindWindowEx.call(0,0,0,"Calculator")
#= FindWindowEx.call(anchor,0,0,"&Cancel")
#ok = FindWindowEx.call(anchor,0,0,"&OK")
#password = FindWindowEx.call(0,0,0,"&Password:")
#= GetWindowLong.call(GWL_STYLE)
#cancel_style |= WS_DISABLED #If the window style does not have WS_DISABLED, this sets it.
#SetWindowLong.call(cancel, GWL_STYLE, cancel_style)
#EnableWindow(cancel, 0) #This makes the button unclickable but not greyed out.
#EnableWindow(cancel,1) #This makes the button clickable again, but doesnt change color.
#ok_style = GetWindowLong.call(ok, GWL_STYLE)
#ok_style &= ~WS_DISABLED #If the window style has WM_DISABLED this unsets this style.
#SetWindowLong.call(ok, GWL_STYLE, ok_style)
one = FindWindowEx.call(calculator, 0,0,"1")
five = FindWindowEx.call(calculator, 0,0,"5")
six = FindWindowEx.call(calculator, 0,0,"6")
eight = FindWindowEx.call(calculator, 0,0,"8")
two = FindWindowEx.call(calculator, 0,0,"2")
times = FindWindowEx.call(calculator, 0,0,"*")
plus = FindWindowEx.call(calculator, 0,0,"+")
equal = FindWindowEx.call(calculator, 0,0,"=")
c = FindWindowEx.call(calculator, 0,0,"C")
ce = FindWindowEx.call(calculator, 0,0,"CE")
click_button(SendMessage, one)
sleep(1)
click_button(SendMessage, five)
sleep(1)
click_button(SendMessage, six)
sleep(1)
click_button(SendMessage, six)
sleep(1)
click_button(SendMessage, eight)
sleep(1)
click_button(SendMessage, times)
sleep(1)
click_button(SendMessage, two)
sleep(1)
click_button(SendMessage, plus)
sleep(1)
click_button(SendMessage, one)
sleep(1)
click_button(SendMessage, equal)
EnableWindow.call(c, 0)
EnableWindow.call(ce, 0)
puts "Try to click the 'C' or 'CE' buttons..."
sleep(5)
puts "Ok hit <ENTER> to continue.";gets()
EnableWindow.call(c, 1)
EnableWindow.call(ce, 1)
puts "ok you get the idea...quitting."
Kernel.exit(0)
__END__