Page 1 of 1

Access the Virtualbox API inside the guest?

Posted: 24. Feb 2012, 16:02
by scottgus1
I'm using "Vboxmanage guestproperty get|set" as a heartbeat indicator in my VMs (Windows 7-64 Prof host, SBS2003 & 3 XP guests). The host runs a batch file every ten minutes that "guestproperty set"s a custom property to 0, then waits for the guest (running its own batch file) to set the property back to 1. If the guest does so within a certain time, the host assumes the guest is working properly, since the guest has to be able to run a batch file successfully to respond to the heartbeat request. Everything is working fine.

But...

I can't stand those flashing command windows every ten minutes. So I'm looking into running the "guestproperty get|set" stuff in a Visual Basic Script (.vbs), which will run invisibly. I found the SDK reference manual, and I'm hunting through it. I've hit a jam, though. The manual offers this code as a test to see the version of Virtualbox from a vbs file:

Code: Select all

set vb = CreateObject("VirtualBox.VirtualBox")
Wscript.Echo "VirtualBox version " & vb.version
On the host, this popus up a little window with the version listed. On a guest, I get an error: "ActiveX component can't create object: 'VirtualBox.VirtualBox' "

I assume either there is no API to access on a guest, or it's called by something else. Anyone know? Or do I contact the developers?

Re: Access the Virtualbox API inside the guest?

Posted: 26. Feb 2012, 04:39
by Technologov
contact devs on vbox-dev maling-list.

Re: Access the Virtualbox API inside the guest?

Posted: 28. Feb 2012, 15:25
by scottgus1
The answer from the developers is, there is no API to access in the guest.
But they helped me with my VBS script so the VboxControl.exe line I was running wouldn't flash a command window. They reminded me of the WindowState command in VBS's Run method that can hide the window.
So I'm all set, problem solved! The devs rock! Thanks very much!

For reference, the script being run to change a guest property is:

Code: Select all

on error resume next
Set shell = WScript.CreateObject ("WSCript.shell")
shell.run "VBoxControl.exe guestproperty set /vmcontrol/heartbeat 1",7
wscript.sleep 30000
shell.run "VBoxControl.exe guestproperty set /vmcontrol/heartbeat 1",7
This script is run via a Scheduled Task, every minute on the guest. It changes the custom Guest Property to a "1", then waits 30 seconds, and sets the property to 1 again. The ",7" after the command on the two "shell.run" lines opens the window minimized. No flashing command window, just a little glitch on the taskbar occasionally. (I have also heard it is possible to run a program completely invisibly, even if it has a window, by having the Scheduled Task run it under a different user account than the one that's lgged on, too.)

This test batch file is on the host:

Code: Select all

set /a heartbeatcount = 0
"C:\Program Files\Oracle\VirtualBox\vboxmanage" guestproperty set {vmname} /vmcontrol/heartbeat 0
:heartbeatloop 
sleep 5
"C:\Program Files\Oracle\VirtualBox\vboxmanage" guestproperty get {vmname} /vmcontrol/heartbeat > heartbeat.txt
find /i /n "Value: 1" heartbeat.txt > nul
if "%Errorlevel%"=="0" goto foundheartbeat
set /a heartbeatcount += 5
if %heartbeatcount%=={SecondsToDelay} goto noheartbeat
goto heartbeatloop

:noheartbeat
echo no heartbeat
goto end

:foundheartbeat
echo heartbeat found
:end
This displays the geartbeat stat of the VM. It sets the Guest Property to 0, then waits a certain amount of time for the guest property to change to 1 again. If it does or does not switch back to 1, the script can do whatever is desired. (My batch file has more in it: if the heartbeat doesn't come, the VM is shut down / killed and restarted, and an event is recorded in the Event Viewer. If all the VM's - 4 as of now - fail to return heartbeats, the Virtualbox main system may have crashed, so the host is forceably rebooted.)