Grabbing Session of already open machine? (C#)

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
JoeTex
Posts: 2
Joined: 17. Feb 2014, 10:02

Grabbing Session of already open machine? (C#)

Post by JoeTex »

I have figured out how to start the machine using LaunchVMProcess, but what if it's already open. How would I grab the Session or Console of the currently running machine?

I am programming in C# using COM, but which interfaces will allow me to grab the session?

Thanks
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: Grabbing Session of already open machine? (C#)

Post by noteirak »

First, you need to grab a ISession object from IVirtualbox.
Then get a shared lock on the machine using the session object you just retrieve
You can then use the ISession object you already have to access the console.

In pseudo code :

Code: Select all

IVirtualbox vbox = VirtualboxManager.createInstance(null);
ISession session = vbox.getSession();
IMachine vm = vbox.findMachine(nameOrUuid);
vm.lockMachine(session,LockType.Shared);
IConsole console = session.getConsole();
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
JoeTex
Posts: 2
Joined: 17. Feb 2014, 10:02

Re: Grabbing Session of already open machine? (C#)

Post by JoeTex »

noteirak wrote:First, you need to grab a ISession object from IVirtualbox.
Then get a shared lock on the machine using the session object you just retrieve
You can then use the ISession object you already have to access the console.

In pseudo code :

Code: Select all

IVirtualbox vbox = VirtualboxManager.createInstance(null);
ISession session = vbox.getSession();
IMachine vm = vbox.findMachine(nameOrUuid);
vm.lockMachine(session,LockType.Shared);
IConsole console = session.getConsole();
I did not see VirtualboxManager in the API that I used. I was using VirtualBoxClass() and SessionClass(). I believe I tried using lockMachine on a VM started through the VirtualBox application, but it said it was already locked.
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: Grabbing Session of already open machine? (C#)

Post by noteirak »

This is pseudo-code, so VirtualboxManager reprensent whatever you use to grab the IVirtualbox object. This is language specific.

For the lock error, you most likely tried to use something else than Shared (Write & VM exists). Since the VM is started and the exclusive write lock is already owned by the processes running the VM, you cannot get it as well. That's where the Shared lock type comes in, giving you the abilitiy to access the VM console (among other things).

If this doesn't fix your issue, please post the exact exception you get including the stacktrace as well as the relevant code.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply