Click here to Skip to main content
15,885,244 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 128.1K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLProcessIO.cpp : Implementation of CPSLProcessIO

#include "stdafx.h"
#include "PSLProcessIO.h"

CPSLProcessIO::CPSLProcessIO()
{
	m_ProcessID = NULL;
	m_CurrentProcessID = ::GetCurrentProcessId();
}

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

void CPSLProcessIO::FinalRelease()
{
}

void CPSLProcessIO::Initialize(long ProcessID)
{
	m_ProcessID = ProcessID;
}

bool CPSLProcessIO::GetProcessIO(IO_COUNTERS * pCounters)
{
	bool bProcessOpen = false;
	HANDLE hProcess = NULL;
	if(m_ProcessID == m_CurrentProcessID)
		hProcess = ::GetCurrentProcess();
	else
	{
		hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, m_ProcessID);
		if(hProcess)
			bProcessOpen = true;
		else
		{
			if(::GetLastError() == ERROR_ACCESS_DENIED)
				SetException(exAccessDenied);
			return false;
		}
	}

	bool bResult = false;

	::memset(pCounters, 0, sizeof(IO_COUNTERS));
	if(::GetProcessIoCounters(hProcess, pCounters))
		bResult = true;

	if(bProcessOpen)
		::CloseHandle(hProcess);

	return bResult;
}


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


STDMETHODIMP CPSLProcessIO::get_ReadOperationCount(VARIANT * pValue)
{
	PSL_BEGIN

	IO_COUNTERS ProcessIO;
	if(GetProcessIO(&ProcessIO))
		CPSLUtilities::SetVariant64Bit(pValue, ProcessIO.ReadOperationCount);
	else
		CPSLUtilities::SetVariant64Bit(pValue, 0);

	PSL_END
}

STDMETHODIMP CPSLProcessIO::get_WriteOperationCount(VARIANT * pValue)
{
	PSL_BEGIN

	IO_COUNTERS ProcessIO;
	if(GetProcessIO(&ProcessIO))
		CPSLUtilities::SetVariant64Bit(pValue, ProcessIO.WriteOperationCount);
	else
		CPSLUtilities::SetVariant64Bit(pValue, 0);

	PSL_END
}

STDMETHODIMP CPSLProcessIO::get_OtherOperationCount(VARIANT * pValue)
{
	PSL_BEGIN

	IO_COUNTERS ProcessIO;
	if(GetProcessIO(&ProcessIO))
		CPSLUtilities::SetVariant64Bit(pValue, ProcessIO.OtherOperationCount);
	else
		CPSLUtilities::SetVariant64Bit(pValue, 0);

	PSL_END
}

STDMETHODIMP CPSLProcessIO::get_ReadTransferCount(VARIANT * pValue)
{
	PSL_BEGIN

	IO_COUNTERS ProcessIO;
	if(GetProcessIO(&ProcessIO))
		CPSLUtilities::SetVariant64Bit(pValue, ProcessIO.ReadTransferCount);
	else
		CPSLUtilities::SetVariant64Bit(pValue, 0);

	PSL_END
}

STDMETHODIMP CPSLProcessIO::get_WriteTransferCount(VARIANT * pValue)
{
	PSL_BEGIN

	IO_COUNTERS ProcessIO;
	if(GetProcessIO(&ProcessIO))
		CPSLUtilities::SetVariant64Bit(pValue, ProcessIO.WriteTransferCount);
	else
		CPSLUtilities::SetVariant64Bit(pValue, 0);

	PSL_END
}

STDMETHODIMP CPSLProcessIO::get_OtherTransferCount(VARIANT * pValue)
{
	PSL_BEGIN

	IO_COUNTERS ProcessIO;
	if(GetProcessIO(&ProcessIO))
		CPSLUtilities::SetVariant64Bit(pValue, ProcessIO.OtherTransferCount);
	else
		CPSLUtilities::SetVariant64Bit(pValue, 0);

	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