1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman. 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/).
</copyright>
<chapter name="Microsoft Windows Support">
<p/>
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.
<p/>
<class name="WIN32OLE" super="Object" type="class">
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ require 'win32ole'
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true
ie.gohome
]]></fullcode>
require<nbsp/>'win32ole'
ie<nbsp/>=<nbsp/>WIN32OLE.new('InternetExplorer.Application')
ie.visible<nbsp/>=<nbsp/>true
ie.gohome
</alltt>
</codefragment>
<p/>
<classname>WIN32OLE</classname> provides a client interface to Windows 32 OLE
Automation servers. See the tutorial description on page 166
for more information.
<p/>
<constants>
<tr>
<td><constant>
<constname>WIN32OLE::VERSION</constname>
<constval></constval>
<constdesc><em>Current version number</em></constdesc>
</constant>
</td>
</tr>
</constants>
<p/>
<methods type="class">
<p/>
<method name="connect" ref="connect">
<callseq>
WIN32OLE.connect( <obj>aString</obj> )
<returns><obj>wapi</obj></returns>
</callseq>
<desc>
<p/>
Returns a new OLE automation client connected to an existing instance
of the named automation server.
<p/>
</desc>
</method>
<p/>
<method name="const_load" ref="const_load">
<callseq>
WIN32OLE.const_load( <obj>wapi</obj>,
<opt><obj>aClass=WIN32OLE</obj></opt> )
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Defines the constants from the specified automation server
as class constants in <obj>aClass</obj>.
<p/>
</desc>
</method>
<p/>
<method name="new" ref="new">
<callseq>
WIN32OLE.new( <obj>aString</obj> )
<returns><obj>wapi</obj></returns>
</callseq>
<desc>
<p/>
Returns a new OLE automation client connected to a new instance
of the automation server named by <obj>aString</obj>.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
<methods type="instance">
<p/>
<method name="[ ]" ref="_ob_cb">
<callseq>
<obj>wapi</obj>[ <obj>aString</obj> ]
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Returns the named property from the OLE automation object.
<p/>
</desc>
</method>
<p/>
<method name="[ ]=" ref="_ob_cb_eq">
<callseq>
<obj>wapi</obj>[ <obj>aString</obj> ] = <obj>aValue</obj>
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Sets the named property in the OLE automation object.
<p/>
</desc>
</method>
<p/>
<method name="each" ref="each">
<callseq>
<obj>wapi</obj>.each <block>{| anObj | <blockbody>block</blockbody> }</block>
<p/>
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Iterates over each item of this OLE server that supports the
<tt>IEnumVARIANT</tt> interface.
<p/>
</desc>
</method>
<p/>
<method name="invoke" ref="invoke">
<callseq>
<obj>wapi</obj>.invoke ( <obj>aCmdString</obj>,
<optz><obj>args</obj></optz> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Invokes the command given in <obj>aCmdString</obj> with the given
<obj>args</obj>. <obj>args</obj> may be a <classname>Hash</classname> of named parameters
and values. You don't need to call <tt>invoke</tt> explicitly; this
class uses <tt>method_missing</tt> to forward calls through
<tt>invoke</tt>, so you can simply use the OLE methods as methods of
this class.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
<p/>
<class name="WIN32OLE_EVENT" super="Object" type="class">
<p/>
This (slightly modified) example from the Win32OLE 0.1.1
distribution shows the use of an event sink.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ 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
]]></fullcode>
require<nbsp/>'win32ole'
<p/>
$urls<nbsp/>=<nbsp/>[]
<p/>
def<nbsp/>navigate(url)
<nbsp/><nbsp/>$urls<nbsp/><<<nbsp/>url
end
<p/>
def<nbsp/>stop_msg_loop
<nbsp/><nbsp/>puts<nbsp/>"IE<nbsp/>has<nbsp/>exited..."
<nbsp/><nbsp/>throw<nbsp/>:done
end
<p/>
def<nbsp/>default_handler(event,<nbsp/>*args)
<nbsp/><nbsp/>case<nbsp/>event
<nbsp/><nbsp/>when<nbsp/>"BeforeNavigate"
<nbsp/><nbsp/><nbsp/><nbsp/>puts<nbsp/>"Now<nbsp/>Navigating<nbsp/>to<nbsp/>#{args[0]}..."
<nbsp/><nbsp/>end
end
<p/>
ie<nbsp/>=<nbsp/>WIN32OLE.new('InternetExplorer.Application')
ie.visible<nbsp/>=<nbsp/>TRUE
ie.gohome
ev<nbsp/>=<nbsp/>WIN32OLE_EVENT.new(ie,<nbsp/>'DWebBrowserEvents')
<p/>
ev.on_event<nbsp/>{|*args|<nbsp/>default_handler(*args)}
ev.on_event("NavigateComplete")<nbsp/>{|url|<nbsp/>navigate(url)}
ev.on_event("Quit")<nbsp/>{|*args|<nbsp/>stop_msg_loop}
<p/>
catch(:done)<nbsp/>{
<nbsp/><nbsp/>loop<nbsp/>{
<nbsp/><nbsp/><nbsp/><nbsp/>WIN32OLE_EVENT.message_loop
<nbsp/><nbsp/>}
}
<p/>
puts<nbsp/>"You<nbsp/>Navigated<nbsp/>to<nbsp/>the<nbsp/>following<nbsp/>URLs:<nbsp/>"
$urls.each_with_index<nbsp/>do<nbsp/>|url,<nbsp/>i|
<nbsp/><nbsp/>puts<nbsp/>"(#{i+1})<nbsp/>#{url}"
end
</alltt>
</codefragment>
<p/>
<classname>WIN32OLE_EVENT</classname> is used in conjunction with the <classname>WIN32OLE</classname> class
to add callbacks for Windows 32 events.
<methods type="class">
<p/>
<method name="message_loop" ref="message_loop">
<callseq>
WIN32OLE_EVENT.message_loop
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Executes the Windows event loop, translating and dispatching
events.
<p/>
</desc>
</method>
<p/>
<method name="new" ref="new">
<callseq>
WIN32OLE_EVENT.new ( <obj>anOle</obj>, <obj>aName</obj> )
<returns><obj>wapi</obj></returns>
</callseq>
<desc>
<p/>
Returns a new <tt>WIN32OLE_EVENT</tt> (an event sink) for the given
<tt>WIN32OLE</tt> object and named event source. If <obj>aName</obj> is <tt>nil</tt>,
it will attempt to use the default source and will raise a
<exception>RuntimeError</exception> if it cannot find one.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
<methods type="instance">
<p/>
<method name="on_event" ref="on_event">
<callseq>
<obj>wapi</obj>.on_event ( <opt><obj>anEvent</obj></opt> )
<block>{| args | <blockbody>block</blockbody> }</block>
<p/>
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Defines a callback
for the named <obj>anEvent</obj>. If <obj>anEvent</obj>
is <tt>nil</tt>, then this callback is associated with all events.
The block will be given any arguments appropriate for this
event.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
<p/>
<class name="Win32API" super="Object" type="class">
<p/>
This example is from the Ruby distribution, in
<tt>ext/Win32API</tt>:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ 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)
]]></fullcode>
require<nbsp/>'Win32API'
<p/>
getCursorPos<nbsp/>=<nbsp/>Win32API.new("user32",<nbsp/>"GetCursorPos",<nbsp/>['P'],<nbsp/>'V')
<p/>
lpPoint<nbsp/>=<nbsp/>"<nbsp/>"<nbsp/>*<nbsp/>8<nbsp/>#<nbsp/>store<nbsp/>two<nbsp/>LONGs
getCursorPos.Call(lpPoint)
x,<nbsp/>y<nbsp/>=<nbsp/>lpPoint.unpack("LL")<nbsp/>#<nbsp/>get<nbsp/>the<nbsp/>actual<nbsp/>values
<p/>
print<nbsp/>"x:<nbsp/>",<nbsp/>x,<nbsp/>"\n"
print<nbsp/>"y:<nbsp/>",<nbsp/>y,<nbsp/>"\n"
<p/>
ods<nbsp/>=<nbsp/>Win32API.new("kernel32",<nbsp/>"OutputDebugString",<nbsp/>['P'],<nbsp/>'V')
ods.Call("Hello,<nbsp/>World\n")
<p/>
GetDesktopWindow<nbsp/>=<nbsp/>Win32API.new("user32",<nbsp/>"GetDesktopWindow",<nbsp/>[],<nbsp/>'L')
GetActiveWindow<nbsp/>=<nbsp/>Win32API.new("user32",<nbsp/>"GetActiveWindow",<nbsp/>[],<nbsp/>'L')
SendMessage<nbsp/>=<nbsp/>Win32API.new("user32",<nbsp/>"SendMessage",<nbsp/>['L']<nbsp/>*<nbsp/>4,<nbsp/>'L')
SendMessage.Call(GetDesktopWindow.Call,<nbsp/>274,<nbsp/>0xf140,<nbsp/>0)
</alltt>
</codefragment>
<p/>
The <tt>Win32API</tt> module allows access to any arbitrary Windows 32
function. Many of these functions take or return a <tt>Pointer</tt>
datatype---a region of memory corresponding to a C string or
structure type.
<p/>
In Ruby, these pointers are represented using class <classname>String</classname>,
which contains a sequence of 8-bit bytes. It is up to you
to pack and unpack the bits in the <classname>String</classname>.
See the reference section for unpack on page 383 and
pack on page 290 for details.
<methods type="class">
<p/>
<method name="new" ref="new">
<callseq>
Win32API.new( <obj>dllname</obj>,
<obj>procname</obj>,
<obj>importArray</obj>,
<obj>export</obj> )
<returns><obj>wapi</obj></returns>
</callseq>
<desc>
<p/>
Returns a new object representing a Windows 32 API function.
<obj>dllname</obj> is the name of the DLL containing the function,
such as ``user32'' or ``kernel32.''
<obj>procname</obj> is the name of the desired function.
<obj>importArray</obj> is an array of strings
representing the types of arguments to the
function.
<obj>export</obj> 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.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
<methods type="instance">
<method name="call" ref="call">
<callseq>
<obj>wapi</obj>.call( <optz><obj>args</obj></optz> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Calls this API function with the given arguments, which must
match the signature specified to <tt>new</tt>.
<p/>
</desc>
</method>
<p/>
<method name="Call" ref="Call">
<callseq>
<obj>wapi</obj>.Call( <optz><obj>args</obj></optz> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Synonym for <cim><front>Win32API</front><back>call</back></cim>.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
<p/>
</chapter>
</ppdoc>
|