Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / VBScript

ProSysLib: Dissecting the Process

Rate me:
Please Sign up or sign in to vote.
4.84/5 (69 votes)
22 Nov 2010CPOL12 min read 129.2K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLTable.cpp : Implementation of CPSLTable

#include "stdafx.h"
#include "PSLTable.h"

CPSLTable::CPSLTable()
{
	m_nCols = 0;
	m_nRows = 0;
}

HRESULT CPSLTable::FinalConstruct()
{
	return S_OK;
}

void CPSLTable::FinalRelease()
{
}

bool CPSLTable::Initialize(IWbemClassObject * pClassObject, long & lErrorCode, vector<tstring> * pColFilter, bool bSpecial)
{
	m_nCols = 0;
	SAFEARRAY * pArray = NULL;
	long lFlags = WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY;
	if(bSpecial)
		lFlags = WBEM_FLAG_ALWAYS;
	lErrorCode = pClassObject->GetNames(NULL, lFlags, NULL, &pArray);
	bool bResult = false;
	vector<tstring> names;
	if(lErrorCode == WBEM_S_NO_ERROR)
	{
		long nElements;	// Number of elements in the array;
		lErrorCode = ::SafeArrayGetUBound(pArray, 1, &nElements);
		if(lErrorCode == S_OK)
		{
			bResult = true;
			long nCols = nElements + 1;
			BSTR sName = NULL;
			long Idx = 0;
			for(long i = 0;i < nCols;i ++)
			{
				lErrorCode = ::SafeArrayGetElement(pArray, &i, &sName);
				if(lErrorCode == S_OK)
				{
					if(pColFilter)
						names.push_back(sName);
					else
					{
						m_ColumnNames.insert(pair<long, _bstr_t>(Idx ++, sName));
						m_nCols ++;
					}

					::SysFreeString(sName);
					bResult = true;
				}
				else
				{
					bResult = false;
					break;
				}
			}
		}
		::SafeArrayDestroy(pArray);
	}

	if(bResult && pColFilter)
	{
		long Idx = 0;
		for(vector<tstring>::iterator i = pColFilter->begin();i != pColFilter->end(); i++)
			for(vector<tstring>::iterator k = names.begin();k != names.end(); k++)
				if(!_tcsicmp(i->c_str(), k->c_str()))
				{
					m_ColumnNames.insert(pair<long, _bstr_t>(Idx ++, k->c_str()));
					m_nCols ++;
					break;
				}
	}

	return bResult;
}

bool CPSLTable::AddRow(IWbemClassObject * pClassObject, long & lErrorCode)
{
	long Idx = 0;
	for(CColumnNames::const_iterator i = m_ColumnNames.begin();i != m_ColumnNames.end();i ++)
	{
		_variant_t v;
		lErrorCode = pClassObject->Get(i->second, 0, &v, 0, 0);
		if(lErrorCode != S_OK)
			return false;

		m_ColumnValues.insert(pair<long, _variant_t>(m_nRows * m_nCols + Idx, v));
		Idx ++;
	}
	m_nRows ++;
	return true;
}

////////////////////////////////////////////////////////////////////////
// Interface Implementation;
////////////////////////////////////////////////////////////////////////

STDMETHODIMP CPSLTable::get_nCols(long * pValue)
{
	PSL_BEGIN

	*pValue = m_nCols;

	PSL_END
}

STDMETHODIMP CPSLTable::get_nRows(long * pValue)
{
	PSL_BEGIN

	*pValue = m_nRows;

	PSL_END
}

STDMETHODIMP CPSLTable::GetValue(long RowIdx, long ColIdx, VARIANT * pValue)
{
	PSL_BEGIN

	if(RowIdx > m_nRows || ColIdx > m_nCols)
		SetException(exIndexOutOfRange);
	else
	{
		CColumnValues::const_iterator i = m_ColumnValues.find(RowIdx * m_nCols + ColIdx);
		if(i == m_ColumnValues.end())
			SetException(exGeneric);
		else
			::VariantCopy(pValue, &i->second);
	}

	PSL_END
}

STDMETHODIMP CPSLTable::GetColName(long ColIdx, BSTR * pValue)
{
	PSL_BEGIN

	if(ColIdx > m_nCols)
		SetException(exIndexOutOfRange);
	else
	{
		CColumnNames::const_iterator i = m_ColumnNames.find(ColIdx);
		if(i == m_ColumnNames.end())
			SetException(exGeneric);
		else
			*pValue = i->second.copy();
	}

	PSL_END
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sibedge IT
Ireland Ireland
My online CV: cv.vitalytomilov.com

Comments and Discussions