紧张的热带鱼 · 白云区“放大招”,打造广州市港产城融合发展示范区· 4 月前 · |
爽快的凉茶 · 工信部第368批新车点评(燃油车篇):HS3 ...· 1 年前 · |
低调的风衣 · 《秦侠》动画与漫画对比:真的能说是改编?漫画 ...· 1 年前 · |
健壮的苦咖啡 · 解决小程序中webview页面多层histo ...· 1 年前 · |
正直的镜子 · 进入21世纪以来,一共20个NBA总冠军,五 ...· 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.
紧张的热带鱼 · 白云区“放大招”,打造广州市港产城融合发展示范区 4 月前 |