⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmicollector.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	bool bConnected = Connect(&pServices);	if (!bConnected)	{		Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"WMICollector::getQueryResult,() - bConnected is false - throw Connect failed error");				throw PEGASUS_CIM_EXCEPTION(CIM_ERR_ACCESS_DENIED, "Connect failed.");	}	//if QueryLanguage is not WQL, throws an exception error informing	//that the query language is not supported	if (!String::equalNoCase("WQL", queryLanguage))	{		throw CIMException(CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED);	}	CComBSTR bsQuery = query.getCString();	CComBSTR bsQueryLanguage = queryLanguage.getCString();	// retrieve query result	hr = pServices->ExecQuery(		bsQueryLanguage,		bsQuery,        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_ENSURE_LOCATABLE,		NULL,		&p_inst);	Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"WMICollector::getQueryResult(), return from ExecQuery - hr value is %x", hr); 	if (SUCCEEDED(hr))		{		p_inst.CopyTo(ppInstances);				// set security attributes on *ppInstances		bool bSecurity = setProxySecurity(*ppInstances);	}	else		{		*ppInstances = NULL;        Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"WMICollector::getQueryResult() - hr value is %x", hr); 		switch(hr)		{			case WBEM_E_INVALID_QUERY: throw CIMException(CIM_ERR_INVALID_QUERY); break;			case WBEM_E_INVALID_QUERY_TYPE: throw CIMException(CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED); break;			default: throw CIMException(CIM_ERR_FAILED, "[getQueryResult] general"); 		}	}	PEG_METHOD_EXIT();	return (SUCCEEDED(hr));}/////////////////////////////////////////////////////////////////////////////// WMICollector::getObject - fetch a pointer to the object//		represented by sObjectName//		Can be either a class or an instance///////////////////////////////////////////////////////////////////////////////bool WMICollector::getObject(IWbemClassObject **ppObject, 								  const String & sObjectName){	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"WMICollector::getObject()");		HRESULT hr;	CComPtr<IWbemServices>	pServices;	CComPtr<IWbemClassObject> p_obj;	bool bConnected = Connect(&pServices);	if (!bConnected)	{		Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"WMICollector::getObject,() - bConnected is false - throw Connect failed error");		throw CIMException(CIM_ERR_ACCESS_DENIED);	}	CComBSTR bsObjectName = sObjectName.getCString();	LONG lFlags = WBEM_FLAG_USE_AMENDED_QUALIFIERS | WBEM_FLAG_RETURN_WBEM_COMPLETE; 	// retrieve class object	hr = pServices->GetObject(		bsObjectName, 		lFlags, 		NULL, 		&p_obj, 		NULL);	Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,		"WMICollector::getObject() - GetObject result is %x", hr);     if (SUCCEEDED(hr))	{		p_obj.CopyTo(ppObject);		// set security attributes on result		bool bSecurity = setProxySecurity(*ppObject);	}	else	{		*ppObject = NULL;		Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"WMICollector::getObject() - GetObject result is %x", hr); 		//generate error		switch(hr)		{			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, "[getObject] general"); 		}	}	PEG_METHOD_EXIT();		return (SUCCEEDED(hr));}/////////////////////////////////////////////////////////////////////////////// getProperties - retrieves selected //		properties for a CIMClass or//		CIMInstance object///////////////////////////////////////////////////////////////////////////////template<class CONTAINER>void getProperties(IWbemClassObject *pClass, 					Boolean localOnly,					Boolean includeQualifiers,					Boolean includeClassOrigin,					const CIMPropertyList& propertyList,								  					CONTAINER & container){	HRESULT hr = S_OK;	String sMessage;	CComBSTR	bsName;			// of the property	CComVariant	vValue;			// of the property	long		lFlavor;		// of the property	CIMTYPE		type;	CIMProperty property;	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"getProperties()");	Uint32 size = propertyList.size();	for (Uint32 i = 0; ((i < size) && SUCCEEDED(hr)); i++)	{		String sPropName = propertyList[i].getString();		bsName.Empty();		vValue.Clear();		sMessage = "Get()";		bsName = sPropName.getCString();		// fetch the property		hr = pClass->Get(			bsName, 			0, 			&vValue, 			&type, 			&lFlavor);		// process each property		if (SUCCEEDED(hr))		{			Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,				"getProperties() - CIMTYPE[%x] - WMITYPE[%x]",				type, 				vValue.vt);			bool bPropagated = (lFlavor & WBEM_FLAVOR_ORIGIN_PROPAGATED) ? true : false;			try			{				property = WMICollector::getProperty(pClass, bsName, vValue, 										type, includeClassOrigin, 										includeQualifiers, bPropagated);			}			catch( TypeMismatchException & e )			{				// ATTN:				// unsupported for now - do some tracing...				String sClass = WMICollector::getClassName(pClass);				Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,					"getProperties() - Ignoring invalid type for %s in %s.  %s, unsupported WMI CIM type is %x",					sPropName, sClass, e.getMessage(), type);				continue;			}			vValue.Clear();						if (bPropagated && localOnly)			{				Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,					"getProperties() - Property %s was defined in a superclass", 					(LPCTSTR)sPropName.getCString());			}			else			// try to add it			{				try				{					container.addProperty(property);				}				catch( AlreadyExistsException& e )				{					// ignore this					Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,						"getProperties() - Property %s is already defined. %s", sPropName, e.getMessage());				}				catch( Exception & e )				{					// ignore this					Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,						"getAllProperties() - Ignoring AddedReferenceToClass.  %s", e.getMessage());				}				catch(... ) 				{					throw CIMException(CIM_ERR_FAILED, "[getProperties] general");				}			}		}		else if (WBEM_E_NOT_FOUND == hr)		{	// we are supposed to keep going...			Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,				"getProperties() - %s property not found", (LPCTSTR)sPropName.getCString());			//reset			throw CIMException(CIM_ERR_FAILED, "[getProperties] Property Not Found");						hr = S_OK;		}	}	if (FAILED(hr))	{		Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"getProperties() - failed, return result is %x", hr);        throw CIMException(CIM_ERR_FAILED, "[getProperties] general 2");	}	PEG_METHOD_EXIT();}/////////////////////////////////////////////////////////////////////////////// getAllProperties - retrieves all the //		properties for a CIMClass or//		CIMInstance object///////////////////////////////////////////////////////////////////////////////template<class CONTAINER>bool getAllProperties(IWbemClassObject *pClass, 									  long lFlags,									  Boolean includeQualifiers,									  Boolean includeClassOrigin,									  CONTAINER & container){	HRESULT hr;	String sMessage;	CComBSTR	bsName;			// of the property	CComVariant	vValue;			// of the property	long		lFlavor;		// of the property	CIMTYPE		type;	CIMProperty property;	bool bFound = false;	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"getAllProperties()");	hr = pClass->BeginEnumeration(lFlags);	sMessage = "BeginEnumeration()";	if (SUCCEEDED(hr))	{		bsName.Empty();		vValue.Clear();		sMessage = "Next()";		hr = pClass->Next(0, &bsName, &vValue, &type, &lFlavor);	}	// process each property	while (SUCCEEDED(hr))	{		if (WBEM_S_NO_MORE_DATA == hr)		{			break;		}		Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,				"getAllProperties() - CIMTYPE[%x] - WMITYPE[%x]",				type, 				vValue.vt);		bFound = true;		CMyString sPropName; sPropName = bsName;				bool bPropagated = (lFlavor & WBEM_FLAVOR_ORIGIN_PROPAGATED) ? true : false;		try		{			property = WMICollector::getProperty(pClass, bsName, vValue, 									type, includeClassOrigin, 									includeQualifiers, bPropagated);		}		catch( TypeMismatchException & e )		{			// ATTN:			// unsupported for now - do some tracing...			String sClass = WMICollector::getClassName(pClass);			Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,				"getAllProperties() - Ignoring invalid type for %s in %s.  %s, unsupported WMI CIM type is %x",				(LPCTSTR)sPropName, sClass, e.getMessage(), type);			bsName.Empty();			vValue.Clear();			sMessage = "Next()";			hr = pClass->Next(0, &bsName, &vValue, &type, &lFlavor);			continue;		}							try		{			container.addProperty(property);		}		catch( AlreadyExistsException& e )		{			// ignore this			Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,				"getAllProperties() - Property %s is already defined", (LPCTSTR)sPropName);			Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3, "getAllProperties() - %s", e.getMessage());		}		catch( Exception & e )		{			// ignore this			Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,				"getAllProperties() - Ignoring AddedReferenceToClass.  %s", e.getMessage());		}		catch(... ) 		{			throw CIMException(CIM_ERR_FAILED, "[getAllProperties] general 1");		}		bsName.Empty();		vValue.Clear();		sMessage = "Next()";		hr = pClass->Next(0, &bsName, &vValue, &type, &lFlavor);	}	pClass->EndEnumeration();	vValue.Clear();	if (FAILED(hr))	{		Tracer::trace(TRC_WMIPROVIDER, Tracer::LEVEL3,			"getAllProperties() - %s result is %x", sMessage.getCString(), hr);		throw CIMException(CIM_ERR_FAILED, "[getAllProperties] general 2");	}	PEG_METHOD_EXIT();	return bFound;}/////////////////////////////////////////////////////////////////////////////// getAllMethods - retrieves all the //		methods for a CIMClass or//		CIMInstance object///////////////////////////////////////////////////////////////////////////////bool getAllMethods(IWbemClassObject *pClass, 				   long lFlags,				   Boolean includeQualifiers,				   Boolean includeClassOrigin,				   CIMClass & container){	HRESULT hr;	String sMessage;	CComBSTR					bsName;			// of the method	CComPtr<IWbemClassObject>	inParameters;	// of the method	CComPtr<IWbemClassObject>	outParameters;  // of the method	CIMMethod					method;	bool bFound = false;	PEG_METHOD_ENTER(TRC_WMIPROVIDER,"getAllMethods()");	hr = pClass->BeginMethodEnumeration(lFlags);	sMessage = "BeginMethodEnumeration()";	if (WBEM_E_INVALID_PARAMETER == hr)    {        // Windows 2000 does not accept any flags for BeginMethodEnumeration()        // (e.g., WBEM_FLAG_LOCAL_ONLY, which is used when localOnly==true)        // so try again with no flags (assuming Windows 2000 here):        hr = pClass->BeginMethodEnumeration(0L);        }        if (SUCCEEDED(hr))	{		bsName.Empty();		sMessage = "NextMethod()";		hr = pClass->NextMethod(0, &bsName, &inParameters, &outParameters);	}	// process each method	while (SUCCEEDED(hr))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -