Ruby WinAPI Test


SUBMITTED BY: Guest

DATE: May 21, 2015, 9:48 a.m.

FORMAT: Ruby

SIZE: 4.6 kB

HITS: 4183

  1. require 'Win32API'
  2. require 'irb'
  3. require 'optparse'
  4. #require 'rdoc/usage'
  5. require 'zlib'
  6. # From winuser.h
  7. #http://doc.ddart.net/msdn/header/include/winuser.h.html
  8. WM_ENABLE = 0x000A
  9. WM_PAINT = 0x000F
  10. WM_KEYDOWN = 0x0100
  11. WM_KEYUP = 0x0101
  12. WM_CHAR = 0x0102
  13. WM_USER = 0x0400
  14. WM_LBUTTONDOWN = 0x0201
  15. WM_LBUTTONUP = 0x0202
  16. WM_MOUSEACTIVATE = 0x0021
  17. WM_NCPAINT=0x0085
  18. RDW_INVALIDATE = 0x0001
  19. RDW_INTERNALPAINT = 0x0002
  20. GWL_STYLE = -16
  21. WS_DISABLED = 0x08000000
  22. EnableWindow = Win32API.new("user32", "EnableWindow", 'LL', 'L')
  23. SendMessage = Win32API.new("user32","SendMessage", ['L'] * 4, 'L')
  24. RedrawWindow = Win32API.new("user32","RedrawWindow", ['L'] * 4, 'L')
  25. FindWindowEx = Win32API.new("user32","FindWindowEx", 'LLPP','L')
  26. IsWindowEnabled = Win32API.new("user32","IsWindowEnabled",'L','L')
  27. UpdateWindow = Win32API.new("user32","UpdateWindow",'L','L')
  28. GetWindowLong = Win32API.new("user32", "GetWindowLong", 'LL','L')
  29. SetWindowLong = Win32API.new("user32", "SetWindowLong", 'LLL','L')
  30. def click_button(sm, button)
  31. sm.call(button, 0x0021, 1, 513)
  32. sm.call(button,0x0201,0,0x000A0021)
  33. sm.call(button,0x202,0,0x000A0021)
  34. end
  35. # Open our backpack...
  36. # Made with: puts [File::open("winuser_test.cpp.gz").read()].pack('u')
  37. def displayC()
  38. stuff = DATA.read()
  39. thingz = stuff.unpack('u')
  40. gz = Zlib::GzipReader.new(StringIO.new(thingz.join())) #trick to convert string
  41. #to input stream
  42. thing = gz.read()
  43. puts thing
  44. end
  45. options={}
  46. options[:e] = false
  47. opts = OptionParser.new()
  48. opts.on("-h", "--help", "You're looking at it."){puts opts.to_s;Kernel.exit(0)}
  49. opts.on("-e", "--extract", "Display a C example of user32 interaction."){|blah| options[:e] = true}
  50. opts.parse(ARGV) rescue puts opts.to_s
  51. if options[:e] then
  52. displayC()
  53. Kernel.exit(0)
  54. end
  55. puts "Finding the Start Menu..."
  56. sleep(1)
  57. startbar = FindWindowEx.call(0,0,"Shell_TrayWnd",0)
  58. puts "Finding Start Button..."
  59. start_button = FindWindowEx.call(startbar,0,0,"start")
  60. click_button(SendMessage, start_button)
  61. puts("I cant find what I am looking for there, so I am launching the Calculator.")
  62. sleep(1)
  63. click_button(SendMessage, start_button)
  64. crap = IO.popen('calc.exe')
  65. sleep(2)
  66. puts("Finding Calculator window...")
  67. calculator = FindWindowEx.call(0,0,"SciCalc",0)
  68. #edit_area = FindWindowEx.call(notepad,0,0,"Edit")
  69. #calc_win = FindWindowEx.call(0,0,0,"Calculator")
  70. #= FindWindowEx.call(anchor,0,0,"&Cancel")
  71. #ok = FindWindowEx.call(anchor,0,0,"&OK")
  72. #password = FindWindowEx.call(0,0,0,"&Password:")
  73. #= GetWindowLong.call(GWL_STYLE)
  74. #cancel_style |= WS_DISABLED #If the window style does not have WS_DISABLED, this sets it.
  75. #SetWindowLong.call(cancel, GWL_STYLE, cancel_style)
  76. #EnableWindow(cancel, 0) #This makes the button unclickable but not greyed out.
  77. #EnableWindow(cancel,1) #This makes the button clickable again, but doesnt change color.
  78. #ok_style = GetWindowLong.call(ok, GWL_STYLE)
  79. #ok_style &= ~WS_DISABLED #If the window style has WM_DISABLED this unsets this style.
  80. #SetWindowLong.call(ok, GWL_STYLE, ok_style)
  81. one = FindWindowEx.call(calculator, 0,0,"1")
  82. five = FindWindowEx.call(calculator, 0,0,"5")
  83. six = FindWindowEx.call(calculator, 0,0,"6")
  84. eight = FindWindowEx.call(calculator, 0,0,"8")
  85. two = FindWindowEx.call(calculator, 0,0,"2")
  86. times = FindWindowEx.call(calculator, 0,0,"*")
  87. plus = FindWindowEx.call(calculator, 0,0,"+")
  88. equal = FindWindowEx.call(calculator, 0,0,"=")
  89. c = FindWindowEx.call(calculator, 0,0,"C")
  90. ce = FindWindowEx.call(calculator, 0,0,"CE")
  91. click_button(SendMessage, one)
  92. sleep(1)
  93. click_button(SendMessage, five)
  94. sleep(1)
  95. click_button(SendMessage, six)
  96. sleep(1)
  97. click_button(SendMessage, six)
  98. sleep(1)
  99. click_button(SendMessage, eight)
  100. sleep(1)
  101. click_button(SendMessage, times)
  102. sleep(1)
  103. click_button(SendMessage, two)
  104. sleep(1)
  105. click_button(SendMessage, plus)
  106. sleep(1)
  107. click_button(SendMessage, one)
  108. sleep(1)
  109. click_button(SendMessage, equal)
  110. EnableWindow.call(c, 0)
  111. EnableWindow.call(ce, 0)
  112. puts "Try to click the 'C' or 'CE' buttons..."
  113. sleep(5)
  114. puts "Ok hit <ENTER> to continue.";gets()
  115. EnableWindow.call(c, 1)
  116. EnableWindow.call(ce, 1)
  117. puts "ok you get the idea...quitting."
  118. Kernel.exit(0)
  119. __END__

comments powered by Disqus