GetProcessTimes 用法及DEMO [转]

原文链接http://www.codeproject.com/Articles/4357/GetProcessTimes (需翻墙) 作者:DavidCrow Software Developer (Senior) Pinnacle Business Systems 这么一篇好文被墙掉实在可惜,先粗劣地转过来。点原文链接更精彩= =

 

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.

转载于:https://www.cnblogs.com/HenryThinker/archive/2012/12/12/2814568.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值