Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

ProSysLib: Dissecting the Process

, 22 Nov 2010 CPOL
Access detailed information about the current process the easiest way.
ProcessInfo32_bin.zip
ProcessInfo.exe
ProSysLib.dll
Interop.ProSysLib.dll
ProcessInfo64_bin.zip
Interop.ProSysLib.dll
ProcessInfo.exe
ProSysLib.dll
ProcessInfo_bin.zip
PSL64v0.9.dll
Interop.ProSysLib.dll
ProcessInfo.exe
PSL32v0.9.dll
ProSysLib_v0.9.zip
ProSysLib_v0.9
Bin
PSL32v0.9.dll
PSL64v0.9.dll
Help
Samples
C#2008
ProcessInfo
Properties
Settings.settings
ProcessViewer
Properties
Settings.settings
VB6
CPUInfo.exe
CPUInfo.frm
CPUInfo.frx
CPUInfo.vbp
CPUInfo.vbw
VC2008
Deployment
Src
mssccprj.scc
ProSysLib.def
ProSysLib.rgs
ProSysLib.vcproj.vspscc
ProSysLib.vssscc
PSLSystem.rgs
PSLv0.5Setup.zip
PSLv0.5Setup.exe
PSLv0.7Setup.zip
PSLv0.7Setup.exe
// 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)

Share

About the Author

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 22 Nov 2010
Article Copyright 2008 by Vitaly Tomilov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid