VirtualBox is a virtual machine emulator created by innotek. It's a fantastic application that allows me to run a second operating system on top of Windows, much like VMWare does.

While I was browsing through the list of COM references in Visual Studio, I was surprised to find that VirtualBox exposes all of its functions through a COM component that can easily be accessed from C# (or any .NET language for that matter). This could be incredibly handy for instances where you need to automate the creation or execution of virtual machines. I'm sure you could even monitor a running machine with this library. I haven't dug that deep yet, however.

To use the component, just add a reference to "innotek VirtualBox Type Library" and put the following using statement at the top of your source file:

using VirtualBox;

The functions are exposed through the IVirtualBox interface, so go ahead and create an instance of it:

IVirtualBox vb = new VirtualBoxClass();

It's relatively simple to retrieve a list of all registered virtual machines. However, we can't use foreach, because the Machines collection doesn't implement the GetEnumerator() function like .NET collections. Don't worry, it's still quite possible:

for (uint m = 0; m < vb.Machines.Count; m++) {
    MessageBox.Show(vb.Machines.GetItemAt(m).Name);
}

Unfortunately, starting a virtual machine is a little more complicated and you have to provide your own method of visualizing the machine's display. I haven't found the best way to do this, yet. I'm working on it, though. So, if anyone has any suggestions, feel free to share them. For now, I'll show you how to boot a virtual machine. First, however, you need to know a little bit about how VirtualBox works.

In VirtualBox, a running machine is a called a session. Sessions are created by calling the CreateSession() method of the VirtualBoxClass object we created before. Sessions are managed with a console. The console provides methods for things such as starting and stopping the machine and generating a snapshot of the machine's current state. This is about as basic as you can get when booting a virtual machine:

Session session = new Session();
vb.OpenSession(session, vb.FindMachine("Machine Name").Id);
session.Console.PowerUp();

FindMachine() will locate the machine with the provided name and return a new Machine object whose Id is then passed to the OpenSession() method. Once the session has been opened, you can start using the console. Calling PowerUp() is just like pressing the power button on your computer. You can also call PowerDown() to shut the machine off.

Running the code will produce absolutely no output. However, VirtualBox is actually booting your virtual machine behind-the-scenes. You can check to make sure everything worked by checking the value of the console's State property.

Hopefully this little introduction has given you enough information that you can begin using the power of VirtualBox in your own applications!

Technorati Tags: ,,