// ************************************************************************** // // CAdvClientDlg::OnDiskdetails() // // Description: // Enumerates the properties of the C: drive using the 'GetNames()' // technique. The technique uses safearrays. // // Parameters: // None. // // Returns: // Nothing. // // Globals accessed: // None. // // Globals modified: // None. // //=========================================================================== void CAdvClientDlg::OnDiskdetails() { HRESULT hRes; long lLower, lUpper, lCount; SAFEARRAY *psaNames = NULL; BSTR PropName = NULL; VARIANT varString, pVal; WCHAR *pBuf; CString clMyBuff; IWbemClassObject *pDriveInst = NULL; IWbemQualifierSet *pQualSet = NULL; VariantInit(&varString); VariantInit(&pVal); m_outputList.ResetContent(); m_outputList.AddString(_T("working...")); //------------------------------- // Get the instance for C: drive. BSTR driveName = SysAllocString(L"Win32_LogicalDisk.DeviceID=\"C:\""); if (!driveName) { TRACE(_T("SysAllocString failed: not enough memory\n")); return; } BSTR cimType = SysAllocString(L"CIMTYPE"); if (!cimType) { SysFreeString(driveName); TRACE(_T("SysAllocString failed: not enough memory\n")); return; } BSTR keyQual = SysAllocString(L"key"); if (!keyQual) { SysFreeString(driveName); SysFreeString(cimType); TRACE(_T("SysAllocString failed: not enough memory\n")); return; } if((hRes = m_pIWbemServices->GetObject(driveName, 0L, NULL, &pDriveInst, NULL)) == S_OK) { m_outputList.ResetContent(); //------------------------------- // Get the property names if((hRes = pDriveInst->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, NULL, &psaNames)) == S_OK) { //------------------------------- // Get the upper and lower bounds of the Names array if((hRes = SafeArrayGetLBound(psaNames, 1, &lLower)) != S_OK) { TRACE(_T("Couldn't get safe array lbound\n")); SafeArrayDestroy(psaNames); return; } //------------------------------- if((hRes = SafeArrayGetUBound(psaNames, 1, &lUpper)) != S_OK) { TRACE(_T("Couldn't get safe array ubound\n")); SafeArrayDestroy(psaNames); return; } //------------------------------- // For all properties... for (lCount = lLower; lCount <= lUpper; lCount++) { //----------------------------------------------- // I'm formatting each property as: // name (type) ==> value //----------------------------------------------- //------------------------------- // get the property name for this element if((hRes = SafeArrayGetElement(psaNames, &lCount, &PropName)) == S_OK) { clMyBuff = PropName; // print variable type for property value clMyBuff += _T(" ("); // Get pointer to property qualifiers // this mess is due to the fact that system properties don't have qualifiers if ((pDriveInst->GetPropertyQualifierSet(PropName, &pQualSet)) == S_OK) { // Get and print syntax attribute (if any) if ((pQualSet->Get(cimType, 0L, &pVal, NULL)) == S_OK) { clMyBuff += V_BSTR(&pVal); } else if (hRes != WBEM_E_NOT_FOUND) { // some other error TRACE(_T("Could not get syntax qualifier\n")); break; } VariantClear(&pVal); //------------------------------- // If this is a key field, print an asterisk if(((hRes = pQualSet->Get(keyQual, 0L, &pVal, NULL)) == S_OK) && (pVal.boolVal)) { // Yes, it's a key clMyBuff += _T(")*"); } else if (hRes == WBEM_E_NOT_FOUND) { // not a key qualifier clMyBuff += _T(")"); } else { // some other error TRACE(_T("Could not get key qualifier\n")); break; } // done with the qualifierSet. if (pQualSet) { pQualSet->Release(); pQualSet = NULL; } } else { clMyBuff += _T(")"); } //endif pDriveClass->GetPropertyQualifierSet() //------------------------------- // Get the value for the property. if((hRes = pDriveInst->Get(PropName, 0L, &varString, NULL, NULL)) == S_OK) { // Print the value clMyBuff += _T(" ==> "); clMyBuff += ValueToString(&varString, &pBuf); m_outputList.AddString(clMyBuff); free(pBuf); // allocated by ValueToString() } else { TRACE(_T("Couldn't get Property Value\n")); break; } //endif pDriveClass->Get() VariantClear(&varString); VariantClear(&pVal); } else // SafeArrayGetElement() failed { TRACE(_T("Couldn't get safe array element\n")); break; } //endif SafeArrayGetElement() } // endfor // cleanup. SysFreeString(PropName); SysFreeString(keyQual); SysFreeString(cimType); SafeArrayDestroy(psaNames); VariantClear(&varString); VariantClear(&pVal); } else // pDriveClass->GetNames() failed { TRACE(_T("Couldn't GetNames\n")); } //endif pDriveClass->GetNames() // done with drive instance. if (pDriveInst) { pDriveInst->Release(); pDriveInst = NULL; } } //endif GetObject() }
IWbemClassObject* CConsumer::GetEventClass(IWbemClassObject* pEventCategoryClass, int EventType) { HRESULT hr = S_OK; HRESULT hrQualifier = S_OK; IEnumWbemClassObject *pClasses = NULL; IWbemClassObject *pClass = NULL; IWbemQualifierSet *pQualifiers = NULL; VARIANT varClassName; VARIANT varEventType; ULONG cnt = 0; BOOL FoundEventClass = FALSE; hr = pEventCategoryClass->Get(L"__RELPATH", 0, &varClassName,NULL,NULL); if(FAILED(hr)) { wprintf(L"pEventCategory->Get failed with 0x%x\r\n", hr); goto cleanup; } hr = g_pServices->CreateClassEnum(varClassName.bstrVal, WBEM_FLAG_SHALLOW | WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_USE_AMENDED_QUALIFIERS, NULL, &pClasses); if(FAILED(hr)) { wprintf(L"g_pServices->CreateClassEnum failed with 0x%x\r\n", hr); goto cleanup; } while(S_OK == hr) { hr = pClasses->Next(WBEM_INFINITE,1, &pClass, &cnt); if(FAILED(hr)) { wprintf(L"pClass->Next Failed with 0x%x\r\n", hr); goto cleanup; } if(!pClass) continue; hrQualifier = pClass->GetQualifierSet(&pQualifiers); if(FAILED(hrQualifier)) { wprintf(L"pClass->GetQualifierSet Failed with 0x%x\r\n", hrQualifier); pClass->Release(); pClass = NULL; goto cleanup; } hrQualifier = pQualifiers->Get(L"EventType", 0, &varEventType, NULL); if(FAILED(hrQualifier)) { wprintf(L"pQualifiers->Get Failed with 0x%x\r\n", hrQualifier); pQualifiers->Release(); pQualifiers = NULL; pClass->Release(); pClass = NULL; goto cleanup; } // If multiple events provide the same data, the EventType qualifier // will contain an array of types. Loop through the array and find a match. if (varEventType.vt & VT_ARRAY) { HRESULT hrSafe = S_OK; int ClassEventType; SAFEARRAY* pEventTypes = varEventType.parray; for (LONG i=0; (ULONG)i < pEventTypes->rgsabound->cElements; i++) { hrSafe = SafeArrayGetElement(pEventTypes, &i, &ClassEventType); if (ClassEventType == EventType) { FoundEventClass = TRUE; break; //for loop } } } else { if (varEventType.intVal == EventType) { FoundEventClass = TRUE; } } VariantClear(&varEventType); if (TRUE == FoundEventClass) { break; //while loop } pClass->Release(); pClass = NULL; } cleanup: if (pClasses) { pClasses->Release(); pClasses = NULL; } if (pQualifiers) { pQualifiers->Release(); pQualifiers = NULL; } VariantClear(&varClassName); VariantClear(&varEventType); return pClass; }
IWbemClassObject* CConsumer::GetEventCategoryClass(BSTR bstrclassGuid, int Version) { HRESULT hr = S_OK; HRESULT hrQualifier = S_OK; IEnumWbemClassObject* pClasses = NULL; IWbemClassObject* pClass = NULL; IWbemQualifierSet* pQualifiers = NULL; ULONG cnt = 0; VARIANT varGuid; VARIANT varVersion; hr = g_pServices->CreateClassEnum(_bstr_t(L"EventTrace"), WBEM_FLAG_DEEP | WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_USE_AMENDED_QUALIFIERS, NULL, &pClasses); if(FAILED(hr)) { wprintf(L"CreateClassEnum failed with 0x%x\r\n", hr); goto cleanup; } while(S_OK == hr) { hr = pClasses->Next(WBEM_INFINITE, 1, &pClass, &cnt); if(FAILED(hr)) { wprintf(L"pClasses->Next Failed with 0x%x\r\n", hr); goto cleanup; } hrQualifier = pClass->GetQualifierSet(&pQualifiers); if(pQualifiers) { hrQualifier = pQualifiers->Get(L"Guid", 0, &varGuid, NULL); if(SUCCEEDED(hrQualifier) && 0 == _wcsicmp(varGuid.bstrVal, bstrclassGuid)) { hrQualifier = pQualifiers->Get(L"EventVersion", 0, &varVersion, NULL); if(SUCCEEDED(hrQualifier)) { if(Version == varVersion.intVal) { break; } VariantClear(&varVersion); } else if(WBEM_E_NOT_FOUND == hrQualifier) { break; } VariantClear(&varGuid); } pQualifiers->Release(); pQualifiers = NULL; } pClass->Release(); pClass = NULL; } cleanup: if(pClasses) { pClasses->Release(); pClasses = NULL; } if(pQualifiers) { pQualifiers->Release(); pQualifiers = NULL; } VariantClear(&varGuid); VariantClear(&varVersion); return pClass; }
SCODE CreateClass( IWbemServices * pNamespace, IWbemClassObject ** pNewClass, WCHAR * pwcClassName, IWbemContext *pCtx) { SCODE sc; sc = pNamespace->GetObject(NULL, 0, pCtx, pNewClass, NULL); if(sc != S_OK) return sc; VARIANT v; // Set the class name. v.vt = VT_BSTR; v.bstrVal = SysAllocString(pwcClassName); if (!v.bstrVal) return WBEM_E_OUT_OF_MEMORY; sc = (*pNewClass)->Put(L"__class", 0, &v, 0); VariantClear(&v); // Set the key property value. v.vt = VT_NULL; sc = (*pNewClass)->Put(L"MyKey", 0, &v, CIM_STRING); // Set the number property value. sc = (*pNewClass)->Put(L"MyValue", 0, &v, CIM_SINT32); // Set the key qualifier IWbemQualifierSet* pQualSet = NULL; sc = (*pNewClass)->GetPropertyQualifierSet(L"MyKey", &pQualSet); if(pQualSet) { v.vt = VT_BOOL; v.boolVal = VARIANT_TRUE; sc = pQualSet->Put(L"Key", &v, 0); pQualSet->Release(); } // Set the dynamic qualifier sc = (*pNewClass)->GetQualifierSet(&pQualSet); if(pQualSet) { v.vt = VT_BOOL; v.boolVal = VARIANT_TRUE; sc = pQualSet->Put(L"Dynamic", &v, 0); v.vt = VT_BSTR; v.bstrVal = SysAllocString(L"ClassProvSamp"); if (v.bstrVal) sc = pQualSet->Put(L"Provider", &v, 0); else sc = WBEM_E_OUT_OF_MEMORY; VariantClear(&v); pQualSet->Release(); } return sc; }