Page 1 of 1

Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 17:31
by KMills
Hello,

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:

Code: Select all

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();

        }
    }
}

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 17:46
by noteirak
For this line

Code: Select all

machine.LockMachine(vBoxClient.Session, LockType.LockType_Write);
Use LockType.Shared instead of Write, so it would look like

Code: Select all

machine.LockMachine(vBoxClient.Session, LockType.LockType_Shared);
You don't need to obtain a write lock for this setting - you don't need a write lock of anything that can be set while the VM is in RUNNING state.

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 17:57
by KMills
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)'.

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 19:01
by noteirak
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().

All this is explained in the SDK Reference at the IMachine::lockMachine() and ISession

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 20:07
by KMills
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?

Code: Select all

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();

        }
    }

}

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 20:11
by KMills
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.

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 20:32
by noteirak
Well, the code seems right, but don't you have a proper method to change the cable state instead of using .setProperty() ?

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 20:57
by KMills
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?

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 21:18
by noteirak
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?

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 21:55
by KMills
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.

Re: Quesiton Regarding cableConnected Property

Posted: 12. Aug 2013, 23:11
by noteirak
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

Code: Select all

machineCopy.GetNetworkAdapter(0).SetProperty("cableConnected", "0");
by this line (or whatever works in C#)

Code: Select all

machineCopy.GetNetworkAdapter(0).cableConnected = false;

Re: Quesiton Regarding cableConnected Property

Posted: 13. Aug 2013, 13:27
by KMills
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!

Re: Quesiton Regarding cableConnected Property

Posted: 13. Aug 2013, 14:51
by noteirak
Glad you manage to make it work :) I wasn't sure what the C# exactly expected, but it's a good reference for the future.