Project

General

Profile

Feature #8601 ยป Win32API.rb

arton (Akio Tajima), 07/05/2013 03:29 AM

 
1
# -*- ruby -*-
2
# for backward compatibility
3
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: Win32API is deprecated after Ruby 1.9.1; use dl directly instead" if $VERBOSE
4

    
5
class Win32API
6
  begin
7
    require 'fiddle'
8
    Caller = Fiddle
9
  rescue LoadError  
10
    require 'dl'
11
    Caller = DL
12
  end  
13
  DLL = {}
14
  TYPEMAP = {"0" => Caller::TYPE_VOID, "S" => Caller::TYPE_VOIDP, "I" => Caller::TYPE_LONG}
15
  POINTER_TYPE = Caller::SIZEOF_VOIDP == Caller::SIZEOF_LONG_LONG ? 'q*' : 'l!*'
16

    
17
  def initialize(dllname, func, import, export = "0", calltype = :stdcall)
18
    @proto = [import].join.tr("VPpNnLlIi", "0SSI").sub(/^(.)0*$/, '\1')
19
    handle = DLL[dllname] ||= Caller.dlopen(dllname)
20
    unless defined? Fiddle
21
      @func = DL::CFunc.new(handle[func], TYPEMAP[export.tr("VPpNnLlIi", "0SSI")], func, calltype)
22
    else
23
      temp = [import].join.each_char.map{|s|s.tr("VPpNnLlIi", "0SSI")}.map{|v|TYPEMAP[v]}
24
      @func = Fiddle::Function.new(handle[func], temp, TYPEMAP[export.tr("VPpNnLlIi", "0SSI")], calltype == :stdcall ? Fiddle::Function::STDCALL : Fiddle::Function::DEFAULT)
25
    end
26
  rescue Caller::DLError => e
27
    raise LoadError, e.message, e.backtrace
28
  end
29

    
30
  def call(*args)
31
    import = @proto.split("")
32
    args.each_with_index do |x, i|
33
      args[i], = [x == 0 ? nil : x].pack("p").unpack(POINTER_TYPE) if import[i] == "S"
34
      args[i], = [x].pack("I").unpack("i") if import[i] == "I"
35
    end
36
    unless defined? Fiddle
37
      ret, = @func.call(args)
38
    else
39
      ret, = @func.call(*args)
40
    end  
41
    return ret || 0
42
  end
43

    
44
  alias Call call
45
end