///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::deleteClass // // /////////////////////////////////////////////////////////////////////////// void WMIClassProvider::deleteClass(const String& nameSpace, const String& userName, const String& password, const String& className) { HRESULT hr; PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIClassProvider::deleteClass()"); CComPtr<IWbemServices> pServices; //Connect to namespace setup(nameSpace,userName,password); bool bConnected = _collector->Connect(&pServices); if (!bConnected) { if (pServices) pServices.Release(); throw CIMException(CIM_ERR_ACCESS_DENIED); } //Convert the parameters to make the WMI call CComBSTR bsClassName = className.getCString(); LONG lFlags = 0L; //Perform the WMI operation hr = pServices->DeleteClass(bsClassName, lFlags, NULL, NULL); if (pServices) pServices.Release(); //Handle the WMI operation result if (FAILED(hr)) { CMyString msg; msg.Format("Failed to delete class [%s]. Error: 0x%X", 255, className.getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::deleteClass() - %s", (LPCTSTR)msg); switch (hr) { case WBEM_E_ACCESS_DENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break; case WBEM_E_FAILED: throw CIMException(CIM_ERR_FAILED); break; case WBEM_E_INVALID_PARAMETER: throw CIMException(CIM_ERR_FAILED, "WMI Invalid Parameter"); break; case WBEM_E_INVALID_CLASS: throw CIMException(CIM_ERR_NOT_FOUND); break; case WBEM_E_NOT_FOUND: throw CIMException(CIM_ERR_NOT_FOUND); break; case WBEM_E_CLASS_HAS_CHILDREN: throw CIMException(CIM_ERR_CLASS_HAS_CHILDREN); break; default: throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } } PEG_METHOD_EXIT(); }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createQualifier creates a qualifiers // ///////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createQualifier (const WMIQualifier &qualifier, IWbemQualifierSet *pQual) { HRESULT hr; PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createQualifier()"); String sName = qualifier.getName().getString(); CComBSTR bs = sName.getCString(); WMIValue value(qualifier.getValue()); WMIFlavor flavor(qualifier.getFlavor()); CComVariant v; value.getAsVariant(&v); // key is created using a special call to wmi //if (!stricmp("key", )) if (String::equalNoCase("key", sName)) { hr = pQual->Put(bs, &v, NULL); } else { hr = pQual->Put(bs, &v, flavor.getAsWMIValue()); } v.Clear(); bs.Empty(); if (FAILED(hr)) { CMyString msg; msg.Format("It is not possible to add the qualifier [%s] to " "the object! Error: 0x%X", 255, sName.getCString(), hr); PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL1, "WmiClassProvider::createQualifier() - %s", (LPCTSTR)msg)); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } PEG_METHOD_EXIT(); return; }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createProperty creates one property // doesn't create the qualifiers ///////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createProperty(const CIMProperty &keyProp, IWbemClassObject *pNewClass) { PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createProperty()"); // create the property //Get the CIMTYPE of the parameter CIMTYPE type = CIMTypeToWMIType(keyProp.getType()); //If the property is an array, add CIM_FLAG_ARRAY to CIMType if (keyProp.isArray()) { type |= CIM_FLAG_ARRAY; } // add the property to the class CComBSTR bs = keyProp.getName().getString().getCString(); HRESULT hr = pNewClass->Put(bs, NULL, NULL, type); bs.Empty(); //handle the error, if failed if (FAILED(hr)) { CMyString msg; msg.Format("Failed to add property [%s]. Error: 0x%X", 255, keyProp.getName().getString().getCString(), hr); PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL1, "WMIClassProvider::createProperty () - %s", (LPCTSTR)msg)); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } PEG_METHOD_EXIT(); return; }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createProperties creates all properties including keys // add the qualifiers too // /////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createProperties(const CIMClass& newClass, IWbemServices *pServices, IWbemClassObject *pNewClass) { HRESULT hr; PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createProperties()"); // create the properties but don't create the keys again CIMProperty prop; for (Uint32 i = 0; i < newClass.getPropertyCount(); i++) { prop = newClass.getProperty(i).clone(); // create the properties try { createProperty(prop, pNewClass); } catch (CIMException&) { throw; } // get a pointer to work with qualifiers CComPtr<IWbemQualifierSet> pQual; CComBSTR bs = prop.getName().getString().getCString(); hr = pNewClass->GetPropertyQualifierSet(bs, &pQual); bs.Empty(); if (FAILED(hr)) { CMyString msg; msg.Format("Failed get Qualifier set of [%s]. Error: 0x%X", 255, prop.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createProperties() - %s", (LPCTSTR)msg); if (pQual) pQual.Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // set the qualifiers to the property for (Uint32 j = 0; j < prop.getQualifierCount(); j++) { WMIQualifier qualifier(prop.getQualifier(j)); try { createQualifier(qualifier, pQual); } catch (CIMException&) { if (pQual) pQual.Release(); throw; } } // set the CLASSORIGIN qualifier if it wasn't set yet String strClassorigin = prop.getClassOrigin().getString(); if (strClassorigin.size() == 0) { strClassorigin = newClass.getClassName().getString(); } WMIFlavor flavor(CIMFlavor::DEFAULTS); /* v.vt = VT_BSTR; v.bstrVal = strClassorigin.getCString(); */ CComVariant v; v = strClassorigin.getCString(); hr = pQual->Put(L"CLASSORIGIN", &v, flavor.getAsWMIValue()); v.Clear(); if (pQual) pQual.Release(); if (FAILED(hr)) { CMyString msg; msg.Format("Failed to add CLASSORIGIN qualifier in [%s]. Error: 0x%X", 255, prop.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createProperties () - %s", (LPCTSTR)msg); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } } PEG_METHOD_EXIT(); return; }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createClassNameAndClassQualifiers // // /////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createClassNameAndClassQualifiers(const CIMClass& newClass, IWbemServices *pServices, IWbemClassObject **pNewClass, const bool hasSuperClass) { HRESULT hr; PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createClassNameAndClassQualifiers()"); // if the class has a superclass, we need to spwan a derived if (hasSuperClass) { // get the superclass name CComPtr<IWbemClassObject> pSuperClass; String tmp = newClass.getSuperClassName().getString(); CComBSTR bs = tmp.getCString(); hr = pServices->GetObject( bs, NULL, NULL, &pSuperClass, NULL); bs.Empty(); if (FAILED(hr)) { if (pSuperClass) pSuperClass.Release(); CMyString msg; msg.Format("Failed to get a pointer to Superclass [%s]. Error: 0x%X", 255, tmp.getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createClassNameAndClassQualifiers() - %s", (LPCTSTR)msg); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } //Creates the new class pSuperClass->SpawnDerivedClass(NULL, pNewClass); if (pSuperClass) pSuperClass.Release(); } else { // we are creating a base class hr = pServices->GetObject(NULL, NULL, NULL, pNewClass, NULL); if (FAILED(hr)) { CMyString msg; msg.Format("Failed to get a pointer to a new class. Error: 0x%X", hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createClassNameAndClassQualifiers() - %s", (LPCTSTR)msg); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } } // create the class name CComVariant v; v = newClass.getClassName().getString().getCString(); hr = (*pNewClass)->Put(L"__CLASS", 0, &v, 0); v.Clear(); if (FAILED(hr)) { CMyString msg; msg.Format("Failed to add class name on class [%s]. Error: 0x%X", 255, newClass.getClassName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createClassNameAndClassQualifiers() - %s", (LPCTSTR)msg); if (*pNewClass) (*pNewClass)->Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // get a pointer to work with qualifiers CComPtr<IWbemQualifierSet> pNewClassQualifier; hr = (*pNewClass)->GetQualifierSet(&pNewClassQualifier); if (FAILED(hr)) { CMyString msg; msg.Format("Failed to get the Qualifier set pointer of class [%s]. Error: 0x%X", 255, newClass.getClassName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createClassNameAndClassQualifiers() - %s", (LPCTSTR)msg); if (*pNewClass) (*pNewClass)->Release(); if (pNewClassQualifier) pNewClassQualifier.Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // check the class qualifiers and create them if they are valid // we are taking care of the class qualifiers and not methods/properties qualifiers :D for (Uint32 i = 0; i < newClass.getQualifierCount(); i++) { try { WMIQualifier qualifier(newClass.getQualifier(i).clone()); createQualifier(qualifier, pNewClassQualifier); } catch (CIMException&) { if (*pNewClass) (*pNewClass)->Release(); if (pNewClassQualifier) pNewClassQualifier.Release(); throw; } } if (pNewClassQualifier) pNewClassQualifier.Release(); PEG_METHOD_EXIT(); return; }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createClass // // /////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createClass(const String& nameSpace, const String& userName, const String& password, const CIMClass& newClass, Boolean updateClass) { PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createClass()"); setup(nameSpace, userName, password); if (!m_bInitialized) { Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createClass - m_bInitilized= %x, throw CIM_ERR_FAILED exception", m_bInitialized); throw CIMException(CIM_ERR_FAILED); } // Check if the class does not exist and if if has a valid // superclass performInitialCheck(newClass, updateClass); bool hasSuperClass = (newClass.getSuperClassName().getString() != String::EMPTY); // gets the pointers CComPtr<IWbemServices> pServices; CComPtr<IWbemClassObject> pNewClass; if (!_collector->Connect(&pServices)) { if (pServices) pServices.Release(); throw CIMException (CIM_ERR_FAILED); } try { // starts the class creation by name and class qualifiers createClassNameAndClassQualifiers( newClass, pServices, &pNewClass, hasSuperClass); // create properties createProperties( newClass, pServices, pNewClass); // create methods createMethods( newClass, pServices, pNewClass); } catch (CIMException&) { if (pServices) pServices.Release(); if (pNewClass) pNewClass.Release(); throw; } // Store the new class into WMI LONG lFlags = 0L; //if updateClass is set, we are trying a modifyclass if (updateClass) lFlags = WBEM_FLAG_UPDATE_ONLY; HRESULT hr = pServices->PutClass(pNewClass, lFlags, NULL, NULL); if (pServices) pServices.Release(); if (pNewClass) pNewClass.Release(); if (FAILED(hr)) { CMyString msg; msg.Format("It is not possible to create the class [%s]. Error: 0x%X", 255, newClass.getClassName().getString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createClass() - %s", (LPCTSTR)msg); switch(hr) { case E_ACCESSDENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break; case WBEM_E_ACCESS_DENIED: throw CIMException(CIM_ERR_ACCESS_DENIED); break; case WBEM_E_CLASS_HAS_CHILDREN: throw CIMException(CIM_ERR_CLASS_HAS_CHILDREN); break; case WBEM_E_CLASS_HAS_INSTANCES: throw CIMException(CIM_ERR_CLASS_HAS_INSTANCES); break; case WBEM_E_NOT_FOUND: throw CIMException(CIM_ERR_NOT_FOUND); break; case WBEM_E_INVALID_CLASS: throw CIMException(CIM_ERR_INVALID_PARAMETER); break; default: throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } } PEG_METHOD_EXIT(); return; }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createParam create a parameter // ///////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createParam(const CIMConstParameter ¶m, IWbemClassObject *pNewClass) { PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createParam()"); //Get the CIMTYPE of the parameter CIMTYPE type = CIMTypeToWMIType(param.getType()); //If the property is an array, add CIM_FLAG_ARRAY to CIMType if (param.isArray()) { type |= CIM_FLAG_ARRAY; } // add the property to the class CComBSTR bs = param.getName().getString().getCString(); HRESULT hr = pNewClass->Put(bs, NULL, NULL, type); //handle the error, if failed if (FAILED(hr)) { CMyString msg; msg.Format("It is not possible to add parameter [%s] to the parameters list! Error: 0x%X", 255, param.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createParam() - %s", (LPCTSTR)msg); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // create the qualifiers for this parameter // get a pointer to work with qualifiers CComPtr<IWbemQualifierSet> pQual; hr = pNewClass->GetPropertyQualifierSet(bs, &pQual); if (FAILED(hr)) { CMyString msg; msg.Format("It is not possible to get a qualifier set for parameter [%s]! Error: 0x%X", 255, param.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createParam() - %s", (LPCTSTR)msg); if (pQual) pQual.Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // create the qualifiers for this parameter for (Uint32 i = 0; i < param.getQualifierCount(); i++) { CIMQualifier cimQual = param.getQualifier(i).clone(); WMIQualifier qualifier(cimQual); try { createQualifier(qualifier, pQual); } catch (CIMException&) { if (pQual) pQual.Release(); throw; } } if (pQual) pQual.Release(); PEG_METHOD_EXIT(); return; }
///////////////////////////////////////////////////////////////////////////// // WMIClassProvider::createMethod create a method // ///////////////////////////////////////////////////////////////////////////// void WMIClassProvider::createMethod (CIMConstMethod &method, IWbemServices *pServices, IWbemClassObject *pNewClass) { PEG_METHOD_ENTER(TRC_WMIPROVIDER, "WmiClassProvider::createMethod()"); // the parameters HRESULT hr = S_OK; CComPtr<IWbemClassObject> pinParameters; CComPtr<IWbemClassObject> poutParameters; // Get pointers to use for in & out params: hr = pServices->GetObject(L"__PARAMETERS", NULL, NULL, &pinParameters, NULL); if (FAILED(hr)) { CMyString msg; msg.Format("Failed to get a paramter pointer while creating method: %s! Error: 0x%X", 255, method.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createMethod() - %s", (LPCTSTR)msg); if (pinParameters) pinParameters.Release(); if (poutParameters) poutParameters.Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // Get pointers to use for in & out params: hr = pServices->GetObject(L"__PARAMETERS", NULL, NULL, &poutParameters, NULL); if (FAILED(hr)) { CMyString msg; msg.Format("Failed to get a paramter pointer while creating method: %s! Error: 0x%X", 255, method.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createMethod() - %s", (LPCTSTR)msg); if (pinParameters) pinParameters.Release(); if (poutParameters) poutParameters.Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // create the parameters for (Uint32 i = 0; i < method.getParameterCount(); i++) { CIMConstParameter param; param = method.getParameter(i); // Is this an in or out parameter, or both? if (param.findQualifier(CIMName("in")) != -1) { try { createParam(param, pinParameters); } catch (CIMException&) { if (pinParameters) pinParameters.Release(); if (poutParameters) poutParameters.Release(); throw; } } if (param.findQualifier(CIMName("out")) != -1) { try { createParam(param, poutParameters); } catch (CIMException&) { if (pinParameters) pinParameters.Release(); if (poutParameters) poutParameters.Release(); throw; } } } // create the method CComBSTR bs = method.getName().getString().getCString(); hr = pNewClass->PutMethod(bs, NULL, pinParameters, poutParameters); if (pinParameters) pinParameters.Release(); if (poutParameters) poutParameters.Release(); if (FAILED(hr)) { CMyString msg; msg.Format("It is not possible to add the method [%s] ! Error: 0x%X", 255, method.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createMethod() - %s", (LPCTSTR)msg); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // create the qualifiers for the method // get a pointer to work with qualifiers CComPtr<IWbemQualifierSet> pQual; hr = pNewClass->GetMethodQualifierSet(bs, &pQual); if (FAILED(hr)) { CMyString msg; msg.Format("It is not possible to get the qualifier set of the method [%s] ! Error: 0x%X", 255, method.getName().getString().getCString(), hr); Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIClassProvider::createMethod() - %s", (LPCTSTR)msg); if (pQual) pQual.Release(); throw CIMException(CIM_ERR_FAILED, (LPCTSTR)msg); } // create the qualifiers for the method for (Uint32 i = 0; i < method.getQualifierCount(); i++) { CIMQualifier cimQual = method.getQualifier(i).clone(); WMIQualifier qualifier(cimQual); try { createQualifier(qualifier, pQual); } catch (CIMException&) { if (pQual) pQual.Release(); throw; } } if (pQual) pQual.Release(); PEG_METHOD_EXIT(); return; }
////////////////////////////////////////////////////////////////////////////// // WMIBaseProvider::getQueryString - builds the query string from the // input parameters for Associator and Reference commands // // /////////////////////////////////////////////////////////////////////////// String WMIBaseProvider::getQueryString(const CIMObjectPath &objectName, const String &sQueryCommand, const String &assocClass, const String &resultClass, const String &role, const String &resultRole) { bool hasWHERE = false; bool isInst; //first we need to get the object name String sObjName = getObjectName(objectName); // check if is an instance name Uint32 pos = sObjName.find(qString(Q_PERIOD)); isInst = (PEG_NOT_FOUND != pos); CMyString sQuery; sQuery.Format(CMyString(sQueryCommand), 128, CMyString(sObjName)); //set up any optional parameters if (!((0 == assocClass.size()) && (0 == resultClass.size()) && (0 == role.size()) && (0 == resultRole.size()))) { // we have optional parameters, append the appropriate ones sQuery += qChar(Q_WHERE); hasWHERE = true; if (0 != assocClass.size()) { sQuery += qChar(Q_ASSOC_CLS); sQuery += assocClass; } if (0 != resultClass.size()) { sQuery += qChar(Q_RESULT_CLASS); sQuery += resultClass; } if (0 != role.size()) { sQuery += qChar(Q_ROLE); sQuery += role; } if (0 != resultRole.size()) { sQuery += qChar(Q_RESULT_ROLE); sQuery += resultRole; } } // check if an instance if (!isInst) { // have a class, add "SchemaOnly" if (!hasWHERE) { sQuery += qChar(Q_WHERE); } sQuery += qChar(Q_SCHEMA); } Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "WMIBaseProvider::getQueryString() - Query is %s", (LPCTSTR)sQuery); String s = (LPCTSTR)sQuery; return s; }