我试图在c ++中使用wmi对象找到虚拟机中存在的VM的IntegrationServiceVersionState
可以通过调用root \ virtualization \ v2 namespace中存在的Msvm_VirtualSystemManagementService类的GetSummaryInformation方法来访问IntegrationServiceVersionState
我遇到错误的代码片段是
// Set up to call the Msvm_VirtualSystemManagementService::GetSummaryInformation method
BSTR ClassName = SysAllocString(L"Msvm_VirtualSystemManagementService");
BSTR MethodName = SysAllocString(L"GetSummaryInformation");
cout << "setup initialised" << endl;
//get class
IWbemClassObject* pClass = NULL;
hr = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
cout << "class object got" << endl;
GetWMIError(hr);
//to get the path of the class
IEnumWbemClassObject* pEnumerator1 = NULL;
hr = pSvc->ExecQuery(
BSTR(L"WQL"),
BSTR(L"select * from MSVM_VirtualSystemManagementService "),/*where Caption=\"Virtual Machine\"*/
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator1);
IWbemClassObject* pVirtualSystemManagementServiceObj = NULL;
ULONG uReturn1 = 0;
hr = pEnumerator1->Next(WBEM_INFINITE, 1, &pVirtualSystemManagementServiceObj, &uReturn1);
if (uReturn1 == 0)
{
cout << " couldnt load class path " << endl;
}
VARIANT classpath;
hr = pVirtualSystemManagementServiceObj->Get(L"__Path", 0, &classpath, 0, 0);
wcout << " __Path : " << classpath.bstrVal << endl;
// Get Method
IWbemClassObject* pInParamsDefinition = NULL;
hr = pClass->GetMethod(MethodName, 0, &pInParamsDefinition, NULL);
cout << "method object got" << endl;
GetWMIError(hr);
// get spawn instance
IWbemClassObject* pClassInstance = NULL;
hr = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
cout << "SpAwn instanaaace e" << endl;
GetWMIError(hr);
//setting data
VARIANT SetData ;
VariantInit(&SetData);
SetData.vt = VT_NULL;
hr = pClassInstance->Put(L"SettingData", 0, &SetData, 0);
if (FAILED(hr))
{
cout << "failed to load setting data parameter" << endl;
}
GetWMIError(hr);
cout << "1st parameter set" << endl;
//requested information
VARIANT reqInfo;
VariantInit(&reqInfo);
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 2;
CComSafeArray<int> arr(5);
arr[0] = 1;
arr[1] = 123;
long nIndex = 0;
psa = SafeArrayCreate(VT_I4, 1, rgsabound);
hr = SafeArrayPutElement(psa, &nIndex, &arr[0]);
nIndex++;
SafeArrayPutElement(psa, &nIndex, &arr[1]);
reqInfo.vt = VT_ARRAY | VT_I4;
reqInfo.parray = psa;
hr = pClassInstance->Put(L"RequestedInformation", 0, &reqInfo, 0);
GetWMIError(hr);
cout << "2nd parameter set" << endl;
//execute method
IWbemClassObject* pOutParams = NULL;
hr = pSvc->ExecMethod(classpath.bstrVal, MethodName, 0, NULL, pClassInstance, &pOutParams, NULL);
GetWMIError(hr);
if (FAILED(hr))
{
cout << "Could not execute method. Error code = 0x" << hex << hr << endl;
}
GetWMIError(hr);
cout << "method executed" << endl;
上面的代码工作正常,但在下面的代码中面临问题
VARIANT varReturnValue;
VariantInit(&varReturnValue);
hr = pOutParams->Get(_bstr_t(L"ReturnValue"), 0,&varReturnValue, NULL, 0);
if (SUCCEEDED(hr))
{
if (varReturnValue.vt == VT_I4)
{
CIMTYPE ct;
VARIANT varJob;
VariantInit(&varJob);
hr = pOutParams->Get(_bstr_t(L"SummaryInformation"), 0, &varJob, &ct, 0);
if (SUCCEEDED(hr))
{
if (varJob.vt == VT_ARRAY | VT_UNKNOWN)
{
VARIANT myvar;
myvar.vt = VT_ARRAY | VT_UNKNOWN;
SAFEARRAY* pSafeArray = V_ARRAY(&myvar);
long lBound, uBound;
SafeArrayGetLBound(pSafeArray, 1, &lBound);
SafeArrayGetUBound(pSafeArray, 1, &uBound);
cout << lBound << endl;
cout << uBound << endl;
UINT16 Element = NULL;
for (long i = lBound; i <= uBound; i++)
{
hr = SafeArrayGetElement(pSafeArray, &i, &Element);
wcout << Element << endl;
}
}
}
}
}
我正确获得了VM的数量,但在获取数组元素时遇到了错误。它只是抛出一些垃圾值。 请一些人帮忙纠正这个问题