![]() |
很拉风的水煮肉 · 最高人民法院发布仲裁司法审查典型案例 - ...· 6 月前 · |
![]() |
跑龙套的蜡烛 · 北京地铁13号线或拆分,北京“回天”整治再提 ...· 1 年前 · |
![]() |
纯真的黑框眼镜 · 沈阳地铁9号线二期来了!这个板块又要大涨_建 ...· 1 年前 · |
![]() |
眼睛小的铅笔 · 【全国二手小鹏】全国小鹏二手车报价及图片 ...· 2 年前 · |
![]() |
魁梧的小刀 · 阿迪耐克50年纷争,谁才是最终赢家?· 2 年前 · |
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);
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.
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.
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.
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.
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);
The way the demo code is currently written, some system-level processes did not allow their name and time-information to be retrieved.
Aryan S wrote:pdhStatus = PdhAddCounter(hQuery, szWorkingSet, 0, &hWorkingSet); // failing here :: Access denied
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
termal wrote:My program crash with Access violation after:
pdhStatus = PdhCollectQueryData(hQuery);
hQuery
valid?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
termal wrote:here to set own machine name!?
szMachineName
.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
SendMessageTimeout(..., WM_NULL, ..., SMTO_ABORTIFHUNG, 4000, ...)
, which waits 4 seconds for the window to respond.
![]() |
魁梧的小刀 · 阿迪耐克50年纷争,谁才是最终赢家? 2 年前 |