[Solved] Error restored snapshot vm

Discussion about using the VirtualBox API, Tutorials, Samples.

[Solved] Error restored snapshot vm

Postby range » 24. Apr 2019, 11:45

Hi gues! I use VirtualBox and Virtual Box API Microsoft Com C++ for my project.
It’s VirtualBox 6.0.6 Software Developer Kit (SDK) and VirtualBox 6.0.6 platform packages .
I have a problem. I want to recover a snapshot system, that was created early, but the program shows errors.
My code with comments:

Code: Select all   Expand viewCollapse view
HRESULT rc;

IVirtualBoxClient *virtualBoxClient = nullptr;
IVirtualBox *virtualBox = nullptr;
IMachine *machine = nullptr;
ISession *session = nullptr;
IProgress *progress = nullptr;
ISnapshot* snapshot = nullptr;

BSTR sessiontype = SysAllocString(L"gui");
BSTR machineName = SysAllocString(L"Win7x64");


HRESULT rc = CoCreateInstance(CLSID_VirtualBoxClient, NULL, CLSCTX_INPROC_SERVER, IID_IVirtualBoxClient, (void**)&virtualBoxClient);

rc = virtualBoxClient->get_VirtualBox(&virtualBox);

// looking for a machine with a name Win7x64
rc = virtualBox->FindMachine(machineName, &machine);

// create a session object
rc = CoCreateInstance(CLSID_Session, NULL, CLSCTX_INPROC_SERVER, IID_ISession, (void**)&session);
if (!SUCCEEDED(rc))
{
printf("Error creating Session instance! rc = 0x%x\n", rc);
break;
}

// block the machine
machine->LockMachine(session, LockType::LockType_Shared);

// the name of the snapshot system
BSTR snapshotUUID = SysAllocString(L"Win7_test_snapshot");
// looking for a snapshot system
rc = machine->FindSnapshot(snapshotUUID, &snapshot);
// recover snapshot. ERROR: variable rc - E_NOTIMPL Not implemented.
rc = machine->RestoreSnapshot(snapshot, &progress);

printf("Starting VM, please wait ...\n");
//waiting for the end of the operation. Abnormal termination- progress == nullptr !
rc = progress->WaitForCompletion(-1);
// unlocking the machine
rc = session->UnlockMachine();
if (!SUCCEEDED(rc))
{
printf("Error restore state machine!\n");
break;
}


Can you help me, how to fix call method error “ RestoreSnapshot - E_NOTIMPL Not implemented” or how to restore a snapshot of the system correctly? Thank you.
Last edited by socratis on 1. May 2019, 21:38, edited 1 time in total.
Reason: Marked as [Solved].
range
 
Posts: 9
Joined: 24. Apr 2019, 08:03

Re: Error restored snapshot vm

Postby mpack » 24. Apr 2019, 16:13

You don't seem to be testing the return value of FindSnapshot()?
mpack
Site Moderator
 
Posts: 29827
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Error restored snapshot vm

Postby range » 24. Apr 2019, 17:03

mpack wrote:You don't seem to be testing the return value of FindSnapshot()?

Return value of FindSnapshot() is S_OK :(
range
 
Posts: 9
Joined: 24. Apr 2019, 08:03

Re: Error restored snapshot vm

Postby range » 25. Apr 2019, 20:24

Application build order:
1) Compiling VirtualBox.idl and getting dlldata.c, VirtualBox_h.h, VirtualBox_i.c, VirtualBox_p.c files to a console project
2) Creating a source code file in a console project
3) Compiling and running a console project.
Is that right?
Just in case - Visual Studio 2017 project: https://drive.google.com/open?id=1zcb3vuIRvYYZ8crr3L_cIbmSUJrj_DdL
range
 
Posts: 9
Joined: 24. Apr 2019, 08:03

Re: Error restored snapshot vm

Postby range » 25. Apr 2019, 22:40

I created a simple C # application, and got the exact same error there. What's the matter? This is mistake API ? I even changed the OS (virtual) many times.
Code: Select all   Expand viewCollapse view
static void Main(string[] args)
        {
            VirtualBox.VirtualBox virtualBox = new VirtualBox.VirtualBox();
            IMachine vmMachine = virtualBox.FindMachine("Win7x64VB");
            Session session = new Session();
            vmMachine.LockMachine(session, LockType.LockType_Shared);
            IConsole console = session.Console;

            // Restore snapshot
            ISnapshot snapShot = vmMachine.FindSnapshot("Win7_snapshot1");

            IProgress snapShotProgress = vmMachine.RestoreSnapshot(snapShot);
            snapShotProgress.WaitForCompletion(300000);

            // unlock before launch VMProcess
            session.UnlockMachine();

            IProgress launchVmProgess = vmMachine.LaunchVMProcess(session, "gui", "None");

            launchVmProgess.WaitForCompletion(300000);
        }

Screenshot exception: http://i-fotki.info/25/bfce220c3c452dc90878304b325d9c1abcaac2338884839.png.html
range
 
Posts: 9
Joined: 24. Apr 2019, 08:03

Re: Error restored snapshot vm

Postby range » 1. May 2019, 19:34

Hi gyes!
I found a solution of the problem. Snapshot should be restored with the system that is current for the session. So code will be as follows:

Code: Select all   Expand viewCollapse view
   
HRESULT rc = machine->LockMachine(session, LockType::LockType_Shared);
rc = machine->FindSnapshot(snapshotUUID, &snapshot);
IMachine* currentMachine(nullptr);
rc = session->get_Machine(&currentMachine);
rc = currentMachine->RestoreSnapshot(snapshot, &progress);
rc = progress->WaitForCompletion(300000);
rc = session->UnlockMachine();


It works with this code.
range
 
Posts: 9
Joined: 24. Apr 2019, 08:03

Re: Error restored snapshot vm

Postby socratis » 1. May 2019, 21:38

Thank you for the feedback and the solution! 8)
I hope someone finds it useful in the future. Marking as [Solved].
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
socratis
Site Moderator
 
Posts: 25894
Joined: 22. Oct 2010, 11:03
Location: Greece
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5


Return to The VirtualBox API

Who is online

Users browsing this forum: No registered users and 1 guest