Discussion:
[vbox-dev] Problem with restoring a snapshot via API: Console is null
Daniel Dreibrodt
2014-07-02 13:52:05 UTC
Permalink
Hello,

I?m developing a C# application that controls VMs, starts them, stops them and runs programs in them. It should also reset the VM to a previously saved snapshot.

But I get a NullReferenceException because the Console object of my session is null, although I call LockMachine.

Here is the code with which I try to reset the machine after it has been powered down.

public void Reset(string snapshotName = "BeforeTest")
{
Logger.WriteLine("Resetting the VM");

var writeSession = new SessionClass();

m_Machine.LockMachine(writeSession, LockType.LockType_Write);

int waitCycles = 0;
while (writeSession.State != SessionState.SessionState_Locked & waitCycles < 100)
{
Thread.Sleep(10);
waitCycles++;
}
if (writeSession.State != SessionState.SessionState_Locked)
throw new VirtualMachineException("The VM could not be locked!");

var console = writeSession.Console;
if(console == null)
throw new VirtualMachineException("The Console is null!"); //This is where the problem is found

var snapshot = m_Machine.FindSnapshot(snapshotName);
if (snapshot == null)
throw new VirtualMachineException("Could not find Snapshot \"" + snapshotName + "\"");

var progress = console.RestoreSnapshot(snapshot);
progress.WaitForCompletion(30000);

writeSession.UnlockMachine();
waitCycles = 0;
while (writeSession.State != SessionState.SessionState_Unlocked & waitCycles < 100)
{
Thread.Sleep(10);
waitCycles++;
}

Logger.WriteLine(" The VM has been reset to the snapshot \"" + snapshotName + "\"");
}

I?m using VirtualBox 4.3.12 and the COM API. How can I properly restore a snapshot? Why Is the Console null?

Best Regards,
Daniel

[cid:image772942.PNG at 45783a2b.4a82e822]


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.virtualbox.org/pipermail/vbox-dev/attachments/20140702/2b6adc55/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image772942.PNG
Type: image/png
Size: 22112 bytes
Desc: image772942.PNG
Url : Loading Image...
Klaus Espenlaub
2014-07-03 14:41:31 UTC
Permalink
Daniel,
Post by Daniel Dreibrodt
Hello,
I?m developing a C# application that controls VMs, starts them, stops
them and runs programs in them. It should also reset the VM to a
previously saved snapshot.
But I get a NullReferenceException because the Console object of my
session is null, although I call LockMachine.
Here is the code with which I try to reset the machine after it has been powered down.
The code looks fine... I wonder if this is some language binding
specific problem. Not that it would make much sense, the call isn't
doing anything complex like returning a safearray.

Your code is rather close to what VBoxManage does, so I'm puzzled.

Klaus
Daniel Dreibrodt
2014-07-04 05:51:30 UTC
Permalink
Hello Klaus,

I've wondered, too. But I just now found the source of the problem. My Visual Studio project was set to build a 32-bit assembly, but I run under a 64-bit Windows with a 64-bit Virtual Box installed. After I changed the project to build a universal "Any CPU" assembly the code worked fine.

Best Regards,
Daniel




-----Original Message-----
From: Klaus Espenlaub [mailto:klaus.espenlaub at oracle.com]
Sent: Donnerstag, 3. Juli 2014 16:42
To: vbox-dev at virtualbox.org
Subject: Re: [vbox-dev] Problem with restoring a snapshot via API: Console is null

Daniel,
Post by Daniel Dreibrodt
Hello,
I?m developing a C# application that controls VMs, starts them, stops
them and runs programs in them. It should also reset the VM to a
previously saved snapshot.
But I get a NullReferenceException because the Console object of my
session is null, although I call LockMachine.
Here is the code with which I try to reset the machine after it has been powered down.
The code looks fine... I wonder if this is some language binding specific problem. Not that it would make much sense, the call isn't doing anything complex like returning a safearray.

Your code is rather close to what VBoxManage does, so I'm puzzled.

Klaus
Klaus Espenlaub
2014-07-04 10:09:19 UTC
Permalink
Daniel,
Post by Daniel Dreibrodt
Hello Klaus,
I've wondered, too. But I just now found the source of the problem. My Visual Studio project was set to build a 32-bit assembly, but I run under a 64-bit Windows with a 64-bit Virtual Box installed. After I changed the project to build a universal "Any CPU" assembly the code worked fine.
Ah right, this *is* one of the remaining todos... in general our COM
components are both available from 32 and 64 bit processes (was a rather
complex exercise to get this done, there is virtually no documentation),
but the Console stuff currently still contains a technical problem
(linking issue) in the 32 bit variant and is therefore "left out".

You now use it from a 64 bit process, and thus the problem is gone.

Klaus
Post by Daniel Dreibrodt
Best Regards,
Daniel
-----Original Message-----
From: Klaus Espenlaub [mailto:klaus.espenlaub at oracle.com]
Sent: Donnerstag, 3. Juli 2014 16:42
To: vbox-dev at virtualbox.org
Subject: Re: [vbox-dev] Problem with restoring a snapshot via API: Console is null
Daniel,
Post by Daniel Dreibrodt
Hello,
I?m developing a C# application that controls VMs, starts them, stops
them and runs programs in them. It should also reset the VM to a
previously saved snapshot.
But I get a NullReferenceException because the Console object of my
session is null, although I call LockMachine.
Here is the code with which I try to reset the machine after it has been powered down.
The code looks fine... I wonder if this is some language binding specific problem. Not that it would make much sense, the call isn't doing anything complex like returning a safearray.
Your code is rather close to what VBoxManage does, so I'm puzzled.
Klaus
Loading...