Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > Win32APIライブラリ > Win32APIクラス

class Win32API

クラスの継承リスト: Win32API < Object < Kernel < BasicObject

要約

Win32 API を呼び出すためのクラスです。

使用例 1: MessageBox

require 'Win32API'
# require 'dl/win32'

class Win32API
  # type flag
  MB_OK               = 0
  MB_OKCANCEL         = 1
  MB_ABORTRETRYIGNORE = 2
  MB_YESNOCANCEL      = 3
  MB_YESNO            = 4
  MB_RETRYCANCEL      = 5

  # return values
  IDOK     = 1
  IDCANCEL = 2
  IDABORT  = 3
  IDRETRY  = 4
  IDIGNORE = 5
  IDYES    = 6
  IDNO     = 7

  def Win32API.MessageBox(wnd, text, caption, type = MB_OK)
    messagebox = Win32API.new('user32', 'MessageBox', %w(p p p i), 'i')

    messagebox.call(wnd, text, caption, type)
  end

  def Win32API.MessageBoxEx(wnd, text, caption, type = MB_OK, languageid = 0)
    messagebox = Win32API.new('user32', 'MessageBoxEx', %w(p p p i i), 'i')

    messagebox.call(wnd, text, caption, type, languageid)
  end
end

p Win32API.MessageBox(0, "test message", "test caption")
p Win32API.MessageBoxEx(0, "test message", "test caption")
p Win32API.MessageBox(0, "てすと", "テスト")
p Win32API.MessageBoxEx(0, "てすと", "テスト")

使用例 2: Cygwin の uname コマンドの代わり

require 'Win32API'

module Cygwin
  def uname
    uname = Win32API.new 'cygwin1', 'uname', ['P'], 'I'
    utsname = ' ' * 100
    raise 'cannot get system name' if uname.call(utsname) == -1

    utsname.unpack('A20' * 5)
  end
  module_function :uname
end

p Cygwin.uname

=> ["CYGWIN_98-4.10", "hoge", "1.1.7(0.31/3/2)", "2000-12-25 12:39", "i586"]

使用例 3: Cygwin の cygpath コマンドの代わり

require 'Win32API'

module Cygwin
  @conv_to_full_posix_path =
    Win32API.new('cygwin1.dll', 'cygwin_conv_to_full_posix_path', 'PP', 'I')
  @conv_to_posix_path =
    Win32API.new('cygwin1.dll', 'cygwin_conv_to_posix_path', 'PP', 'I')
  @conv_to_full_win32_path =
    Win32API.new('cygwin1.dll', 'cygwin_conv_to_full_win32_path', 'PP', 'I')
  @conv_to_win32_path =
    Win32API.new('cygwin1.dll', 'cygwin_conv_to_win32_path', 'PP', 'I')

  def cygpath(options, path)
    absolute = shortname = false
    func = nil
    options.delete(" \t-").each_byte {|opt|
      case opt
      when ?u
        func = [@conv_to_full_posix_path, @conv_to_posix_path]
      when ?w
        func = [@conv_to_full_win32_path, @conv_to_win32_path]
      when ?a
        absolute = true
      when ?s
        shortname = true
      end
    }
    raise ArgumentError "first argument must contain -u or -w" if func.nil?
    func = absolute ? func[0] : func[1]
    buf = "\0" * 300
    if func.Call(path, buf) == -1
      raise "cannot convert path name"
    end
    buf.delete!("\0")
    buf
  end
  module_function :cygpath
end

p Cygwin.cygpath("-u", 'c:\\')         # => "/cygdrive/c"
p Cygwin.cygpath("-w", '/cygdrive/c')  # => "c:\\"
p Cygwin.cygpath("-wa", '.')           # => "d:\\home\\arai"

特異メソッド

定義 説明
new(dllname, func, import, export)

DLL dllname をロードし、API func のオブジェクトを生成します。

インスタンスメソッド

定義 説明
call(*args)
Call(*args)

API を呼び出します。 指定する引数と戻り値は Win32API.new の引数の指定に従います。 特にポインタを渡してそのポインタの指す領域に値が設定される場合は その領域をあらかじめ確保しておく必要があります。

継承したメソッド

! != == === =~ __id__ __send__ _dump class clone dclone display enum_for eql? equal? extend freeze frozen? hash initialize initialize_copy inspect instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods must_be must_be_close_to must_be_empty must_be_instance_of must_be_kind_of must_be_nil must_be_same_as must_be_within_epsilon must_equal must_include must_match must_raise must_respond_to must_send must_throw nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? respond_to_missing? singleton_class singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_s to_str to_yaml to_yaml_properties to_yaml_style trust untaint untrust untrusted? wont_be wont_be_close_to wont_be_empty wont_be_instance_of wont_be_kind_of wont_be_nil wont_be_same_as wont_be_within_epsilon wont_equal wont_include wont_match wont_respond_to