Results 1 to 7 of 7

Thread: Process Monitor

Threaded View

  1. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    17

    Re: Process Monitor

    Quote Originally Posted by stanav View Post
    Use WMI for this. A quick Google for "wmi process events" returned this as the top hit:
    http://weblogs.asp.net/whaggard/arch...11/438006.aspx

    It's in C#, but can be converted to vb.net using online conversion tools. Or if you search enough, you should be able to find a vb.net version of it.
    well, this is the output:
    vb Code:
    1. Imports System
    2. Imports System.ComponentModel
    3. Imports System.Collections
    4. Imports System.Globalization
    5. Imports System.Management
    6.  
    7. Namespace WMI.Win32
    8.     Public delegate void ProcessEventHandler(Win32_Process proc)
    9.     Public Class ProcessWatcher
    10.      Inherits ManagementEventWatcher
    11.         ' Process Events
    12.         Public event ProcessEventHandler ProcessCreated
    13.         Public event ProcessEventHandler ProcessDeleted
    14.         Public event ProcessEventHandler ProcessModified
    15.  
    16.         ' WMI WQL process query strings
    17.         Shared ReadOnly String WMI_OPER_EVENT_QUERY = "SELECT Property FROM() As *
    18.         End Property
    19. __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA "Win32_Process"c"
    20.         static readonly String WMI_OPER_EVENT_QUERY_WITH_PROC =
    21.             WMI_OPER_EVENT_QUERY + " and TargetInstance.Name = '{0}'"
    22.  
    23.         Public  Sub New()
    24.             Init(String.Empty)
    25.         End Sub
    26.         Public  Sub New(ByVal processName As String)
    27.             Init(processName)
    28.         End Sub
    29.         Private  Sub Init(ByVal processName As String)
    30.             Me.Query.QueryLanguage = "WQL"
    31.             If String.IsNullOrEmpty(processName) Then
    32.                 Me.Query.QueryString = WMI_OPER_EVENT_QUERY
    33.             Else
    34.                 Me.Query.QueryString =
    35.                     String.Format(WMI_OPER_EVENT_QUERY_WITH_PROC, processName)
    36.             End If
    37.  
    38.             Me.EventArrived += New EventArrivedEventHandler(watcher_EventArrived)
    39.         End Sub
    40.         Private  Sub watcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
    41.             Dim eventType As String =  e.NewEvent.ClassPath.ClassName
    42.             Win32_Process proc = New
    43.                 Win32_Process(e.NewEvent("TargetInstance") as ManagementBaseObject)
    44.  
    45.             Select Case eventType
    46.                 Case "__InstanceCreationEvent"
    47.                     If Not ProcessCreated Is Nothing Then
    48.                          Dim break As ProcessCreated(proc)
    49.                     End If
    50.                 Case "__InstanceDeletionEvent"
    51.                     If Not ProcessDeleted Is Nothing Then
    52.                          Dim break As ProcessDeleted(proc)
    53.                     End If
    54.                 Case "__InstanceModificationEvent"
    55.                     If Not ProcessModified Is Nothing Then
    56.                          Dim break As ProcessModified(proc)
    57.                     End If
    58.             End Select
    59.         End Sub
    60.     End Class
    61.  
    62.     ' Auto-Generated running: mgmtclassgen Win32_Process /n root\cimv2 /o WMI.Win32
    63.     ' Renaming the class from Process to Win32_Process
    64.     Public Class Win32_Process
    65.          ...
    66.     End Class
    67. End Namespace
    68.  
    69. ' Sample Usage
    70. Dim procWatcher As ProcessWatcher =  New ProcessWatcher("notepad.exe")
    71. procWatcher.ProcessCreated += New ProcessEventHandler(procWatcher_ProcessCreated)
    72. procWatcher.ProcessDeleted += New ProcessEventHandler(procWatcher_ProcessDeleted)
    73. procWatcher.ProcessModified += New ProcessEventHandler(procWatcher_ProcessModified)
    74. procWatcher.Start()
    75.  
    76. ' Do Work
    77.  
    78. procWatcher.Stop()

    which obviously isn't going to work
    Last edited by DumbAndDumber; Jun 15th, 2011 at 02:40 PM. Reason: Grammar
    If my post helped you out, please rate me!
    Thread resolved? Mark it as Resolved!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width