Click here to Skip to main content
15,892,575 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.9K   2.6K   174  
Access detailed information about the current process the easiest way.
#pragma once


////////////////////////////////////////
// class CProcessorUsage;
//
// Calculates overal processor usage at
// any given time.
//
// The usage value is updated every 200
// milliseconds;
//
// The class is fully thread-safe;
//
class CProcessorUsage
{
	typedef BOOL (WINAPI * pfnGetSystemTimes)(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime );
	typedef LONG (WINAPI * pfnNtQuerySystemInformation)(ULONG SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);

	struct PROC_PERF_INFO
	{
		LARGE_INTEGER IdleTime;
		LARGE_INTEGER KernelTime;
		LARGE_INTEGER UserTime;
		LARGE_INTEGER Reserved1[2];
		ULONG Reserved2;
	};

public:

    CProcessorUsage();
    ~CProcessorUsage();

    short GetUsage();

private:

	void GetSysTimes(__int64 & idleTime, __int64 & kernelTime, __int64 & userTime);

	////////////////////////////////////////////////
	// Set of static variables to be accessed from
	// within critical section by multiple threads:
	//
	static DWORD s_TickMark;
	static __int64 s_time;
	static __int64 s_idleTime;
	static __int64 s_kernelTime;
	static __int64 s_userTime;
	static int s_lastCpu;
	static int s_cpu[5];
	static __int64 s_kernelTimeProcess;
	static __int64 s_userTimeProcess;
	static int s_cpuProcess[5];
    static int s_count;
    static int s_index;
	//
	/////////////////////////////////////////////////

	pfnGetSystemTimes s_pfnGetSystemTimes;
	pfnNtQuerySystemInformation s_pfnNtQuerySystemInformation;
	CRITICAL_SECTION m_cs;
	PROC_PERF_INFO * m_pInfo;
	ULONG m_uInfoLength;
};

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