Class: Cosmos::Win32API
Constant Summary collapse
- DLL_CACHE =
Cache to hold already opened dll files
{}
- RETURN_VALUE_TYPEMAP =
{"0" => DL::TYPE_VOID, "S" => DL::TYPE_VOIDP, "I" => DL::TYPE_LONG}
Instance Method Summary collapse
- #call(*args) ⇒ Object (also: #Call)
-
#initialize(dll_name, function_name, import, export = "0") ⇒ Win32API
constructor
A new instance of Win32API.
Constructor Details
#initialize(dll_name, function_name, import, export = "0") ⇒ Win32API
Returns a new instance of Win32API.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cosmos/win32/win32_main.rb', line 21 def initialize(dll_name, function_name, import, export = "0") # Convert all input parameters into either 0, S, or I @function_prototype = [import].join.tr("VPpNnLlIi", "0SSI") # Get handle to dll file and add to cache if necessary dll_handle = DLL_CACHE[dll_name] ||= DL.dlopen(dll_name) # Create DL::CFunc necessary to call a function with proper return type and name @function = DL::CFunc.new(dll_handle[function_name], RETURN_VALUE_TYPEMAP[export.tr("VPpNnLlIi", "0SSI")], function_name) end |
Instance Method Details
#call(*args) ⇒ Object Also known as: Call
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cosmos/win32/win32_main.rb', line 32 def call(*args) # Break up prototype into characters import = @function_prototype.split('') args.each_with_index do |arg, index| case import[index] when 'S' # Handle NULL specified with 0 value arg = nil if arg == 0 # Convert argument into array of longs args[index], = [arg].pack("p").unpack("l!*") when 'I' # Handle intergers larger than 2^31 - 1 args[index], = [arg].pack("I").unpack("i") end end # Call the function and return its return value return_value = @function.call(args) return_value ||= 0 return_value end |