WMIString::WMIString(const VARIANT & var) { try { *this = WMIString(_bstr_t(_variant_t(var))); } catch(...) { } }
///////////////////////////////////////////////////////////////////////////// // WMIInstanceProvider::enumerateInstances // // /////////////////////////////////////////////////////////////////////////// Array<CIMInstance> WMIInstanceProvider::enumerateInstances( const String& nameSpace, const String& userName, const String& password, const String& className, Boolean deepInheritance, Boolean localOnly, Boolean includeQualifiers, Boolean includeClassOrigin, const CIMPropertyList& propertyList) { HRESULT hr; long lCount = 0; DWORD dwReturned; CComPtr<IEnumWbemClassObject> pInstEnum; CComPtr<IWbemClassObject> pInstance; Array<CIMInstance> namedInstances; PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WMIInstanceProvider::enumerateInstances()"); setup(nameSpace, userName, password); PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL3, "enumerateInstances - deepInheritance %x, localOnly %x, " "includeQualifiers %x, includeClassOrigin %x", deepInheritance, localOnly, includeQualifiers, includeClassOrigin)); if (!m_bInitialized) { PEG_TRACE_CSTRING(TRC_WMIPROVIDER, Tracer::LEVEL1, "enumerateInstances - m_bInitialized is false; throw exception"); throw CIMException(CIM_ERR_FAILED); } // retrieve instance enumeration object if (!(_collector->getInstanceEnum(&pInstEnum, className, deepInheritance))) { if (pInstEnum) pInstEnum.Release(); throw CIMException(CIM_ERR_FAILED); } // set proxy security on pInstEnum bool bSecurity = _collector->setProxySecurity(pInstEnum); // Get the instances and append them to the array hr = pInstEnum->Next(WBEM_INFINITE, 1, &pInstance, &dwReturned); while (SUCCEEDED(hr) && (1 == dwReturned)) { //get class from the returned instance //it will avoid "type mismatch" exceptions //when deepInheritance is true and instances //of subclasses are returned CComVariant vTmpClassName; String strTmpClassName; if (pInstance->Get(L"__CLASS", 0, &vTmpClassName, NULL, NULL) == S_OK) { strTmpClassName = WMIString(vTmpClassName); } CIMInstance tempInst(strTmpClassName); if (_collector->getCIMInstance(pInstance, tempInst, localOnly, includeQualifiers, includeClassOrigin, propertyList, TRUE)) { //new code CIMObjectPath tempRef; CComVariant vAux; char* strAux=NULL; //set hostname if (pInstance->Get(L"__SERVER", 0, &vAux, NULL, NULL) != S_OK) throw CIMException(CIM_ERR_FAILED, "Failed to retrieve WMI Data."); strAux = new char[wcslen(vAux.bstrVal)+1]; if (strAux == NULL) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Out of Memory."); wcstombs(strAux, vAux.bstrVal, wcslen(vAux.bstrVal)+1); tempRef.setHost(strAux); delete [] strAux; strAux = NULL; vAux.Clear(); //set class name if (pInstance->Get(L"__CLASS", 0, &vAux, NULL, NULL) != S_OK) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Failed to retrieve WMI Data."); strAux = new char[wcslen(vAux.bstrVal)+1]; if (strAux == NULL) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Out of Memory."); wcstombs(strAux, vAux.bstrVal, wcslen(vAux.bstrVal)+1); tempRef.setClassName(strAux); delete [] strAux; strAux = NULL; vAux.Clear(); //set namespace if (pInstance->Get(L"__NAMESPACE", 0, &vAux, NULL, NULL) != S_OK) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Failed to retrieve WMI Data."); strAux = new char[wcslen(vAux.bstrVal)+1]; if (strAux == NULL) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Out of Memory."); wcstombs(strAux, vAux.bstrVal, wcslen(vAux.bstrVal)+1); //converts '\' to '/' _translateBackslashes(strAux); tempRef.setNameSpace(strAux); delete [] strAux; strAux = NULL; vAux.Clear(); //get key bindings SAFEARRAY * aNames; if (pInstance->GetNames(NULL, WBEM_FLAG_KEYS_ONLY, NULL, &aNames) != S_OK) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Failed to retrieve WMI Data."); LONG lLBuond; LONG lUBuond; SafeArrayGetLBound(aNames, 1, &lLBuond); SafeArrayGetUBound(aNames, 1, &lUBuond); Array<CIMKeyBinding> keyBindings; for(LONG i = lLBuond; i <=lUBuond; i++) { CComBSTR bstrName; SafeArrayGetElement(aNames, &i, &bstrName); char * strPropertyName = new char[bstrName.Length()+1]; if (strPropertyName == NULL) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Out of Memory."); wcstombs(strPropertyName, bstrName, bstrName.Length()+1); CIMName keyname(strPropertyName); Uint32 Index = tempInst.findProperty(keyname); if (Index == PEG_NOT_FOUND) throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, "Failed to retrieve WMI Data."); CIMValue keyvalue = tempInst.getProperty(Index).getValue(); CIMKeyBinding key(keyname, keyvalue); keyBindings.append(key); delete [] strPropertyName; } SafeArrayDestroy(aNames); tempRef.setKeyBindings(keyBindings); tempInst.setPath(tempRef); namedInstances.append(CIMInstance(tempInst)); } if (pInstance) pInstance.Release(); hr = pInstEnum->Next(WBEM_INFINITE, 1, &pInstance, &dwReturned); } if (pInstEnum) pInstEnum.Release(); PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL4, "WMIInstanceProvider::enumerateInstances() - " "Instance count is %d", lCount)); if (lCount == 0) { PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL2, "WMIInstanceProvider::enumerateInstances() - " "hResult value is %x", hr)); } PEG_METHOD_EXIT(); return namedInstances; }