سؤال

I managed to query the httpd.exe service 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top