Class: VirtualBox::COM::FFIInterface
- Inherits:
-
BaseInterface
- Object
- BaseInterface
- VirtualBox::COM::FFIInterface
- Extended by:
- FFI::Library
- Includes:
- Logger
- Defined in:
- lib/virtualbox/com/ffi_interface.rb
Constant Summary collapse
- XPCOMC_VERSION =
Constant used to initialize the XPCOM C interface
0x00020000
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#virtualbox ⇒ Object
readonly
The VirtualBox and Session interfaces, both of which are extremely important in interfacing with the VirtualBox API.
-
#xpcom ⇒ Object
readonly
VBOXXPCOMC struct.
Class Method Summary collapse
-
.create(lib_path = nil) ⇒ Object
Sets up the FFI interface and also initializes the interface, returning an instance of FFIInterface.
-
.setup(lib_path = nil) ⇒ Object
Sets up the FFI interface by specifying the FFI library path and attaching the initial function (which can’t be done until the FFI library is specified).
Instance Method Summary collapse
-
#initialize ⇒ FFIInterface
constructor
A new instance of FFIInterface.
-
#initialize_com ⇒ Object
Initializes the COM interface with XPCOM.
-
#initialize_for_version(version) ⇒ Object
Initializes the FFI interface for a specific version.
-
#initialize_singletons ⇒ Object
Initializes the VirtualBox and Session interfaces.
Methods included from Logger
included, #logger, #logger_output=
Constructor Details
#initialize ⇒ FFIInterface
Returns a new instance of FFIInterface.
46 47 48 49 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 46 def initialize super initialize_com end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
21 22 23 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 21 def session @session end |
#virtualbox ⇒ Object (readonly)
The VirtualBox and Session interfaces, both of which are extremely important in interfacing with the VirtualBox API. Once these have been initialized, all other parts of the API can be accessed via these instances.
20 21 22 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 20 def virtualbox @virtualbox end |
#xpcom ⇒ Object (readonly)
VBOXXPCOMC struct. This typically won’t be used.
14 15 16 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 14 def xpcom @xpcom end |
Class Method Details
.create(lib_path = nil) ⇒ Object
Sets up the FFI interface and also initializes the interface, returning an instance of VirtualBox::COM::FFIInterface.
26 27 28 29 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 26 def create(lib_path=nil) setup(lib_path) new end |
.setup(lib_path = nil) ⇒ Object
Sets up the FFI interface by specifying the FFI library path and attaching the initial function (which can’t be done until the FFI library is specified).
36 37 38 39 40 41 42 43 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 36 def setup(lib_path=nil) # Setup the path to the C library lib_path ||= "/Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMC.dylib" # Attach to the interface ffi_lib lib_path attach_function :VBoxGetXPCOMCFunctions, [:uint], :pointer end |
Instance Method Details
#initialize_com ⇒ Object
Initializes the COM interface with XPCOM. This sets up the ‘virtualbox`, `session`, and `xpcom` attributes. This should only be called once.
53 54 55 56 57 58 59 60 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 53 def initialize_com # Get the pointer to the XPCOMC struct which contains the functions # to initialize xpcom_pointer = self.class.VBoxGetXPCOMCFunctions(XPCOMC_VERSION) @xpcom = FFI::VBOXXPCOMC.new(xpcom_pointer) initialize_singletons end |
#initialize_for_version(version) ⇒ Object
Initializes the FFI interface for a specific version.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 74 def initialize_for_version(version) logger.debug("FFI init: Trying version #{version}") # Setup the FFI classes COM::Util.set_interface_version(version) virtualbox_klass = COM::Util.versioned_interface(:VirtualBox) session_klass = COM::Util.versioned_interface(:Session) # Setup the OUT pointers virtualbox_ptr = ::FFI::MemoryPointer.new(:pointer) session_ptr = ::FFI::MemoryPointer.new(:pointer) # Call the initialization functions @xpcom[:pfnComInitialize].call(virtualbox_klass::IID_STR, virtualbox_ptr, session_klass::IID_STR, session_ptr) @virtualbox = virtualbox_klass.new(Implementer::FFI, self, virtualbox_ptr.get_pointer(0)) @session = session_klass.new(Implementer::FFI, self, session_ptr.get_pointer(0)) # Make a call to version to verify no exceptions are raised @virtualbox.implementer.valid? && @session.implementer.valid? logger.debug(" -- Valid version") true rescue ::FFI::NullPointerError => e logger.debug(" -- Invalid version") false end |
#initialize_singletons ⇒ Object
Initializes the VirtualBox and Session interfaces. It goes through the various directories until it finds a working pair.
64 65 66 67 68 69 70 71 |
# File 'lib/virtualbox/com/ffi_interface.rb', line 64 def initialize_singletons interface_dir = File.(File.join(File.dirname(__FILE__), "interface")) Dir[File.join(interface_dir, "*")].each do |f| if File.directory?(f) return if initialize_for_version(File.basename(f)) end end end |