Class: OpenC3::Win32API

Inherits:
Object show all
Defined in:
lib/openc3/win32/win32_main.rb

Constant Summary collapse

DLL_CACHE =

Cache to hold already opened dll files

{}
VALUE_TYPEMAP =
{ "0" => Fiddle::TYPE_VOID, "S" => Fiddle::TYPE_VOIDP, "I" => Fiddle::TYPE_LONG }

Instance Method Summary collapse

Constructor Details

#initialize(dll_name, function_name, import, export = "0") ⇒ Win32API

Returns a new instance of Win32API.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/openc3/win32/win32_main.rb', line 36

def initialize(dll_name, function_name, import, export = "0")
  # Convert all input parameters into either 0, S, or I
  @function_prototype = [import].join.tr("VPpNnLlIiCc", "0SSI")
  params = []
  @function_prototype.split('').each do |param|
    params << VALUE_TYPEMAP[param]
  end

  # Get handle to dll file and add to cache if necessary
  dll_handle = DLL_CACHE[dll_name] ||= Fiddle.dlopen(dll_name)

  # Create Fiddle::Function necessary to call a function with proper return type and name
  @function = Fiddle::Function.new(dll_handle[function_name], params, VALUE_TYPEMAP[export.tr("VPpNnLlIi", "0SSI")])
end

Instance Method Details

#call(*args) ⇒ Object Also known as: Call



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openc3/win32/win32_main.rb', line 51

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(POINTER_TYPE)
    when 'I'
      # Handle integers 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