This article shows how to enumerate all the processes currently running on your system and display them with the process name, process ID tag, Kernel time and User time.
Process kernel mode and user mode times are amounts of time. For example, if a process has spent one second in kernel mode, this function will fill the
FILETIME structure specified by
lpKernelTime with a 64-bit value of ten million. That is the number of 100-nanosecond units in one second. To verify this, I used the following code:
DWORD dwLow, dwLow2, dwRes;
FILETIME ft, ft2;
dwLow = ft.dwLowDateTime;
st.wSecond += 1;
dwLow2 = ft2.dwLowDateTime;
dwRes = dwLow2 - dwLow;
Using the code
I first saved all the kernel and user times in an array of a structure which saves the 64 bit values in an <CODE lang=mc++>__int64 variable. Then after five seconds, I sample them again and calculate the difference, the difference is then divided by 156250. The number 156250 is 1/64 of the 64-bit value of ten million, the smallest amount of time a process is running. Now the CPU usage can be calculated, the sum of the kernel and user times divided by five. The console functions defined in Console.h are useful in any Win32 console application. The function
clrscr(int screenSize) can be called with
MAXIMUM, the first will set the screen buffer size to its normal size of 24 by 80, the second sets it to 60 by 80. The function
gotoxy(x, y) sets the cursor at line
y character position
x. The function
setrgb(int fgc, int bgc) is used to set the background and foreground colors. The colors are defined in an
enum in the header file, for example,
setrgb(BLACK, INT_WHITE) is the default.
<PRE lang=mc++>typedef struct tagPROCESSTIMES
Points of Interest
Being basically lazy, I don't like to click the maximize window button and then scroll to the top of the console window. See Console.cpp, I added code to set the console screen buffer to its maximum size when the second screen is called to display the results.
26, January 2005 - Version 1.0.