0

I want to develop a windows application that will monitor whether a program has started (lets say windows calculator) and time for how much time it has been used. I know I could poll the process to see whether it is on but I am not sure that this is the best solution.

I was considering global system hooks but after looking for hours I ended up more confused than from when I started. Are there any pointers towards any tutorials on how to do what I want or just some ideas. Is this something that needs global hooks or something else is better?

I don't mind using either C# or C++ so please feel free point out to any relevant tutorials or documentation for either.

Thanks

sarnold
  • 102,305
  • 22
  • 181
  • 238

1 Answers1

0

Check process info (using polling)

Process [] processes = Process.GetProcessesByName("notepad");
if(processes.Length > 0)
   processes[0].StartTime;

Register an event using native Windows (WMI) APIs:

using System;
using System.ComponentModel;
using System.Collections;
using System.Globalization;
using System.Management;

namespace WMI.Win32
{
    public delegate void ProcessEventHandler(Win32_Process proc);
    public class ProcessWatcher : ManagementEventWatcher
    {
        // Process Events
        public event ProcessEventHandler ProcessCreated;
        public event ProcessEventHandler ProcessDeleted;
        public event ProcessEventHandler ProcessModified;

        // WMI WQL process query strings
        static readonly string WMI_OPER_EVENT_QUERY = @"SELECT * FROM 
__InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'";
        static readonly string WMI_OPER_EVENT_QUERY_WITH_PROC =
            WMI_OPER_EVENT_QUERY + " and TargetInstance.Name = '{0}'";

        public ProcessWatcher()
        {
            Init(string.Empty);
        }
        public ProcessWatcher(string processName)
        {
            Init(processName);
        }
        private void Init(string processName)
        {
            this.Query.QueryLanguage = "WQL";
            if (string.IsNullOrEmpty(processName))
            {
                this.Query.QueryString = WMI_OPER_EVENT_QUERY;
            }
            else
            {
                this.Query.QueryString =
                    string.Format(WMI_OPER_EVENT_QUERY_WITH_PROC, processName);
            }

            this.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
        }
        private void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            string eventType = e.NewEvent.ClassPath.ClassName;
            Win32_Process proc = new 
                Win32_Process(e.NewEvent["TargetInstance"] as ManagementBaseObject);

            switch (eventType)
            {
                case "__InstanceCreationEvent":
                    if (ProcessCreated != null) ProcessCreated(proc); break;
                case "__InstanceDeletionEvent":
                    if (ProcessDeleted != null) ProcessDeleted(proc); break;
                case "__InstanceModificationEvent":
                    if (ProcessModified != null) ProcessModified(proc); break;
            }
        }
    }

    // Auto-Generated running: mgmtclassgen Win32_Process /n root\cimv2 /o WMI.Win32
    // Renaming the class from Process to Win32_Process
    public class Win32_Process { ... }
}

// Sample Usage
ProcessWatcher procWatcher = new ProcessWatcher("notepad.exe");
procWatcher.ProcessCreated += new ProcessEventHandler(procWatcher_ProcessCreated);
procWatcher.ProcessDeleted += new ProcessEventHandler(procWatcher_ProcessDeleted);
procWatcher.ProcessModified += new ProcessEventHandler(procWatcher_ProcessModified);
procWatcher.Start();

// Do Work

procWatcher.Stop();
Salaros
  • 1,444
  • 1
  • 14
  • 34
  • Link only answers don't make for good answers. Keeping the content here is what we're aiming for with other sites as references. – ChrisF Jan 31 '12 at 22:44
  • Thanks for the response. However, I want my application to start with windows and waiting until the application I want (e.g., notepad) fires up. What you suggest is polling by periodically check the processes running. Or not? – Andreas Sotirakopoulos Jan 31 '12 at 22:55
  • You are right, my answer was incomplete, check this out: http://stackoverflow.com/questions/848618/net-events-for-process-executable-start – Salaros Jan 31 '12 at 22:57
  • @Andreas Sotirakopoulos check out my edited answer, I hope it will help you somehow – Salaros Jan 31 '12 at 23:18