Class: VirtualBox::Lib

Inherits:
Object
  • Object
show all
Defined in:
lib/virtualbox/lib.rb

Overview

Used by the rest of the VirtualBox library to interface with the VirtualBox XPCOM library (VBoxXPCOMC). Most users will never need to interface with this class directly, except other to set the path to the ‘VBoxXPCOMC` lib.

# Setting the Path to the VBoxXPCOMC Library

This won’t be necessary for 95% of users, and won’t be necessary at all for windows users. But for unix users, the VirtualBox gem uses a dynamic library named ‘VBoxXPCOMC` to interface with VirtualBox. The gem does its best to guess the path to this gem based on the operating system ruby is running on, but in the case you get an error about it missing, you can easily set it:

VirtualBox::Lib.lib_path = "/path/to/VBoxXPCOMC.so"

**Windows users will never need to do this.**

Constant Summary collapse

@@lib_path =
nil
@@lib =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib_path) ⇒ Lib

Returns a new instance of Lib.



73
74
75
76
77
78
79
80
81
82
# File 'lib/virtualbox/lib.rb', line 73

def initialize(lib_path)
  if Platform.windows?
    @interface = COM::MSCOMInterface.new
  else
    @interface = COM::FFIInterface.create(lib_path)
  end

  @virtualbox = @interface.virtualbox
  @session = @interface.session
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface.



24
25
26
# File 'lib/virtualbox/lib.rb', line 24

def interface
  @interface
end

#sessionObject (readonly)

Returns the value of attribute session.



26
27
28
# File 'lib/virtualbox/lib.rb', line 26

def session
  @session
end

#virtualboxObject (readonly)

Returns the value of attribute virtualbox.



25
26
27
# File 'lib/virtualbox/lib.rb', line 25

def virtualbox
  @virtualbox
end

Class Method Details

.libLib

The singleton instance of Lib.

Returns:



38
39
40
# File 'lib/virtualbox/lib.rb', line 38

def lib
  @@lib ||= new(lib_path)
end

.lib_pathObject

Returns the path to the virtual box library. If the path has not yet been set, it attempts to infer it based on the platform ruby is running on.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/virtualbox/lib.rb', line 54

def lib_path
  if @@lib_path.nil?
    if Platform.mac?
      @@lib_path = ["/Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMC.dylib"]
    elsif Platform.linux?
      @@lib_path = ["/opt/VirtualBox/VBoxXPCOMC.so", "/usr/lib/virtualbox/VBoxXPCOMC.so"]
    elsif Platform.solaris?
      @@lib_path = ["/opt/VirtualBox/amd64/VBoxXPCOMC.so", "/opt/VirtualBox/i386/VBoxXPCOMC.so"]
    elsif Platform.windows?
      @@lib_path = "Unknown"
    else
      @@lib_path = "Unknown"
    end
  end

  @@lib_path
end

.lib_path=(value) ⇒ Object

Sets the path to the VBoxXPCOMC library which is created with any VirtualBox install. 90% of the time, this won’t have to be set manually, and instead the gem will try to find it for you.

Parameters:

  • Full (String)

    path to the VBoxXPCOMC library



47
48
49
# File 'lib/virtualbox/lib.rb', line 47

def lib_path=(value)
  @@lib_path = value.nil? ? value : File.expand_path(value)
end

.reset!Object

Resets the initialized library (if there is any). This is primarily only used for testing.



31
32
33
# File 'lib/virtualbox/lib.rb', line 31

def reset!
  @@lib = nil
end