Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC
Article

GetProcessTimes

Rate me:
Please Sign up or sign in to vote.
4.29/5 (12 votes)
4 May 20042 min read 199.5K   2.9K   26   29
How to retrieve the running-time of a process

Sample Image - ProcessInfo.jpg

Introduction

This article is a brief explanation of how to use the GetProcessTimes API. There are times when knowing how long a process has been running might be useful.

The time values returned from GetProcessTimes are fairly easy to convert into something useful/readable. Let's operate on this code snippet:

HANDLE hProcess;
</CODE>FILETIME ftCreation,
         ftExit,
         ftKernel,
         ftUser;

GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);

Calculating running-time

A processes' running-time is the amount of time that has elapsed between the current time and the processes' creation time. This is stored in a FILETIME structure.

Once the elapsed time is calculated, then it's a matter of converting it to hours, minutes, seconds, etc. Luckily, the COleDateTime class makes this a painless process.

COleDateTime timeNow = COleDateTime::GetCurrentTime(),
timeCreation = ftCreation;
COleDateTimeSpan timeDiff = timeNow - timeCreation;

From here, you can use the different methods of COleDateTimeSpan to get the elapsed hours, minutes, etc.

Calculating kernel and user times

Per the documentation, the kernel and user times are amounts of time rather than an actual time period. The value in the FILETIME structure is expressed in 100-nanosecond units. To convert that to something useful, let's look at two methods.

Method 1

We can convert that to seconds with some basic arithmetic. A nanosecond is one billionth of a second, but since the time is already expressed in 100-nanosecond units, we'll only divide by 10 million:

__int64 i64Kernel = *((__int64 *) &ftKernel);
DWORD dwKernel = (DWORD) (i64Kernel / 10000000U);

As an alternative to the casting used above, a union could have just as easily been employed:

union
{
FILETIME ftKernel;
__int64 i64Kernel;
} timeKernel;

timeKernel.ftKernel = ftKernel;
DWORD dwKernel = (DWORD) (timeKernel.i64Kernel / 10000000U);

Either way, dwKernel now represents the number of elapsed seconds that the process has been in kernel mode. Converting seconds to hours, minutes, and seconds is a straightforward process.

Method 2

An alternative method that does not require anything other than a function call is to use the FileTimeToSystemTime API. This stores the result in a SYSTEMTIME structure, where we then have access to the wHour, wMinute, and wSecond members.

SYSTEMTIME stKernel;
FileTimeToSystemTime(&ftKernel, &stKernel);

The user-mode time is handled in the same way as kernel-mode time.

Summary

That's all there is to it. Looking at all of this together yields:

GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);

timeCreation = ftCreation;

strData.Format("Created at %02d:%02d:%02d", timeCreation.GetHour(), 
               timeCreation.GetMinute(), timeCreation.GetSecond());

timeDiff = timeNow - timeCreation;
strData.Format("Elapsed time = %ud %uh %um %us", timeDiff.GetDays(), 
               timeDiff.GetHours(), timeDiff.GetMinutes(), 
               timeDiff.GetSeconds());

FileTimeToSystemTime(&ftKernel, &stKernel);
strData.Format("Time in kernel mode = %uh %um %us", stKernel.wHour,
               stKernel.wMinute, stKernel.wSecond);

Notes

The way the demo code is currently written, some system-level processes did not allow their name and time-information to be retrieved.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

Comments and Discussions

 
Generalprogram compiles but no process's are shown Pin
johnBgood12327-Nov-07 14:39
johnBgood12327-Nov-07 14:39 
QuestionCan I modify process create time, and HOW? Pin
All is New26-Jul-07 4:16
All is New26-Jul-07 4:16 
QuestionHow to monitor processes on remote system? Pin
Aryan S2-May-07 19:10
Aryan S2-May-07 19:10 
AnswerRe: How to monitor processes on remote system? Pin
David Crow3-May-07 4:27
David Crow3-May-07 4:27 
Questionhow to get memory usage of each process Pin
14-Jul-04 3:12
suss14-Jul-04 3:12 
AnswerRe: how to get memory usage of each process [modified] Pin
David Crow14-Jul-04 7:39
David Crow14-Jul-04 7:39 
GeneralRe: how to get memory usage of each process Pin
Martial Spirit6-Jul-05 6:22
Martial Spirit6-Jul-05 6:22 
GeneralRe: how to get memory usage of each process Pin
De@r3-Aug-09 4:25
De@r3-Aug-09 4:25 
GeneralMissing file in project Pin
pranay5-May-04 1:51
pranay5-May-04 1:51 
GeneralRe: Missing file in project Pin
Coderider5-May-04 11:07
Coderider5-May-04 11:07 
GeneralRe: Missing file in project Pin
David Crow6-May-04 4:35
David Crow6-May-04 4:35 
QuestionHow to get CPU usage per process? Pin
Airport121-Apr-04 23:58
Airport121-Apr-04 23:58 
AnswerRe: How to get CPU usage per process? Pin
David Crow22-Apr-04 3:39
David Crow22-Apr-04 3:39 
Airport1 wrote:
How to get CPU usage per process?

