473,471 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trapping Process starts using WMI

I've a requirement to monitor when certain applications are started. I'm
using WMI called from VS Stusio 2005 in VB to trap Excel and Word starting.
I've written the following console application which wires up 4 events to
trap Excel/Word Start/Stop events. The application works fine for Excel but
Word behaves strangely. When I start an instance of Word I get the correct
message displayed in the console. When I start a second instance of Word I
get the Stop event fired (rather then the Start) - the console looks like
this
after the second instance is started - ie an immediate stop event and stop
message.

press <enterto stop...
Application=Word, Start, Parent Process Id=324, Process Id=4776
Application=Word, Start, Parent Process Id=324, Process Id=5236
Application=Word, Stop, Parent Process Id=324, Process Id=5236

any help appreciated. The code is attached.
thanks

Imports System
Imports System.Management

Module TaskMonitor

Dim Applications As New ApplicationCollection

Dim WordStartWatcher As ManagementEventWatcher
Dim WordStopWatcher As ManagementEventWatcher
Dim ExcelStartWatcher As ManagementEventWatcher
Dim ExcelStopWatcher As ManagementEventWatcher

Sub Main()

StartWatching()

Console.WriteLine("press <enterto stop...")
Console.ReadLine()

StopWatching()

End Sub

Private Sub StartWatching()

WordStartWatcher = New
ManagementEventWatcher(GenerateStartQuery("WINWORD .EXE"))
AddHandler WordStartWatcher.EventArrived, AddressOf WordStarted
WordStartWatcher.Start()

WordStopWatcher = New
ManagementEventWatcher(GenerateStopQuery("WINWORD. EXE"))
AddHandler WordStopWatcher.EventArrived, AddressOf WordStopped
WordStopWatcher.Start()

ExcelStartWatcher = New
ManagementEventWatcher(GenerateStartQuery("EXCEL.E XE"))
AddHandler ExcelStartWatcher.EventArrived, AddressOf ExcelStarted
ExcelStartWatcher.Start()

ExcelStopWatcher = New
ManagementEventWatcher(GenerateStopQuery("EXCEL.EX E"))
AddHandler ExcelStopWatcher.EventArrived, AddressOf ExcelStopped
ExcelStopWatcher.Start()

End Sub

Private Sub StopWatching()

WordStartWatcher.Stop()
RemoveHandler WordStartWatcher.EventArrived, AddressOf WordStarted
WordStartWatcher = Nothing

WordStopWatcher.Stop()
RemoveHandler WordStopWatcher.EventArrived, AddressOf WordStopped
WordStopWatcher = Nothing

ExcelStartWatcher.Stop()
RemoveHandler ExcelStartWatcher.EventArrived, AddressOf ExcelStarted
ExcelStartWatcher = Nothing

ExcelStopWatcher.Stop()
RemoveHandler ExcelStopWatcher.EventArrived, AddressOf ExcelStopped
ExcelStopWatcher = Nothing

End Sub

Public Sub WordStarted(ByVal sender As Object, ByVal e As
EventArrivedEventArgs)

Applications.Add(e.NewEvent.Properties("ProcessID" ).Value, New
Word(e.NewEvent.Properties("ParentProcessID").Valu e,
e.NewEvent.Properties("ProcessID").Value))
Console.WriteLine(String.Concat("Application=Word, Start, Parent
Process Id=", e.NewEvent.Properties("ParentProcessID").Value.ToS tring, ",
Process Id=", e.NewEvent.Properties("ProcessID").Value.ToString) )

End Sub

Public Sub WordStopped(ByVal sender As Object, ByVal e As
EventArrivedEventArgs)

If
Applications.ContainsKey(e.NewEvent.Properties("Pr ocessID").Value) Then
Applications.Remove(e.NewEvent.Properties("Process ID").Value)
End If

Console.WriteLine(String.Concat("Application=Word, Stop, Parent
Process Id=", e.NewEvent.Properties("ParentProcessID").Value.ToS tring, ",
Process Id=", e.NewEvent.Properties("ProcessID").Value.ToString) )

End Sub

Public Sub ExcelStarted(ByVal sender As Object, ByVal e As
EventArrivedEventArgs)

Applications.Add(e.NewEvent.Properties("ProcessID" ).Value, New
Word(e.NewEvent.Properties("ParentProcessID").Valu e,
e.NewEvent.Properties("ProcessID").Value))
Console.WriteLine(String.Concat("Application=Excel , Start, Parent
Process Id=", e.NewEvent.Properties("ParentProcessID").Value.ToS tring, ",
Process Id=", e.NewEvent.Properties("ProcessID").Value.ToString) )

End Sub

Public Sub ExcelStopped(ByVal sender As Object, ByVal e As
EventArrivedEventArgs)

If
Applications.ContainsKey(e.NewEvent.Properties("Pr ocessID").Value) Then
Applications.Remove(e.NewEvent.Properties("Process ID").Value)
End If

Console.WriteLine(String.Concat("Application=Excel , Stop, Parent
Process Id=", e.NewEvent.Properties("ParentProcessID").Value.ToS tring, ",
Process Id=", e.NewEvent.Properties("ProcessID").Value.ToString) )

End Sub

Private Function GenerateStartQuery(ByVal ProcessName As String) As
WqlEventQuery
Dim ApplicationStartQuery As New WqlEventQuery
ApplicationStartQuery.EventClassName = "Win32_ProcessStartTrace"
ApplicationStartQuery.QueryString = String.Concat("SELECT * FROM
Win32_ProcessStartTrace where ProcessName = ", """", ProcessName, """")
Return ApplicationStartQuery
End Function

Private Function GenerateStopQuery(ByVal ProcessName As String) As
WqlEventQuery
Dim ApplicationStopQuery As New WqlEventQuery
ApplicationStopQuery.EventClassName = "Win32_ProcessStopTrace"
ApplicationStopQuery.QueryString = String.Concat("SELECT * FROM
Win32_ProcessStopTrace where ProcessName = ", """", ProcessName, """")
Return ApplicationStopQuery
End Function

End Module

Jun 10 '07 #1
0 1713

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
3
by: JP | last post by:
I need to be able to trap errors at the application level. I added this code to the Global.asax file. The code I wrote is supposed to get the last error that was generated and write to the event...
3
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
10
by: Sorin Dolha [MCSD .NET] | last post by:
I would like to start a process from C# code as another user. The C# code is executed as the ASPNET user because it relies in a Web Page class, and I would like that the process will run as another...
2
by: PMac | last post by:
I'm trying to execute a stand-alone console application from an aspx page when a use clicks a button on that page. On the aspx page is the following lines of pertinent code: private void...
1
by: Brad | last post by:
I have an issue trying to execute commands in a web service. The command starts under the specified user. However, it never completes its execution. I can execute simple commands like "echo HELLO"...
1
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of...
0
by: ppardi | last post by:
I'm developing an addin for Word 2007 and I need to determine whether a user saves a Word 2007 document in an older format (97-2003) after a save as is done. The scenario is that the user starts...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.