views:

100

answers:

2
+1  Q: 

Processes timing

How to calculate the total execution time of each application Win (process) with the group on day.

For example: the process - notepad.exe - 10 minutes today (in total)

+4  A: 

I would use a WMI query against Win32_Process and retrieve the CreationDate value which according to MSDN is the date (and time) a process began executing.

A good library for Delphi is available from Magenta Systems. This library includes several examples to help you get started.

This only tracks currently running processes. If your wanting to track processes which were run once but are no longer running, then you will want to hook into windows so you are notified at each application launch. One example of this would be to use the CBT hooks (computer based training, but its used for other things also) which allow you to get notified every time a window is created. If you use the window handle to then find the parent process, you can then use this to track for yourself how long the parent process is running.

skamradt
I stand corrected. I wasn't aware that Win32_Process tracked starting time of individual processes. I've deleted my useless answer.
Ken White
+1  A: 

The Win32 function GetProcessTimes is an alternative way of getting hold of the creation time and CPU time spent during the day. With execution time I assume you mean cpu time rather than wall clock time.

Here is a link to a blog post describing how to use GetProcessTimes in Delphi.

This is a more "direct" approach than using WMI, however WMI has other advantages such as being able to query other machines over the network.

As skamradt also suggests, this application needs to run continuously to track all starting and stopping processes.The only way I know of to get events on starting and stopping applications is to use WMI events. See this question for some more info on that as well as this Technet blog on scripting

The steps to do this would be to

  1. enumerate all running processes covered by this question
  2. and then call GetProcessTimes for each process as described here.
Christer Fahlgren