Quantcast
Channel: Hyper-V forum
Viewing all articles
Browse latest Browse all 8743

C++ application to detach a virtual hard disk from a VM

$
0
0

As part of an application to detach a vhd from a VM, I am trying to execute the "RemoveResourceSettings" method ofMsvm_VirtualSystemManagementService class(https://msdn.microsoft.com/en-us/library/hh850278(v=vs.85).aspx).

Below if the mof of the method:

uint32 RemoveResourceSettings(
  [in]  CIM_ResourceAllocationSettingData REF ResourceSettings[],
  [out] CIM_ConcreteJob                   REF Job
);

The input parameter to be passed to the method should be an array of references to instances of the CIM_ResourceAllocationSettingData class, where each instance represents the settings of a virtual resource within a virtual machine configuration that are to be removed. 

With the below code, the function execMethod is throwing the error "0x8004102f" - "Parameters provided for the method are not valid". Please let me know if you have any clue as to what could be going wrong here.
    // Use the IWbemServices pointer(pSvc) to make requests in WMI namespace \ROOT\\VIRTUALIZATION\V2---

	BSTR VirtualSystemManagementService_ClassName = SysAllocString(L"Msvm_VirtualSystemManagementService");
	BSTR RemoveResourceSettings_MethodName = SysAllocString(L"RemoveResourceSettings");

	//Get resource allocation settings of the disk to be removed
	IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
		bstr_t("select * from Msvm_ResourceAllocationSettingData where InstanceID like 'Microsoft:A87CEE11-889C-44E0-8A41-44452ABD0BEE%83F8638B-8DCA-4152-9EDA-2CA8B33039B4%1%1%D'"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,&pEnumerator);

	IWbemClassObject *resourceAllocationSettingInstance;
	ULONG uReturn = 0;

	hres = pEnumerator->Next(WBEM_INFINITE, 1,&resourceAllocationSettingInstance, &uReturn);

	VARIANT vtProp;
	hres = resourceAllocationSettingInstance->Get(L"InstanceID", 0, &vtProp, 0, 0);
	wcout << "InstanceID(The disk drive to be removed) : " << vtProp.bstrVal << endl;

	hres = resourceAllocationSettingInstance->Get(L"__PATH", 0, &vtProp, 0, 0);
	wcout << "__PATH(The disk drive to be removed) : " << vtProp.bstrVal << endl;

	BSTR resourceAllocationSettingInstanceString= vtProp.bstrVal;

	IWbemClassObject* VirtualSystemManagementService_Class = NULL;
    hres = pSvc->GetObject(VirtualSystemManagementService_ClassName, 0, NULL, &VirtualSystemManagementService_Class, NULL);

	IWbemClassObject* pInParamsDefinition = NULL;
	IWbemClassObject* pOutParams = NULL;
	hres = VirtualSystemManagementService_Class->GetMethod(RemoveResourceSettings_MethodName, 0,&pInParamsDefinition, NULL);

	cout << "After GetMethod\n";

	IWbemClassObject* VirtualSystemManagementServiceClassInstance = NULL;
	hres = pInParamsDefinition->SpawnInstance(0, &VirtualSystemManagementServiceClassInstance);

	//Get a pointer to the "resourceAllocationSettingInstance" which should be detached or removed from the corresponding VM configuration
	void **ppvObject;
	const IID riid = IID_IUnknown;

	hres = resourceAllocationSettingInstance->QueryInterface(riid, ppvObject);
	if (FAILED(hres))
    {
        cout << "Failed to QueryInterface = 0x"<< hex << hres << endl;
        return 1;                  // Program has failed.
    }
	VARIANT object_var;
	VariantInit(&object_var);
	object_var.vt = VT_UNKNOWN;
	object_var.punkVal = (IUnknown*)*ppvObject;


	CComSafeArray<VARIANT> resourceSettingsInputSafeArray;
	resourceSettingsInputSafeArray.Add(object_var);

	VARIANT resourceAllocationSettingVariant;
	VariantInit(&resourceAllocationSettingVariant);
	resourceAllocationSettingVariant.parray = resourceSettingsInputSafeArray.Detach();
	resourceAllocationSettingVariant.vt = VT_ARRAY | VT_VARIANT ;

	VirtualSystemManagementServiceClassInstance->Put(L"ResourceSettings", 0, &resourceAllocationSettingVariant, NULL);

	if (FAILED(hres))
    {
        cout << "Failed to set input parameter. Error code = 0x"<< hex << hres << endl;
		return 1;                  // Program has failed.
	}

	VARIANT vtPath;
	VariantInit(&vtPath);
	if(FAILED(VirtualSystemManagementService_Class->Get(L"__Path", 0, &vtPath, NULL, NULL)))
	{
		cout << "Object has no __Path!" << endl;
		VirtualSystemManagementService_Class->Release();
	}
	else
	{
		wcout << "Object path on which method should be executed: " << vtPath.bstrVal <<endl;
	}

	hres = pSvc->ExecMethod(vtPath.bstrVal, RemoveResourceSettings_MethodName, 0,
    NULL, VirtualSystemManagementServiceClassInstance, &pOutParams, NULL);

	if (FAILED(hres))
    {
        cout << "Could not execute method. Error code = 0x"<< hex << hres << endl;
        return 1;               // Program has failed.
    }



Viewing all articles
Browse latest Browse all 8743

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>