The three libraries documented in this chapter turn Ruby into a powerful and convenient Windows scripting language. Now you have the power to control your applications, but in a controlled, object-oriented environment.
require 'win32ole'
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true
ie.gohome
WIN32OLE
provides a client interface to Windows 32 OLE Automation servers. See the tutorial description “Windows Automation” for more information.
WIN32OLE::VERSION |
Current version number |
nil
nil
nil
IEnumVARIANT
interface.Hash
of named parameters and values. You don't need to call invoke
explicitly; this class uses method_missing
to forward calls through invoke
, so you can simply use the OLE methods as methods of this class.This (slightly modified) example from the Win32OLE 0.1.1 distribution shows the use of an event sink.
require 'win32ole'
$urls = []
def navigate(url)
$urls << url
end
def stop_msg_loop
puts "IE has exited..."
throw :done
end
def default_handler(event, *args)
case event
when "BeforeNavigate"
puts "Now Navigating to #{args[0]}..."
end
end
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = TRUE
ie.gohome
ev = WIN32OLE_EVENT.new(ie, 'DWebBrowserEvents')
ev.on_event {|*args| default_handler(*args)}
ev.on_event("NavigateComplete") {|url| navigate(url)}
ev.on_event("Quit") {|*args| stop_msg_loop}
catch(:done) {
loop {
WIN32OLE_EVENT.message_loop
}
}
puts "You Navigated to the following URLs: "
$urls.each_with_index do |url, i|
puts "(#{i+1}) #{url}"
end
WIN32OLE_EVENT
is used in conjunction with the WIN32OLE
class to add callbacks for Windows 32 events.
nil
WIN32OLE_EVENT
(an event sink) for the given WIN32OLE
object and named event source. If aName is nil
, it will attempt to use the default source and will raise a RuntimeError
if it cannot find one.nil
nil
, then this callback is associated with all events. The block will be given any arguments appropriate for this event.This example is from the Ruby distribution, in ext/Win32API
:
require 'Win32API'
getCursorPos = Win32API.new("user32", "GetCursorPos", ['P'], 'V')
lpPoint = " " * 8 # store two LONGs
getCursorPos.Call(lpPoint)
x, y = lpPoint.unpack("LL") # get the actual values
print "x: ", x, "\n"
print "y: ", y, "\n"
ods = Win32API.new("kernel32", "OutputDebugString", ['P'], 'V')
ods.Call("Hello, World\n")
GetDesktopWindow = Win32API.new("user32", "GetDesktopWindow", [], 'L')
GetActiveWindow = Win32API.new("user32", "GetActiveWindow", [], 'L')
SendMessage = Win32API.new("user32", "SendMessage", ['L'] * 4, 'L')
SendMessage.Call(GetDesktopWindow.Call, 274, 0xf140, 0)
The Win32API
module allows access to any arbitrary Windows 32 function. Many of these functions take or return a Pointer
datatype—a region of memory corresponding to a C string or structure type.
In Ruby, these pointers are represented using class String
, which contains a sequence of 8-bit bytes. It is up to you to pack and unpack the bits in the String
. See the reference sections for String#unpack
and Array#pack
for details.
Returns a new object representing a Windows 32 API function. dllname is the name of the DLL containing the function, such as “user32” or “kernel32.” procname is the name of the desired function. importArray is an array of strings representing the types of arguments to the function.
export is a string representing the return type of the function. Strings “n” and “l” represent numbers, “i” represent integers, “p” represents pointers to data stored in a string, and “v” represents a void type (used for export parameters only). These strings are case-insensitive.
new
.Win32API#call
.Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.