I am having some issues in accessing the cableConnected property with C# for a VM. The goal is to be able to disconnect the cable upon a certain event happening. However, I am stuck on simply trying to perform this action whenever my application is run. I am currently using VirtualBox (and API) v4.2.16 on Windows 7, and would really appreciate some help in pointing out what is wrong with this application. Whenever I run this application I get the error "The machine 'test' is already locked for a session (or being unlocked)". The VM is running at the point which I run my application just fyi. I am very new to C#, so please be patient with me. The code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
using VirtualBox;
namespace AdapterDisableTest
{
class Program
{
static void Main(string[] args)
{
VirtualBoxClient vBoxClient = new VirtualBoxClient();
var machine = vBoxClient.VirtualBox.FindMachine("test");
machine.LockMachine(vBoxClient.Session, LockType.LockType_Write);
machine.GetNetworkAdapter(0).SetProperty("cableConnected", "0");
vBoxClient.Session.UnlockMachine();
Console.WriteLine(machine.GetNetworkAdapter(0).GetProperty("Cable Connected"));
Console.ReadLine();
}
}
}
I tried changing the lock to a shared lock as you suggested. However, when I did that I received the error 'The machine is not mutable (state is Running)'.
Ah yes, forgot to mention it : you also need to get a new machine object from the Session object. The first object you get is read-only, but you use the session lock to actually get another version of the object that is read/write.
Use session.getMachine().
Great! Thank you for the references. The code below now works to an extent. It is now setting the cableConnected to 0 as reported by the console output. However, the cable still shows connected in the VM itself. Just to give you an example of what I am trying to accomplish, it would be the same as executing "VBoxManage.exe controlvm test setlinkstate1 off" from the command prompt. What am I missing here?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
using VirtualBox;
namespace AdapterDisableTest
{
class Program
{
static void Main(string[] args)
{
VirtualBoxClient vBoxClient = new VirtualBoxClient();
IMachine machine = vBoxClient.VirtualBox.FindMachine("test");
Session machineSession = vBoxClient.Session;
machine.LockMachine(machineSession, LockType.LockType_Shared);
IMachine machineCopy = machineSession.Machine;
machineCopy.GetNetworkAdapter(0).SetProperty("cableConnected", "0");
machineCopy.SaveSettings();
machineSession.UnlockMachine();
Console.WriteLine(machine.GetNetworkAdapter(0).GetProperty("cableConnected"));
Console.ReadLine();
}
}
}
I take that back. It reports 0 or 1 as the cableConnected value (depending on the value that I assign to it in the application). However, nothing changes on the actual running VM. It has to be something simple that I am missing.
I guess that depends on what you consider a proper method lol. I have been trying to make the change via a command line call from a C# service, but I just can't get the service to run the cli command "VBoxManage.exe controlvm test setlinkstate1 off". I decided it may be best to do this via the SDK since it was going to be running as a service. The point of this is to install a service on user's machines so that it runs in background and will check for certain network conditions every so often. When these network conditions are met, it should shut off all network access for the VM. I am definitely open to other ideas as to how I should go about doing this. Can you make any recommendations?
Well I never touched C# for Virtualbox, but in Java I do have a setCableConnected() & getCableConnected() method, taking a boolean as argument. Don't you have the same?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
No, I don't appear to, at least not that I can see. What class are they methods of? I don't even see those listed in the reference site that you referred me to earlier.
You can find the info at INetworkAdapter.
In case of Java, there is an automatic set & get method for each property/attribute of the class, instead of directly editing it like in C++.
But looking at the SDK, it looks like you have a cabledConnected attribute, and that setProperty seems to be for something else all together.
What happens if you replace this line
That didn't work because it was looking for an int type. However, when I changed it to 0 instead of false, it worked perfectly. Strange how the SetProperty() method didn't work, but directly setting the variable did. Thank you so much for your help and patience!