ruby提供的功能比较强大,能直接调用win32 api,还能使用OLE组建。
一、 win32 ole调用:
require "win32ole"
i = WIN32OLE.new('AutoItX3.Control') #使用AutoIt工具的OLE
puts i.ole_methods #打印出所有支持的方法
i.Run("calc.exe")
二。 win32 api调用:
由于涉及到一些结构体,自己使用二进制方法比较麻烦,可以使用已有的CStruct gem包来处理。
安装步骤和简单介绍见:http://www.w-yong.com/docs/ruby_win32_api.html
官方的一个事例:
require 'windows/memory'
require 'win32struct'
include Windows::Memory
# example:
# typedef struct _MEMORYSTATUS {
# DWORD dwLength;
# DWORD dwMemoryLoad;
# DWORD dwTotalPhys;
# DWORD dwAvailPhys;
# DWORD dwTotalPageFile;
# DWORD dwAvailPageFile;
# DWORD dwTotalVirtual;
# DWORD dwAvailVirtual;
# } MEMORYSTATUS, *LPMEMORYSTATUS;
class MEMORYSTATUS < Win32Struct
DWORD :dwLength
DWORD :dwMemoryLoad
DWORD :dwTotalPhys
DWORD :dwAvailPhys
DWORD :dwTotalPageFile
DWORD :dwAvailPageFile
DWORD :dwTotalVirtual
DWORD :dwAvailVirtual
end
# create a MEMORYSTATUS's instance
stat = MEMORYSTATUS.new {|st| st.dwLength = MEMORYSTATUS.size }
# call API "GlobalMemoryStatus" - See also MSDN
GlobalMemoryStatus(stat.data)
#output
printf "[Physical Memory]
"
printf " total:%12d bytes
",stat.dwTotalPhys
printf " free :%12d bytes
",stat.dwAvailPhys
printf "[Virtual Memory]
"
printf " total:%12d bytes
",stat.dwTotalVirtual
printf " free :%12d bytes
",stat.dwAvailVirtual
printf "[Paging File]
"
printf " total:%12d bytes
",stat.dwTotalPageFile
printf " free :%12d bytes
",stat.dwAvailPageFile
运行结果:
E:att_projRubyStudylib>ruby cstruct.rb
[Physical Memory]
total: 2147483647 bytes
free : 2126528512 bytes
[Virtual Memory]
total: 2147352576 bytes
free : 2082926592 bytes
[Paging File]
total: 4294967295 bytes
free : 4294967295 bytes