Basic question about using the API

Discussion about using the VirtualBox API, Tutorials, Samples.

Basic question about using the API

Postby bobby4078 » 8. Aug 2013, 15:56

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   Expand viewCollapse view
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   Expand viewCollapse view
IMachine machine = ...;
String name = machine.getName();


But how do I compile it in a simply way ?

Thanks !
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

Postby mpack » 8. Aug 2013, 16:14

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++).
mpack
Site Moderator
 
Posts: 29928
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

Postby bobby4078 » 8. Aug 2013, 16:35

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 ?
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

Postby mpack » 8. Aug 2013, 17:22

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.
mpack
Site Moderator
 
Posts: 29928
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

Postby noteirak » 8. Aug 2013, 17:44

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://kamax.io/hbox/
Manage your VirtualBox infrastructure the free way!
noteirak
Site Moderator
 
Posts: 5198
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7

Re: Basic question about using the API

Postby bobby4078 » 9. Aug 2013, 13:00

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   Expand viewCollapse view
#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   Expand viewCollapse view
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

Postby bobby4078 » 9. Aug 2013, 13:47

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   Expand viewCollapse view
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   Expand viewCollapse view
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
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

Postby mpack » 9. Aug 2013, 13:55

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.
mpack
Site Moderator
 
Posts: 29928
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

Postby bobby4078 » 9. Aug 2013, 17:00

Yes I add this to my makefile :
Code: Select all   Expand viewCollapse view
-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 ?
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

Postby mpack » 9. Aug 2013, 17:12

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.
mpack
Site Moderator
 
Posts: 29928
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

Postby rousseauhk » 24. Oct 2013, 04:52

>>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).
rousseauhk
 
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server


Return to The VirtualBox API

Who is online

Users browsing this forum: No registered users and 1 guest