473,545 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ 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=Wor d, Start, Parent Process Id=324, Process Id=4776
Application=Wor d, Start, Parent Process Id=324, Process Id=5236
Application=Wor d, Stop, Parent Process Id=324, Process Id=5236

any help appreciated. The code is attached.
thanks

Imports System
Imports System.Manageme nt

Module TaskMonitor

Dim Applications As New ApplicationColl ection

Dim WordStartWatche r As ManagementEvent Watcher
Dim WordStopWatcher As ManagementEvent Watcher
Dim ExcelStartWatch er As ManagementEvent Watcher
Dim ExcelStopWatche r As ManagementEvent Watcher

Sub Main()

StartWatching()

Console.WriteLi ne("press <enterto stop...")
Console.ReadLin e()

StopWatching()

End Sub

Private Sub StartWatching()

WordStartWatche r = New
ManagementEvent Watcher(Generat eStartQuery("WI NWORD.EXE"))
AddHandler WordStartWatche r.EventArrived, AddressOf WordStarted
WordStartWatche r.Start()

WordStopWatcher = New
ManagementEvent Watcher(Generat eStopQuery("WIN WORD.EXE"))
AddHandler WordStopWatcher .EventArrived, AddressOf WordStopped
WordStopWatcher .Start()

ExcelStartWatch er = New
ManagementEvent Watcher(Generat eStartQuery("EX CEL.EXE"))
AddHandler ExcelStartWatch er.EventArrived , AddressOf ExcelStarted
ExcelStartWatch er.Start()

ExcelStopWatche r = New
ManagementEvent Watcher(Generat eStopQuery("EXC EL.EXE"))
AddHandler ExcelStopWatche r.EventArrived, AddressOf ExcelStopped
ExcelStopWatche r.Start()

End Sub

Private Sub StopWatching()

WordStartWatche r.Stop()
RemoveHandler WordStartWatche r.EventArrived, AddressOf WordStarted
WordStartWatche r = Nothing

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

ExcelStartWatch er.Stop()
RemoveHandler ExcelStartWatch er.EventArrived , AddressOf ExcelStarted
ExcelStartWatch er = Nothing

ExcelStopWatche r.Stop()
RemoveHandler ExcelStopWatche r.EventArrived, AddressOf ExcelStopped
ExcelStopWatche r = Nothing

End Sub

Public Sub WordStarted(ByV al sender As Object, ByVal e As
EventArrivedEve ntArgs)

Applications.Ad d(e.NewEvent.Pr operties("Proce ssID").Value, New
Word(e.NewEvent .Properties("Pa rentProcessID") .Value,
e.NewEvent.Prop erties("Process ID").Value))
Console.WriteLi ne(String.Conca t("Application= Word, Start, Parent
Process Id=", e.NewEvent.Prop erties("ParentP rocessID").Valu e.ToString, ",
Process Id=", e.NewEvent.Prop erties("Process ID").Value.ToSt ring))

End Sub

Public Sub WordStopped(ByV al sender As Object, ByVal e As
EventArrivedEve ntArgs)

If
Applications.Co ntainsKey(e.New Event.Propertie s("ProcessID"). Value) Then
Applications.Re move(e.NewEvent .Properties("Pr ocessID").Value )
End If

Console.WriteLi ne(String.Conca t("Application= Word, Stop, Parent
Process Id=", e.NewEvent.Prop erties("ParentP rocessID").Valu e.ToString, ",
Process Id=", e.NewEvent.Prop erties("Process ID").Value.ToSt ring))

End Sub

Public Sub ExcelStarted(By Val sender As Object, ByVal e As
EventArrivedEve ntArgs)

Applications.Ad d(e.NewEvent.Pr operties("Proce ssID").Value, New
Word(e.NewEvent .Properties("Pa rentProcessID") .Value,
e.NewEvent.Prop erties("Process ID").Value))
Console.WriteLi ne(String.Conca t("Application= Excel, Start, Parent
Process Id=", e.NewEvent.Prop erties("ParentP rocessID").Valu e.ToString, ",
Process Id=", e.NewEvent.Prop erties("Process ID").Value.ToSt ring))

End Sub

Public Sub ExcelStopped(By Val sender As Object, ByVal e As
EventArrivedEve ntArgs)

If
Applications.Co ntainsKey(e.New Event.Propertie s("ProcessID"). Value) Then
Applications.Re move(e.NewEvent .Properties("Pr ocessID").Value )
End If

Console.WriteLi ne(String.Conca t("Application= Excel, Stop, Parent
Process Id=", e.NewEvent.Prop erties("ParentP rocessID").Valu e.ToString, ",
Process Id=", e.NewEvent.Prop erties("Process ID").Value.ToSt ring))

End Sub

Private Function GenerateStartQu ery(ByVal ProcessName As String) As
WqlEventQuery
Dim ApplicationStar tQuery As New WqlEventQuery
ApplicationStar tQuery.EventCla ssName = "Win32_ProcessS tartTrace"
ApplicationStar tQuery.QueryStr ing = String.Concat(" SELECT * FROM
Win32_ProcessSt artTrace where ProcessName = ", """", ProcessName, """")
Return ApplicationStar tQuery
End Function

Private Function GenerateStopQue ry(ByVal ProcessName As String) As
WqlEventQuery
Dim ApplicationStop Query As New WqlEventQuery
ApplicationStop Query.EventClas sName = "Win32_ProcessS topTrace"
ApplicationStop Query.QueryStri ng = String.Concat(" SELECT * FROM
Win32_ProcessSt opTrace where ProcessName = ", """", ProcessName, """")
Return ApplicationStop Query
End Function

End Module

Jun 10 '07 #1
0 1719

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

Similar topics

4
2947
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 loops; after that, the process is redone, or moves on to a new position of the same figure. With a slow system each build-up may take some time. But...
3
661
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 log as well as fire up my email class and generate an email to send me the error. But no matter what I do, I still get the icky looking yellow and...
3
6873
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 from rsSource into rsDest - if it finds a key violation then it deletes the current record from rsDest and adds the new record from rsSource. This...
13
4442
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 runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of...
10
14403
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 user to gain the required rights for execution (the external process needs to create a mailbox in Exchange, so it needs to be run as an Exchange Full...
2
2223
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 btnStartApp(object sender, System.EventArgs e) { Process procID; string procName; ProcessStartInfo psi = new ProcessStartInfo();
1
4832
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" and cmd.exe starts but never terminates. I'm relatively new to .NET, so I may be forgetting some thing obvious. Thanks! Here's the code: using...
1
3031
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 the web service methods. What I like about the asynchronous calls that they use events. So when I call the web service operation is finished is...
0
3732
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 out with a Word 2007 document, saves that document to disk, then saves the same document in an older format If the user saves a doc in an older...
0
7484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.