Home > OS >  The Hyper - v c create virtual machine snapshot through WMI CreateSnapshot success did not create a
The Hyper - v c create virtual machine snapshot through WMI CreateSnapshot success did not create a

Time:05-22

Windows server 2016
I CreateSnapshot calling through the wmi, ready to generate snapshot, interface call returns 0, the call is successful, did not produce a snapshot, the following is the code

 BOOL CWMIUtil: : CreateSnapshot (ElementName would * and would be * strName) 
{
Retrieves the hr;
IWbemClassObject * pVirtualSysSnapshotService=NULL;
IWbemClassObject * pVirutalSysMgrService=NULL;

IWbemContext * pCtx=0;
IWbemCallResult * pResult=0;

//retrieve the virtual machine snapshot instance
BSTR snapshotClassPath=SysAllocString (L "Msvm_VirtualSystemSnapshotService");
BSTR VirtualSysMgrService=SysAllocString (L "Msvm_VirtualSystemManagementService");
Do {

//for the virtual machine management instance objects
Hr=m_pSvc - & gt; GetObject (
VirtualSysMgrService,
0,
NULL,
& PVirutalSysMgrService,
NULL);

Failed to get instance objects//
{if (FAILED (hr))
STD: : cout & lt; <"Failed to get the class object (Msvm_VirtualSystemManagementService). The Error code=0 x" & lt; Wprintf (L "Failed to get the class object (Msvm_VirtualSystemManagementService). The Error code=0 x \ n");
break;
}

STD: : cout & lt; <"Succeeded to get object (Msvm_VirtualSystemManagementService.)" & lt;

//acquisition system snapshot instance objects
Hr=m_pSvc - & gt; GetObject (
SnapshotClassPath,
0,
NULL,
& PVirtualSysSnapshotService,
NULL);

Failed to get instance objects//
{if (FAILED (hr))
STD: : cout & lt; <"Failed to get the class object (Msvm_VirtualSystemSnapshotService). The Error code=0 x" & lt; Wprintf (L "Failed to get the class object (Msvm_VirtualSystemSnapshotService). The Error code=0 x \ n");
break;
}

//Call the GetTargetComputer
IEnumWbemClassObject * pComputer=NULL;
Would be comsystem [100]=L "SELECT * FROM Msvm_ComputerSystem";
Hr=m_pSvc - & gt; ExecQuery (
BSTR "WQL" (L),
BSTR (comsystem),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
& PComputer
);
{if (FAILED (hr))
STD: : cout & lt; <"SELECT * FROM msvm_computersystem, Error code: 0 x % 08 lx" & lt; break;
}
STD: : cout & lt; <"Succeeded to query pComputer." & lt;
IWbemClassObject * pObjectVirtualMachine=NULL;

ULONG pReturn1=0;
Wprintf (L "ElementName: % s \ n", strName);

while (! FAILED (pComputer - & gt; Next (WBEM_INFINITE, 1, & amp; PObjectVirtualMachine, & amp; PReturn1))) {
The VARIANT vt.
//Get the value of the path property from Msvm_VirtualSystemSnapshotService Class
Hr=pObjectVirtualMachine - & gt; Get (L "ElementName" 0, & amp; Vt, 0, 0);
{if (FAILED (hr))
STD: : wcout & lt; <"Failed to get the vm 's path." & lt; }
Wprintf (L "ElementName: % s \ n", n. BstrVal);

Cstrings csValue;
The switch (n. N)
{
Case VT_BSTR:
CsValue=https://bbs.csdn.net/topics/CString (CStringW (vt. BstrVal));
break;

Default:
break;
}

If (WCSCMP (strName, csValue)==0) {
Break;;
}
PObjectVirtualMachine=NULL;
}

If (pObjectVirtualMachine==NULL) {
Printf (" could not find to create a snapshot of the virtual machine \ n ");
break;
}


//get Msvm_VirtualSystemSnapshotSettingData class object
IWbemClassObject * pVirtualSystemSnaphotSettingData=https://bbs.csdn.net/topics/NULL;
Hr=m_pSvc - & gt; GetObject (L "Msvm_VirtualSystemSnapshotSettingData," 0, NULL, & amp; PVirtualSystemSnaphotSettingData, & amp; PResult);


//create Msvm_VirtualSystemSnapshotSettingData class instances
IWbemClassObject * pInpInstOfSnapshotSettingData=https://bbs.csdn.net/topics/NULL;
Hr=pVirtualSystemSnaphotSettingData - & gt; SpawnInstance (
0,
& PInpInstOfSnapshotSettingData);
{if (FAILED (hr))
Wprintf (L "Error SpawnInstance Msvm_VirtualSystemSnapshotSettingData: 0 x % 08 lx \ n", hr);
break;
}
STD: : cout & lt; <"Succeeded to created the instance (Msvm_VirtualSystemSnapshotSettingData.)" & lt; //retrieve the computer system will be a snapshot of the virtual machine instance of the
The VARIANT ConsistencyLevel;
VariantInit (& amp; ConsistencyLevel);
V_VT (& amp; ConsistencyLevel)=VT_BSTR;
V_BSTR (& amp; ConsistencyLevel)=SysAllocString (L "2");

Hr=pInpInstOfSnapshotSettingData - & gt; Put (L "ConsistencyLevel," 0, & amp; ConsistencyLevel CIM_UINT8);
{if (FAILED (hr))
Wprintf (L "Failed to set the property (ConsistencyLevel), Erorr code: 0 x % 08 lx \ n", hr);
break;
}
Wprintf (L "Succeeded to set the property (ConsistencyLevel) \ n");
VariantClear (& amp; ConsistencyLevel);

/*
The VARIANT IgnoreNonSnapshottableDisks;
V_VT (& amp; IgnoreNonSnapshottableDisks)=VT_BOOL;
V_UI8 (& amp; IgnoreNonSnapshottableDisks)=true;

Hr=pInpInstOfSnapshotSettingData - & gt; Put (L "IgnoreNonSnapshottableDisks," 0, & amp; IgnoreNonSnapshottableDisks CIM_BOOLEAN);
{if (FAILED (hr))
Wprintf (L "Failed to set the property (IgnoreNonSnapshottableDisks), Erorr code: 0 x % 08 lx \ n", hr);
break;
}
VariantClear (& amp; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related