bool CMSFT_DiskOnlineMethod::Process()

in host/AzureRecoveryLib/win32/WmiRecordProcessors.cpp [191:393]


bool CMSFT_DiskOnlineMethod::Process(IWbemClassObject *precordobj, IWbemServices* pNamespace)
{
	TRACE_FUNC_BEGIN;
	bool bprocessed = true;

	VARIANT vtProp, vtRet;
	HRESULT hr;
    
	IWbemClassObject* pClassDef = NULL;
	IWbemClassObject* pMethodSign = NULL;
	IWbemClassObject* pInParamInst = NULL;
	IWbemClassObject* pOutParams = NULL;

	do
	{
		//
		// Get Object Instance path. ObjectId is the primary key for MSFT_Disk class.
		//
		hr = precordobj->Get(_bstr_t("ObjectId"), 0, &vtProp, 0, 0);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not retrieved ObjectId for SMFT_Disk instance. Com error %s\n",
				        m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		//
		// Construct class path for the required MSFT_Disk instance
		//
		std::wstring objPath(L"MSFT_Disk.ObjectId=");
		objPath += L"\'";
		objPath += _bstr_t(vtProp);
		objPath += L"\'";
		VariantClear(&vtProp);
		
		//
		// Execute MSFT_Disk.SetAttributes to clear ReadOnly flag.
		//
		hr = pNamespace->GetObject(_bstr_t("MSFT_Disk"),
			                      0,
								  NULL,
								  &pClassDef,
								  NULL);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not get MSFT_Disk class object definition. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		hr = pClassDef->GetMethod(_bstr_t(METHOD_MSFT_DISK_SETATTRIBUTES),
			                      0,
								  &pMethodSign, 
								  NULL);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not get SMFT_Disk.SetAttributes definition method. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		hr = pMethodSign->SpawnInstance(0, &pInParamInst);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not instantiate SMFT_Disk.SetAttributes method InParams. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		vtProp.vt = VT_BOOL;
		vtProp.boolVal = FALSE;
		hr = pInParamInst->Put(_bstr_t("IsReadOnly"), 0, &vtProp, 0);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not set IsReadOnly Value as SMFT_Disk.SetAttributes method InParam. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		hr = pNamespace->ExecMethod(_bstr_t(objPath.c_str()),
			                        _bstr_t(METHOD_MSFT_DISK_SETATTRIBUTES),
			                        0,
			                        NULL,
			                        pInParamInst,
			                        &pOutParams,
			                        NULL);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not invoke SMFT_Disk.SetAttributes method. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		hr = pOutParams->Get(_bstr_t(PROP_RETURN_VALUE),
			                 0,
							 &vtRet,
							 0,
							 0);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not retrieved ReturnValue for SMFT_Disk.SetAttributes method. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}
		m_retCode = vtRet.uintVal;
		if (ERROR_SUCCESS != m_retCode)
		{
			TRACE_ERROR("Could not clear ReadOnly flag on the disk\n");
			bprocessed = false;
			break;
		}

		WMI_SAFE_RELEASE(pOutParams);

		//
		// Execute MSFT_Disk.Online method
		//
		hr = pNamespace->ExecMethod(_bstr_t(objPath.c_str()),
			_bstr_t(METHOD_MSFT_DISK_ONLINE),
			0,
			0,
			0,
			&pOutParams,
			0);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not invoke SMFT_Disk.Online method. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}

		//
		// Get the return value
		//
		hr = pOutParams->Get(_bstr_t(PROP_RETURN_VALUE), 0, &vtRet, 0, 0);
		if (FAILED(hr))
		{
			m_error_msg = _com_error(hr).ErrorMessage();

			TRACE_ERROR("Could not retrieved ReturnValue for SMFT_Disk.Online method. Com error %s\n",
				m_error_msg.c_str());

			bprocessed = false;
			break;
		}
		m_retCode = vtRet.uintVal;
		VariantClear(&vtRet);

		if (ERROR_SUCCESS != m_retCode)
		{
			TRACE_ERROR("Could not online the disk. MSFT_Disk.Online failed with error %u\n", m_retCode);

			bprocessed = false;
		}

	} while (false);

	//
	// Cleanup
	//
	VariantClear(&vtProp);
	VariantClear(&vtRet);

	WMI_SAFE_RELEASE(pClassDef);
	WMI_SAFE_RELEASE(pMethodSign);
	WMI_SAFE_RELEASE(pInParamInst);
	WMI_SAFE_RELEASE(pOutParams);
	
	TRACE_FUNC_END;
	return bprocessed;
}