This article briefly describes how to use the Getprocesstimes API function to get how long a process has been running. The time value returned by the Getprocesstimes function can easily be converted into readable and available information. Refer to the following code snippet:
HANDLE hProcess;
FILETIME ftCreation,
ftExit,
ftKernel,
ftUser;
GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);
The running interface of this example program is shown in the following illustration:
Calculate when to run
The length of time that a process is running is the time elapsed between the time the process was created and the current time. This information is stored in the FILETIME structure. As long as the elapsed time is calculated, it is converted into an hour/minute/second form. Luckily, with the help of the COleDateTime class, the job is easy to accomplish.
COleDateTime timeNow = COleDateTime::GetCurrentTime(),
timeCreation = ftCreation;
COleDateTimeSpan timeDiff = timeNow - timeCreation;
Here you can use different methods in COleDateTimeSpan to get information about the lost hours/minutes.
Calculate Kernel and User time
Refer to the documentation, kernel and user time is the actual elapsed time. This value is represented in the FILETIME structure as a 100 nanosecond unit. There are two ways to convert it to available information:
Method One:
We can use some basic methods to convert it to a value in seconds, a nanosecond equivalent to one out of 10,000 seconds, because this time has been expressed in 100 nanoseconds, so use 10个百万 to divide it:
__int64 i64Kernel = *((__int64 *) &ftKernel);
DWORD dwKernel = (DWORD) (i64Kernel / 10000000U);