Quesiton Regarding cableConnected Property

Discussion about using the VirtualBox API, Tutorials, Samples.

Quesiton Regarding cableConnected Property

Postby KMills » 12. Aug 2013, 17:31

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   Expand viewCollapse view
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();

        }
    }
}
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby noteirak » 12. Aug 2013, 17:46

For this line
Code: Select all   Expand viewCollapse view
machine.LockMachine(vBoxClient.Session, LockType.LockType_Write);

Use LockType.Shared instead of Write, so it would look like
Code: Select all   Expand viewCollapse view
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.
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: Quesiton Regarding cableConnected Property

Postby KMills » 12. Aug 2013, 17:57

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)'.
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby noteirak » 12. Aug 2013, 19:01

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
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: Quesiton Regarding cableConnected Property

Postby KMills » 12. Aug 2013, 20:07

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   Expand viewCollapse view
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();

        }
    }

}
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby KMills » 12. Aug 2013, 20:11

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.
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby noteirak » 12. Aug 2013, 20:32

Well, the code seems right, but don't you have a proper method to change the cable state instead of using .setProperty() ?
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: Quesiton Regarding cableConnected Property

Postby KMills » 12. Aug 2013, 20:57

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?
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby noteirak » 12. Aug 2013, 21:18

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://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: Quesiton Regarding cableConnected Property

Postby KMills » 12. Aug 2013, 21:55

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.
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby noteirak » 12. Aug 2013, 23:11

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   Expand viewCollapse view
machineCopy.GetNetworkAdapter(0).SetProperty("cableConnected", "0");

by this line (or whatever works in C#)
Code: Select all   Expand viewCollapse view
machineCopy.GetNetworkAdapter(0).cableConnected = false;
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: Quesiton Regarding cableConnected Property

Postby KMills » 13. Aug 2013, 13:27

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!
KMills
 
Posts: 7
Joined: 12. Aug 2013, 17:18

Re: Quesiton Regarding cableConnected Property

Postby noteirak » 13. Aug 2013, 14:51

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


Return to The VirtualBox API

Who is online

Users browsing this forum: No registered users and 1 guest