2007-12-02 21:53:13 -05:00
|
|
|
# -*- ruby -*-
|
|
|
|
# for backward compatibility
|
2014-11-04 00:25:41 -05:00
|
|
|
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead" if $VERBOSE
|
2007-12-02 21:53:13 -05:00
|
|
|
|
2014-11-04 07:26:19 -05:00
|
|
|
require 'fiddle/import'
|
2007-12-02 21:53:13 -05:00
|
|
|
|
|
|
|
class Win32API
|
|
|
|
DLL = {}
|
2014-10-31 17:13:09 -04:00
|
|
|
TYPEMAP = {"0" => Fiddle::TYPE_VOID, "S" => Fiddle::TYPE_VOIDP, "I" => Fiddle::TYPE_LONG}
|
|
|
|
POINTER_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q*' : 'l!*'
|
2007-12-02 21:53:13 -05:00
|
|
|
|
2009-03-11 04:27:21 -04:00
|
|
|
def initialize(dllname, func, import, export = "0", calltype = :stdcall)
|
2007-12-02 21:53:13 -05:00
|
|
|
@proto = [import].join.tr("VPpNnLlIi", "0SSI").sub(/^(.)0*$/, '\1')
|
2014-10-31 17:13:09 -04:00
|
|
|
handle = DLL[dllname] ||= Fiddle.dlopen(dllname)
|
|
|
|
|
|
|
|
@func = Fiddle::Function.new(
|
|
|
|
handle[func],
|
2014-11-04 07:26:19 -05:00
|
|
|
import.chars.map { |win_type| TYPEMAP[win_type.tr("VPpNnLlIi", "0SSI")] },
|
2014-10-31 17:13:09 -04:00
|
|
|
TYPEMAP[export.tr("VPpNnLlIi", "0SSI")],
|
2014-11-04 07:26:19 -05:00
|
|
|
Fiddle::Importer.const_get(:CALL_TYPE_TO_ABI)[calltype]
|
2014-10-31 17:13:09 -04:00
|
|
|
)
|
|
|
|
rescue Fiddle::DLError => e
|
2009-02-13 09:37:11 -05:00
|
|
|
raise LoadError, e.message, e.backtrace
|
2007-12-02 21:53:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(*args)
|
|
|
|
import = @proto.split("")
|
|
|
|
args.each_with_index do |x, i|
|
2012-11-05 09:00:41 -05:00
|
|
|
args[i], = [x == 0 ? nil : x].pack("p").unpack(POINTER_TYPE) if import[i] == "S"
|
2007-12-02 21:53:13 -05:00
|
|
|
args[i], = [x].pack("I").unpack("i") if import[i] == "I"
|
|
|
|
end
|
|
|
|
ret, = @func.call(args)
|
|
|
|
return ret || 0
|
|
|
|
end
|
|
|
|
|
|
|
|
alias Call call
|
|
|
|
end
|