Capturing New Window/Form Event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there any way within the .NET framework to detect when a new Windows Form is opened/created? Or more simply when a new process is created

I need to be able to detect when the user starts up an application and grab the name of the executable. Is WMI the direction I want to go with System.ManagementEventWatcher? I can't seem to find any examples of what I want to do and I am not particularly familiar with WMI in general.
 
Well, I will attempt to answer my own question. I did some messing around with WMI and with just a few lines of code I was able to detect when new processes are created

------

// The WMI query to watch for a new process even
// The 'WITHIN 2' clause causes the watcher to poll the system every 2 seconds for a __InstanceCreationEven
// of type Win32_Process - i.e. when a new process object is create
WqlEventQuery newProcEventQuery = new WqlEventQuery
"SELECT * FROM __InstanceCreationEvent WITHIN 2 "
"WHERE TargetInstance ISA \"Win32_Process\"" )

// Define an event watcher to watch for the query up abov
ManagementEventWatcher newProcWatcher = new ManagementEventWatcher( newProcEventQuery )

// Add a new handler to the watcher - the NewProcEvent method is the called when the event occur
newProcWatcher.EventArrived += new EventArrivedEventHandler( NewProcEvent )

// Start the watche
newProcWatcher.Start();
 
Back
Top