Basic question about using the API

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
bobby4078
Posts: 10
Joined: 24. Nov 2011, 14:35
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: XP Pro SP3

Basic question about using the API

Post by bobby4078 »

Hello,

I would like to use the API to do simple progaming things like an executable that start a machine for exemple. I would like to do that in C++. The problem is that I don't know how to set it up.

I've download the sources from svn :

Code: Select all

svn co http://www.virtualbox.org/svn/vbox/trunk vbox
But now what should I do ?

I don't even know if what I downloaded from SVN is the API actually...

So what I would like to be able is to write a simple program of just a few lines like :

Code: Select all

IMachine machine = ...;
String name = machine.getName();
But how do I compile it in a simply way ?

Thanks !
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Basic question about using the API

Post by mpack »

Download the SDK from the downloads area. What you downloaded from SVN was the application source code, which is probably not what you want (that's like downloading the source code of the gcc compiler because you want to learn how to write C++).
bobby4078
Posts: 10
Joined: 24. Nov 2011, 14:35
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: XP Pro SP3

Re: Basic question about using the API

Post by bobby4078 »

Oh I didn't see that link :D

Ok now, how I use that ? I see in the samples folder tstVBoxAPIWin.cpp. I would like to compile it to test it. But before I suppose that I have to install the SKD right ? In the installer folder I see python script. Do I just have to run the python script ?
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Basic question about using the API

Post by mpack »

Sorry, I don't use the SDK. You asked if the source codes you downloaded were what you needed, and I'm afraid my answer to that question pretty much exhausted my knowledge of the subject.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Basic question about using the API

Post by noteirak »

Under the folder <SKD_ROOT>\bindings\xpcom\include, you'll normally find every include you need. The main headers you're interested into are the VBoxCAPI_vX_X.h files.
You should also read in its entierty the SDKRef.pdf file located in the docs dir of the SDK
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
bobby4078
Posts: 10
Joined: 24. Nov 2011, 14:35
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: XP Pro SP3

Re: Basic question about using the API

Post by bobby4078 »

Thanks. I still can't compile. I try to compile with g++. I created a test.cpp file, with this simple code :

Code: Select all

#include <iostream>
#include "VirtualBox_XPCOM.h"

using namespace std;

int main(){
	
	cout << "Test";
	
	return 0;
	
}
I just include a header. I did a make file so I can include all the .h :

Code: Select all

INC = 	-I../include/ \
		-I../include/ipcd \
		-I../include/nsprub \
		-I../include/string \
		-I../include/xpcom

Test:
	g++ $(INC) test.cpp -o TesT
But I still have a lot of error messages. What is wrong with it ?
bobby4078
Posts: 10
Joined: 24. Nov 2011, 14:35
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: XP Pro SP3

Re: Basic question about using the API

Post by bobby4078 »

Ok I found the problem, I was trying to use XPCom instead of MSCom. Now compilation seems to be better if I try to compile the sample file tstVBoxAPIWin.cpp. But I still have errors :

Code: Select all

D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): undefined reference to `_imp__SafeArrayAccessData@8'
D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): udefined reference to `_imp__SysFreeString@4'
D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): undefined reference to `_imp__SafeArrayUnaccessData@4'
D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): undefined reference to `_imp__SafeArrayDestroy@4'
D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): undefined reference to `_imp__SysAllocString@4'
D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): undefined reference to `_imp__GetErrorInfo@8'
D:\DOCUME~1\BOBBY\LOCALS~1\Temp\ccrUxk1M.o:tstVBoxAPIWin.cpp:(.text+0x42a): undefined reference to `_imp__SysFreeString@4'.

...etc...
I got these errors when I run that compilation command :

Code: Select all

g++ -I../include/ tstVBoxAPIWin.cpp
So It seems that the problem is comming from the Win32 API in functions like SafeArrayAccessData(), SysFreeString(), SafeArrayUnaccessData() etc. But I don't know what to do to solve it ?

Thanks
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Basic question about using the API

Post by mpack »

SafeArrayXXXXX etc are not strictly part of the Win32 API. Trying linking in OleAut32.lib. Google for any remaining missing functions, note which library is required.
bobby4078
Posts: 10
Joined: 24. Nov 2011, 14:35
Primary OS: MS Windows XP
VBox Version: PUEL
Guest OSses: XP Pro SP3

Re: Basic question about using the API

Post by bobby4078 »

Yes I add this to my makefile :

Code: Select all

-loleaut32 -lOle32
and it's a lot better.
Now I just miss some lib that I don't find, here are the functions :
  • IID_ISession
  • CLSID_Session
  • IID_IVirtualBox
  • CLSID_VirtualBox
Where can I find the lib for these functions ?
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Basic question about using the API

Post by mpack »

Some of those look like VirtualBox SDK specifics, (VirtualBox GUIDs) so you'll need to consult the SDK docs on that. I expect they'll be declared in a header file.
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Re: Basic question about using the API

Post by rousseauhk »

>>bobby4078

If you are trying to build on windows, by far the easiest way to get started is to download the free Visual Studio Express/Desktop:

* Download/install VSE Desktop 2012
* Create a new empty C++ project (remove any auto-generated files)
* Copy the following into your project
sdk\bindings\mscom\samples\tstVBoxAPIWin.cpp
sdk\bindings\mscom\lib\VirtualBox_i.c
sdk\bindings\mscom\include\VirtualBox.h
* Compile & Run

This should work for you out of the box (just make sure that the SDK version is the same as the VirtualBox version you have installed e.g. don't mix 4.2 SDK with 4.3 VirtualBox).
Post Reply