Click here to Skip to main content
15,896,606 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.3K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLProcessMemory.cpp : Implementation of CPSLProcessMemory

#include "stdafx.h"
#include "PSLProcessMemory.h"

#if (_WIN32_WINNT < 0x0501)
struct PROCESS_MEMORY_COUNTERS_EX
{
    DWORD cb;
    DWORD PageFaultCount;
    SIZE_T PeakWorkingSetSize;
    SIZE_T WorkingSetSize;
    SIZE_T QuotaPeakPagedPoolUsage;
    SIZE_T QuotaPagedPoolUsage;
    SIZE_T QuotaPeakNonPagedPoolUsage;
    SIZE_T QuotaNonPagedPoolUsage;
    SIZE_T PagefileUsage;
    SIZE_T PeakPagefileUsage;
    SIZE_T PrivateUsage;
};
#endif

CPSLProcessMemory::CPSLProcessMemory()
{
	m_LastPageFaultCount = 0;
	m_ProcessID = NULL;
	m_CurrentProcessID = ::GetCurrentProcessId();
}

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

void CPSLProcessMemory::FinalRelease()
{
}

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

bool CPSLProcessMemory::GetMemoryInfo(PROCESS_MEMORY_COUNTERS_EX * pInfo)
{
	bool bProcessOpen = false;
	HANDLE hProcess = NULL;
	if(m_ProcessID == m_CurrentProcessID)
		hProcess = ::GetCurrentProcess();
	else
	{
		hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, m_ProcessID);
		if(hProcess)
			bProcessOpen = true;
		else
		{
			if(::GetLastError() == ERROR_ACCESS_DENIED)
				SetException(exAccessDenied);
			return false;
		}
	}

	bool bResult = false;

	::memset(pInfo, 0, sizeof(PROCESS_MEMORY_COUNTERS_EX));
	pInfo->cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
	if(::GetProcessMemoryInfo(hProcess, (PROCESS_MEMORY_COUNTERS*)pInfo, sizeof(PROCESS_MEMORY_COUNTERS_EX)))
		bResult = true;
	else
	{
		// The only failure to expect is that the structure
		// is not supported on the current OS, so we need to use
		// the previous version of the structure:
		PROCESS_MEMORY_COUNTERS Info;
		::memset(&Info, 0, sizeof(PROCESS_MEMORY_COUNTERS));
		Info.cb = sizeof(PROCESS_MEMORY_COUNTERS);
		if(::GetProcessMemoryInfo(hProcess, &Info, sizeof(PROCESS_MEMORY_COUNTERS)))
		{
			::memcpy(pInfo, &Info, sizeof(PROCESS_MEMORY_COUNTERS));
			pInfo->PrivateUsage = 0; // Unknown;
			bResult = true;
		}
	}

	if(bProcessOpen)
		::CloseHandle(hProcess);

	return bResult;
}

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

STDMETHODIMP CPSLProcessMemory::get_PageFaultCount(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = Info.PageFaultCount;
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_PageFaultDelta(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
	{
		*pValue = Info.PageFaultCount - m_LastPageFaultCount;
		m_LastPageFaultCount = Info.PageFaultCount;
	}
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_PeakWorkingSetSize(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.PeakWorkingSetSize / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_WorkingSetSize(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.WorkingSetSize / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_QuotaPeakPagedPoolUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.QuotaPeakPagedPoolUsage / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_QuotaPagedPoolUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.QuotaPagedPoolUsage / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_QuotaPeakNonPagedPoolUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.QuotaPeakNonPagedPoolUsage / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_QuotaNonPagedPoolUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.QuotaNonPagedPoolUsage / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_PagefileUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.PagefileUsage / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_PeakPagefileUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.PeakPagefileUsage / 1024);
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLProcessMemory::get_PrivateUsage(long * pValue)
{
	PSL_BEGIN

	PROCESS_MEMORY_COUNTERS_EX Info;
	if(GetMemoryInfo(&Info))
		*pValue = (long)(Info.PrivateUsage / 1024);
	else
		*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