예제 #1
0
WMIProperty::WMIProperty(const BSTR & name, const VARIANT & value, const CIMTYPE type)
{
	CComBSTR bsName = name;
	CComVariant vValue = value;

	// build the property
	BSTR tmpBstr = (BSTR)bsName.Copy();
	String s(_bstr_t(tmpBstr, FALSE));
	SysFreeString(tmpBstr);
    *this = CIMProperty(CIMName(s), WMIValue(vValue, type));
    // old way -- memory leak?:
	// *this = CIMProperty(WMIString(bsName), WMIValue(vValue, type));

	vValue.Clear();
}
예제 #2
0
WMIProperty::WMIProperty(const BSTR & name, 
						 const VARIANT & value, 
						 const CIMTYPE type, 
						 IWbemQualifierSet * pQualifierSet,
						 Boolean includeQualifiers)
{
	CIMQualifierList qualifierList;
	CComBSTR bsName = name;
	CComVariant vValue = value;
	qualifierList = WMIQualifierSet(pQualifierSet);

	// get additional property information
	String referenceClass = String::EMPTY;

	// the WMI 'CIMTYPE' qualifier stores a string that contains the reference class name
	// cimtype_qualifier ::= ["ref:" + <reference_class_name>]
	if (CIM_REFERENCE == type)
	{
	// strip "ref:"
		Uint32 pos = qualifierList.find(CIMName("CIMTYPE"));

		if (PEG_NOT_FOUND != pos)
		{
			qualifierList.getQualifier(pos).getValue().get(referenceClass);

			//strip off "ref:" or, if not found, erase the whole string
			if ((pos = referenceClass.find(qString(Q_COLON))) != PEG_NOT_FOUND)
			{
				referenceClass.remove(0, pos + 1);
			}
		}
	}

	String classOrigin = String::EMPTY;

	// the 'Propagated" qualifier stores a string containing the class origin
	// propagated qualifier ::= [<class_origin>"."]<property_name>
	{
		Uint32 pos = qualifierList.find(CIMName("Propagated"));

		if (PEG_NOT_FOUND != pos)
		{
			qualifierList.getQualifier(pos).getValue().get(classOrigin);

			// strip on the ".<property_name> portion if there...
			if ((pos = classOrigin.find(".")) != PEG_NOT_FOUND)
			{
				classOrigin.remove(pos);
			}
		}
	}

	// build the property
	BSTR tmpBstr = (BSTR)bsName.Copy();
	String s(_bstr_t(tmpBstr, FALSE));
	SysFreeString(tmpBstr);
    // old way -- memory leak?:
	// String s = WMIString(bsName);
	CIMName cimRef;
	CIMName cimClsOrigin;

	if (0 != referenceClass.size())
	{
		cimRef = referenceClass;
	}

	if (0 != classOrigin.size())
	{
		cimClsOrigin = classOrigin;
	}

	*this = CIMProperty(CIMName(s), WMIValue(vValue, type), 0, cimRef,
				cimClsOrigin, (classOrigin.size() != 0));

	vValue.Clear();

	// add the qualifiers
	if (includeQualifiers)
	{
		Uint32 i, n;

		for (i = 0, n = qualifierList.getCount(); i < n; i++)
		{
			addQualifier(qualifierList.getQualifier(i));
		}
	}

}
예제 #3
0
/////////////////////////////////////////////////////////////////////////////
// WMIInstanceProvider::setProperty
//
// ///////////////////////////////////////////////////////////////////////////
void WMIInstanceProvider::setProperty(
        const String& nameSpace,
        const String& userName,
        const String& password,
        const CIMObjectPath& instanceName,
        const String& propertyName,
        const CIMValue& newValue)
{

    CComPtr<IWbemServices>            pServices;
    CComPtr<IWbemClassObject>        pInstance;
    CComVariant                        vValue;
    CComBSTR                        bsPropName;
    HRESULT                            hr;
    String                            sInstanceName;

    PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::setProperty()");

    sInstanceName = getObjectName(instanceName);

    setup(nameSpace, userName, password);

    PEG_TRACE((TRC_WMIPROVIDER,
                  Tracer::LEVEL3,
                  "setProperty() - setting property %s in %s",
                  propertyName,
                  sInstanceName));

    if (!m_bInitialized)
    {
        PEG_TRACE((TRC_WMIPROVIDER, Tracer::LEVEL1,
            "WMIInstanceProvider::setProperty - m_bInitilized= %x, "
            "throw CIM_ERR_FAILED exception",
            m_bInitialized));

        throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
            "Collector initialization failed.");
    }

    // retrieve instance object
    if (!(_collector->getObject(&pInstance, sInstanceName)))
    {
        if (pInstance)
            pInstance.Release();

        throw CIMException(CIM_ERR_NOT_FOUND);
    }
    //else if ((!(_collector->isInstance(pInstance))) || (prop == ""))
    else if ((!(_collector->isInstance(pInstance))) ||
        (propertyName.size() == 0))
    {
        if (pInstance)
            pInstance.Release();

        throw CIMException(CIM_ERR_INVALID_PARAMETER);
    }

    //check if property exists
    CIMInstance cimInstance;
    Array<CIMName> propertyNames;
    CIMName propName = propertyName;

    propertyNames.append(propName);
    CIMPropertyList propertyList = CIMPropertyList(propertyNames);

    cimInstance = getCIMInstance(nameSpace,
                                 userName,
                                 password,
                                 instanceName,
                                 propertyList);

    Uint32 pos = cimInstance.findProperty(propName);

    if (PEG_NOT_FOUND == pos)
    {
        throw CIMException(CIM_ERR_NO_SUCH_PROPERTY);
    }

    // check the existing value and type
    //bsPropName = prop.Bstr();
    bsPropName = propertyName.getCString();

    //convert property value from CIMValue to VARIANT
    WMIValue(newValue).getAsVariant(&vValue, nameSpace, userName, password);

    //update property value
    hr = pInstance->Put(bsPropName, 0, &vValue, 0);
    vValue.Clear();

    if (FAILED(hr))
    {
        if (pInstance)
            pInstance.Release();

        switch(hr)
        {
            case WBEM_E_TYPE_MISMATCH:
                throw CIMException(CIM_ERR_TYPE_MISMATCH);
            default:
                PEG_TRACE((TRC_WMIPROVIDER,
                              Tracer::LEVEL1,
                              "setProperty() - Put failed, hr = %x",
                              hr));

                throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
                    "WMI Put property failed.");
        }

    }

    //update instance
    _collector->Connect(&pServices);

    hr = pServices->PutInstance(pInstance,
                                WBEM_FLAG_UPDATE_ONLY,
                                NULL,
                                NULL);

    if (pInstance)
        pInstance.Release();

    if (pServices)
        pServices.Release();

    if (FAILED(hr))
    {
        PEG_TRACE((TRC_WMIPROVIDER,
                      Tracer::LEVEL1,
                      "setProperty() - PutInstance failed, hr = %x",
                      hr));

        throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
            "WMI Put instance failed.");
    }

    PEG_METHOD_EXIT();

    return;
}
예제 #4
0
/////////////////////////////////////////////////////////////////////////////
// WMIInstanceProvider::createInstance
//
// ///////////////////////////////////////////////////////////////////////////
CIMObjectPath WMIInstanceProvider::createInstance(
        const String& nameSpace,
        const String& userName,
        const String& password,
        const CIMInstance& newInstance)
{
    PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMIInstanceProvider::createInstance()");

    HRESULT hr;
    CComPtr<IWbemClassObject>    pClass;
    CComPtr<IWbemClassObject>    pNewInstance;
    CComBSTR bs;

    setup(nameSpace, userName, password);

    PEG_TRACE((TRC_WMIPROVIDER,
                  Tracer::LEVEL3,
                  "createInstance() - nameSpace %s, userName %s",
                  nameSpace.getCString(),
                  userName.getCString()));

    if (!m_bInitialized)
    {
        throw CIMException(CIM_ERR_FAILED);
    }

    // Get the class definition.
    String className = newInstance.getClassName().getString();

    if (!(_collector->getObject(&pClass, className)))
    {
        if (pClass)
            pClass.Release();

        throw CIMException(CIM_ERR_INVALID_CLASS);
    }
    else if (_collector->isInstance(pClass))
    {
        if (pClass)
            pClass.Release();

        throw CIMException(CIM_ERR_INVALID_PARAMETER);
    }

    // Create a new instance.
    hr = pClass->SpawnInstance(0, &pNewInstance);

    if (pClass)
        pClass.Release();

    if(FAILED(hr))
    {
        throw CIMException(CIM_ERR_FAILED);
    }

    // Set the properties
    for(Uint32 i = 0; i < newInstance.getPropertyCount(); i++)
    {
        CComVariant v;

        CIMProperty property = newInstance.getProperty(i).clone();
        CIMValue propertyValue = property.getValue();

        try
        {
            WMIValue(propertyValue).getAsVariant(
                                        &v,
                                        nameSpace,
                                        userName,
                                        password);
        }
        catch (CIMException&)
        {
            if (pNewInstance)
                pNewInstance.Release();

            v.Clear();

            throw;
        }

        bs.Empty();
        bs = property.getName().getString().getCString();

        // NULL properties in causes a CIM_ERR_FAILED
        // this conditional ignores properties with NULL.
        if (!propertyValue.isNull())
            hr = pNewInstance->Put(bs, 0, &v, 0);

        v.Clear();

        if(FAILED(hr))
        {
            if (pNewInstance)
                pNewInstance.Release();

            throw CIMException(CIM_ERR_FAILED);
        }
    }

    // Connect to the server
    CComPtr<IWbemServices>    pServices;
    bool bConnected = _collector->Connect(&pServices);

    if (!bConnected)
    {
        throw CIMException(CIM_ERR_ACCESS_DENIED);
    }

    // Write the instance to WMI.
    CComPtr<IWbemCallResult> pResult;

    hr = pServices->PutInstance(pNewInstance,
                                WBEM_FLAG_RETURN_IMMEDIATELY |
                                WBEM_FLAG_CREATE_ONLY |
                                WBEM_FLAG_USE_AMENDED_QUALIFIERS,
                                NULL,
                                &pResult);

    if (pNewInstance)
        pNewInstance.Release();

    if (pServices)
        pServices.Release();


    // set proxy security on pResult
    bool bSecurity = _collector->setProxySecurity(pResult);


    //check for error
    pResult->GetCallStatus(WBEM_INFINITE, &hr);
    if(FAILED(hr))
    {
        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_ALREADY_EXISTS:
                throw CIMException(CIM_ERR_ALREADY_EXISTS);
                break;
            case WBEM_E_INVALID_CLASS:
                throw CIMException(CIM_ERR_INVALID_CLASS);
                break;
            case WBEM_E_NOT_FOUND:
                throw CIMException(CIM_ERR_NOT_FOUND);
                break;
            default:
                throw CIMException(CIM_ERR_FAILED);
        }
    }


    // Mount the path to return

    // Prepend namespace to path
    String sPath = "//";

    // Append the host name if it is local
    if(_collector->isLocalNamespace())
    {
        sPath.append(getHostName());
        sPath.append("/");
    }

    // Append the namespace, colon
    sPath.append(nameSpace);
    sPath.append(":");

    // Get the key bindings
    CComBSTR bsKeyBindings;
    hr = pResult->GetResultString(WBEM_INFINITE, &bsKeyBindings);

    if (pResult)
        pResult.Release();

    if(FAILED(hr))
    {
        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_INVALID_OPERATION:
                // GetResultString() may fail on Win2K - get the keys
                // from the CIM instance:
                if (!GetKeyBindingsFromCIMInstance(newInstance,
                    &bsKeyBindings))
                    throw CIMException(CIM_ERR_NOT_SUPPORTED);
                break;
            default:
                throw CIMException(CIM_ERR_FAILED);
        }
    }

    // Append the key bindings to the path
    bs.Empty();
    bs = sPath.getCString();
    bs.Append(bsKeyBindings);
    bsKeyBindings.Empty();

    // Return the CIMObjectPath

    PEG_METHOD_EXIT();

    CMyString s; s = bs;
    return CIMObjectPath(String((LPCTSTR)s));
 }