Click here to Skip to main content
15,881,715 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 127.9K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLEnvironmentVars.cpp : Implementation of CPSLEnvironmentVars

#include "stdafx.h"
#include "PSLEnvironmentVar.h"
#include "PSLEnvironmentVars.h"

CPSLEnvironmentVars::CPSLEnvironmentVars()
{
}

HRESULT CPSLEnvironmentVars::OnIndexOutOfRange()
{
	return MakeException(exIndexOutOfRange);
}

HRESULT CPSLEnvironmentVars::FinalConstruct()
{
	PSL_BEGIN

	InternalUpdate();
	
	PSL_END
}

void CPSLEnvironmentVars::FinalRelease()
{
}

bool CPSLEnvironmentVars::ParseVariable(LPCTSTR sVariable, tstring & sName, tstring & sValue)
{
	LPCTSTR pValue = _tcschr(sVariable, '=');
	if(!pValue || pValue == sVariable)
		return false;

	__int3264 Length = pValue - sVariable;
	sName.resize(Length + 1);
	::_tcsncpy_s((LPTSTR)sName.c_str(), Length + 1, sVariable, Length);
	pValue ++;
	sValue = pValue;
	::trim(sName);
	::trim(sValue);

	return sName.length() > 0 && sValue.length() > 0;
}

void CPSLEnvironmentVars::InternalUpdate()
{
	CCritSecLock cs(m_csCollection);
	m_coll.clear();
	LPTCH pBuffer = ::GetEnvironmentStrings();
	if(!pBuffer)
		return;
	CComObject<CPSLEnvironmentVar> * pVariable = NULL;
	tstring sName, sValue;
	LPCTSTR ptr = pBuffer;
	while(ptr[0])
	{
		if(ParseVariable(ptr, sName, sValue))
		{
			CComObject<CPSLEnvironmentVar>::CreateInstance(&pVariable);
			pVariable->Initialize(sName.c_str(), sValue.c_str());
			m_coll.push_back(CComPtr<IPSLEnvironmentVar>(pVariable));
		}
		ptr += ::_tcslen(ptr) + 1;
	}
	::FreeEnvironmentStrings(pBuffer);
}

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

STDMETHODIMP CPSLEnvironmentVars::Update()
{
	PSL_BEGIN

	InternalUpdate();

	PSL_END
}

STDMETHODIMP CPSLEnvironmentVars::Find(BSTR Name, IPSLEnvironmentVar ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;
	_bstr_t sVarName(Name);
	CCritSecLock cs(m_csCollection);
	for(EnvironmentVarsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
	{
		BSTR sName = NULL;
		if(i->m_T->get_Name(&sName) == S_OK && !::_tcsicmp(_bstr_t(sName), sVarName))
		{
			IPSLEnvironmentVar * pVar = i->m_T;
			pVar->AddRef();
			*ppValue = pVar;
			break;
		}
	}

	PSL_END
}

STDMETHODIMP CPSLEnvironmentVars::Add(BSTR Name, BSTR Value, IPSLEnvironmentVar ** ppValue)
{
	PSL_BEGIN

	IPSLEnvironmentVar * pVar = NULL;
	Find(Name, &pVar);
	if(pVar)
		pVar->put_Value(Value);
	else
	{
		_bstr_t sName(Name), sValue(Value);
		if(::SetEnvironmentVariable(sName, sValue))
		{
			CComObject<CPSLEnvironmentVar> * pVariable = NULL;
			CComObject<CPSLEnvironmentVar>::CreateInstance(&pVariable);
			pVariable->AddRef();
			pVariable->Initialize(_bstr_t(Name), _bstr_t(Value));
			pVar = pVariable;
			CCritSecLock cs(m_csCollection);
			m_coll.push_back(CComPtr<IPSLEnvironmentVar>(pVariable));
		}
	}
	*ppValue = pVar;

	PSL_END
}

STDMETHODIMP CPSLEnvironmentVars::Remove(BSTR Name, VARIANT_BOOL * pValue)
{
	PSL_BEGIN

	_bstr_t sName(Name);
	*pValue = ::SetEnvironmentVariable(sName, NULL)?VARIANT_TRUE:VARIANT_FALSE;
	CCritSecLock cs(m_csCollection);
	for(EnvironmentVarsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
	{
		BSTR s = NULL;
		if(i->m_T->get_Name(&s) == S_OK && !::_tcsicmp(_bstr_t(s), sName))
		{
			m_coll.erase(i);
			break;
		}	
	}

	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