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

#include "stdafx.h"
#include "PSLCmdParams.h"


CPSLCmdParams::CPSLCmdParams()
{
}

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

HRESULT CPSLCmdParams::FinalConstruct()
{
	PSL_BEGIN

	InternalUpdate();
	
	PSL_END
}

void CPSLCmdParams::FinalRelease()
{
}

void CPSLCmdParams::InternalUpdate()
{
	CCritSecLock cs(m_csCollection);

	m_coll.clear(); // Releases interfaces and clears the collection;
					// NOTE: .NET clients catch up interfaces, so takes
					// about 30 seconds before they are garbaged by .NET;

	vector<tstring> commands;
	LPTSTR sCmd = ::GetCommandLine();
	LPTSTR ParamsPtr = NULL;

	if(sCmd[0] == '"')
		ParamsPtr = _tcschr(sCmd + 1, '"');
	else
		ParamsPtr = _tcschr(sCmd, ' ');

	if(ParamsPtr)
		ParamsPtr ++; // Point at the next symbol after space or quote;
	else
		return;

	long L = (long)_tcslen(ParamsPtr);
	LPTSTR LineEnd = ParamsPtr + L;

	TCHAR * pStart = NULL; // Position where command started;
	bool bQuoted = false;
	TCHAR symbol = 0;
	TCHAR * pCommand = new TCHAR[L];
	while(ParamsPtr < LineEnd)
	{
		symbol = ParamsPtr[0];
		if(symbol == ' ')
		{
			if(pStart)
			{
				if(!bQuoted)
				{
					_tcsncpy_s(pCommand, L, pStart, ParamsPtr - pStart);
					commands.push_back(pCommand);
					pStart = NULL;
					bQuoted = false;
				}
			}
		}
		else
			if(symbol == '"')
			{
				if(pStart)
				{
					if(bQuoted) // end of the value;
					{
						if(ParamsPtr[1] == ' ' || ParamsPtr[1] == '\0')
						{
							_tcsncpy_s(pCommand, L, pStart, ParamsPtr - pStart + 1);
							commands.push_back(pCommand);
							pStart = NULL;	
						}
						bQuoted = false;
					}
					else
						bQuoted = true;
				}
				else
				{
					pStart = ParamsPtr;
					bQuoted = true;
				}
			} 
			else
			{
				if(!pStart)
				{
					pStart = ParamsPtr;
					bQuoted = false;
				}
			}

		ParamsPtr ++;
	}
	if(pStart)
	{
		_tcsncpy_s(pCommand, L, pStart, ParamsPtr - pStart);
		commands.push_back(pCommand);
	}
	delete []pCommand;

	CComObject<CPSLCmdParam> * pParam = NULL;
	for(vector<tstring>::iterator i = commands.begin();i != commands.end();i ++)
	{
		CComObject<CPSLCmdParam>::CreateInstance(&pParam);
		pParam->Initialize(i->c_str());
		m_coll.push_back(CComPtr<IPSLCmdParam>(pParam));
	}
}

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

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