![]() |
痴情的香菜 · 面试官:“我裤子拉链开了,你怎么提醒我?”他 ...· 10 月前 · |
![]() |
鬼畜的可乐 · 苹果公司隐私新规将落地,对游戏、广告营销等从 ...· 10 月前 · |
![]() |
忐忑的毛衣 · 史上最赚钱的16个游戏IP,第一名战绩抵10 ...· 1 年前 · |
![]() |
豪爽的刺猬 · 雅尼紫禁城音乐会_抖抖音· 1 年前 · |
![]() |
爱搭讪的红金鱼 · 漫画全集在线观看 - 哔哩哔哩漫画· 1 年前 · |
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.