WMI query for determining either the service is doing its work or not
I managed to query the httpd.exe service开发者_StackOverflow using WMI to check whether it is running or stopped. Here is the code that I am playing with:
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Process Where Name='httpd.exe'"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
Now I would like to query the amount of CPU that the service is using. I want to know whether the running service is doing work or not. Can this be done? Am I asking the right question? need advice :)
Using Win32_Process
, You are able to get the UserModeTime
and the KernelModeTime
(given in 100 nanosecond units) which allows you to compute each CPU average during a certain time.
Assuming you get two Win32_Process
informations separate by a TimeInterval
(given in 100 nanosecond units).
UserTimeRate = ((UserModeTime2 - UserModeTime1) / TimeInterval) * 100;
KernelTimeRate =((KernelModeTime2 - KernelModeTime1) / TimeInterval) * 100;
CPU = (((UserModeTime2 - UserModeTime1) + (KernelModeTime2 - KernelModeTime1)) / TimeInterval) * 100;
If you want the rate from the begining, you can compute TimeInterval
from CreationDate
to now.
精彩评论