This article hints at it, but here's a working example:

PDH_STATUS                pdhStatus;
HQUERY                    hQuery = NULL;
HCOUNTER                  hProcessorTime;
PDH_COUNTER_PATH_ELEMENTS pdhCpe;
PDH_FMT_COUNTERVALUE      pdhfmtProcessorTime;
TCHAR                     szProcessorTime[256],
                          szBuffer[256];
DWORD                     dwBufferSize;

__try
{
    pdhStatus = PdhOpenQuery(NULL, 0, &hQuery);
    if (pdhStatus != ERROR_SUCCESS)
        __leave;

    // make counter path
    pdhCpe.szMachineName    = TEXT("crow-nt");
    pdhCpe.szObjectName     = TEXT("Process");
    pdhCpe.szInstanceName   = TEXT("Idle");
    pdhCpe.szParentInstance = NULL;
    pdhCpe.dwInstanceIndex  = -1;
    pdhCpe.szCounterName    = TEXT("% Processor Time");

    dwBufferSize = sizeof(szProcessorTime);

    pdhStatus = PdhMakeCounterPath(&pdhCpe, szProcessorTime, &dwBufferSize, 0);
    if (pdhStatus != ERROR_SUCCESS)
        __leave;

    // add counter
    pdhStatus = PdhAddCounter(hQuery, szProcessorTime, 0, &hProcessorTime);
    if (pdhStatus != ERROR_SUCCESS)
        __leave;

    // get the data
    pdhStatus = PdhCollectQueryData(hQuery);
    if (pdhStatus != ERROR_SUCCESS)
        __leave;

    // format counter value
    pdhStatus = PdhGetFormattedCounterValue(hProcessorTime, PDH_FMT_LONG, NULL, &pdhfmtProcessorTime);
    if (pdhStatus != ERROR_SUCCESS)
        __leave;

    if (ERROR_SUCCESS == pdhStatus)
        wsprintf(szBuffer, TEXT("Processor Time = %ld\n"), pdhfmtProcessorTime.longValue);
}
__finally
{
    if (hQuery != NULL)
        pdhStatus = PdhCloseQuery(hQuery);
}



"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)


GeneralRe: How to get CPU usage per process? [modified] Pin
bosfan20-Jul-07 2:24
bosfan20-Jul-07 2:24 
QuestionRe: How to get CPU usage per process? Pin
David Crow20-Jul-07 2:30
David Crow20-Jul-07 2:30 
AnswerRe: How to get CPU usage per process? Pin
bosfan20-Jul-07 3:21
bosfan20-Jul-07 3:21 
GeneralRe: How to get CPU usage per process? Pin
David Crow20-Jul-07 3:24
David Crow20-Jul-07 3:24 
GeneralRe: How to get CPU usage per process? Pin
bosfan20-Jul-07 3:30
bosfan20-Jul-07 3:30 
GeneralRe: How to get CPU usage per process? Pin
David Crow20-Jul-07 3:35
David Crow20-Jul-07 3:35 
AnswerRe: How to get CPU usage per process? Pin
deee12313-Oct-04 4:59
deee12313-Oct-04 4:59 
GeneralRe: How to get CPU usage per process? Pin
David Crow13-Oct-04 5:08
David Crow13-Oct-04 5:08 
QuestionHow to get cpu cycles Pin
amarzee17-Sep-03 0:10
amarzee17-Sep-03 0:10 
AnswerRe: How to get cpu cycles Pin
David Crow22-Apr-04 3:39
David Crow22-Apr-04 3:39 
GeneralIdle Apps Pin
m.bergman1-Aug-03 5:29
sussm.bergman1-Aug-03 5:29 
GeneralRe: Idle Apps Pin
David Crow1-Aug-03 5:50
David Crow1-Aug-03 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.