Tuesday, June 3, 2008

Bulding and Running the VirtualBox SDK sample using COM, VC++

VirtualBox is Sun Microsystem's desktop virtualization software which allows you to do cool stuff like running Linux right inside Windows without having to repartition Windows or anything like that. They recently released 1.6 version allows you to run even Solaris (not OpenSolaris yet at the time of this writing) inside Windows. (They also support a bunch of other operating systems as hosts and guests).

Check it out - its a breeze to download and get a Linux (e.g. Knoppix) distro working within minutes. With USB support, you can even mount a pen-drive and save your files externally.

I was poring over the developer docs to what they exposed in terms of an API. Its pretty cool - Virtualbox allows access to its runtime using both web services (Apache Axis et. al.) as well as using the now almost defunct COM (component object model) technology . It also seems to support the cross-platform Mozilla XPCOM but I haven't looked at it yet.

Having worked with COM a lot prior to 2000, i was eager to try out the COM approach. The docs however disappoint immensely as it seems to assume that if you were someone truly interested in really taking that route, you would know what to do. Turned out, my COM was rusty and after some digging around for almost non-existent COM documentation/articles, I figured I wasn't linking in the interfaces file generated by VirtualBox.

Install VirtualBox if you already haven't. The SDK is present under \Sun\xVM VirtualBox\sdk\

Here is a gist of steps I took :

Visual C++

1. Fire up your favorite C++ compiler (I use the free VC++ 9.0 Express edition) and create a Win32 console project.
2. Add the sdk\samples\API\tstVBoxAPIWin.cpp.
3. Add sdk\include to the INCLUDE path.
4. Don't forget to also add sdk\lib\VirtualBox_i.c to your sources since this file contains all the generated COM interfaces for VirtualBox.
5. Add sdk\lib to your linker path .
6. Run a build.


CYGWIN (using gcc, mingw32)

1. Copy all source files i.e. (
tstVBoxAPIWin.cpp, VirtualBox_i.c, VirtualBox.h) to a working directory.

2. gcc -c
tstVBoxAPIWin.cpp VirtualBox_i.c

3. export LD_LIBRARY_PATH = c:\WINDOWS\system32

4. gcc -o vbox VirtualBox_i.o tstVBoxAPIWin.o -mno-cygwin -lole32 -loleaut32


The resultant executable when run, prints out all my virtual machines for the various guest OSes I run on my Windows Box.

Output :

Name: knoppix-on-xp
Name: LKB_ON_KNOPPIX
Name: open-solaris

Not much achieved but for me this is a cool way to get back into my COM programming days while tinkering with my favorite virtualization software. The install has a SDK reference that gives all gory details about the various interfaces exposed by the COM interface. Onto more tinkering .........


References :

  • http://forums.virtualbox.org/viewtopic.php?t=6948

5 comments:

Silent Gamer said...

Hey Mr

I've just done what you said in my Visual C++ project and I got this error when I build it :

Compiling...
1>VirtualBox_i.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals

it seems I haven't coorectly added the library to the linker path so can you please explain to me how exactly did you do it ?

Raghavan said...

@SilentGamer : It appears to be a problem with your Project settings resulting in a fairly common error :

A google search for the error throws up this useful link :

http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/14e85604-6929-4707-a22e-8cdf596926a6

Hope that helps.

Silent Gamer said...

Hey Mr

first of all thank you for answering me.

Well, after 5 days of trying all kinds of files, googling and surfing all kinds of websites I've discovered that I need to add the file "VirtualBox.idl" to the project folder to get the sample running.

To let you know, I added the files you mentioned on your blog plus that file I mentioned to the folder project, I compiled the c and cpp files, I didn't add any path to the linker and project has successfully built the application

Raghavan said...

@Silent Gamer : Glad to know you figured it out. I'll try your instructions outwhen I have a chance.

Anonymous said...

Thanks for the post. It was not obvious that adding VirtualBox_i.cpp would solve the missing interface errors.