Results 1 to 7 of 7

Thread: Process Monitor

  1. #1

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

    Post Process Monitor

    Hey
    So I got this idea to write a very small application for a school project that raises a balloon from the system tray everytime a new process is getting launched or an existing process quits.

    With my current code, I am getting the list of processes using a timer.

    This list needs to be compared with the previous list and detect differences (if this is possible, or should I use a different concept)? I have no clue how to do this.

    I currently have:
    -A ListView (ListView1), with the View property to "Details"
    -A Timer (at a 5000ms interval), with the following code:
    Code:
            Dim processList() As Process
            processList = Process.GetProcesses()
            For Each proc As Process In processList
                Dim Item As New ListViewItem
                Item.Text = proc.Id.ToString
                Item.SubItems.Add(proc.ProcessName.ToString)
                Item.SubItems.Add(proc.Responding.ToString)
                ListView1.Items.Add(Item)
            Next
    Anyone can help me out with the comparison part?

    tl;dr: Need some help with a piece of code to notify the user if a new process started or an existing process quit.
    Last edited by DumbAndDumber; Jun 15th, 2011 at 11:37 AM.
    If my post helped you out, please rate me!
    Thread resolved? Mark it as Resolved!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Process Monitor

    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.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #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!

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Process Monitor

    It obviously doesn't work because you did not follow the instructions in that article. Did you run mgmtclassgen.exe to generate the Process class and then rename it to Win32_Process? That is the comment near the end of that code which reads:
    Code:
    ' Auto-Generated running: mgmtclassgen Win32_Process /n root\cimv2 /o WMI.Win32
    ' Renaming the class from Process to Win32_Process
    Public Class Win32_Process
             ...
    End Class
    All you did was just copy the partial code shown in the article and converted it into VB.Net without even checking to see if the code is complete.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

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

    Re: Process Monitor

    Quote Originally Posted by stanav View Post
    It obviously doesn't work because you did not follow the instructions in that article. Did you run mgmtclassgen.exe to generate the Process class and then rename it to Win32_Process? That is the comment near the end of that code which reads:
    Code:
    ' Auto-Generated running: mgmtclassgen Win32_Process /n root\cimv2 /o WMI.Win32
    ' Renaming the class from Process to Win32_Process
    Public Class Win32_Process
             ...
    End Class
    All you did was just copy the partial code shown in the article and converted it into VB.Net without even checking to see if the code is complete.
    I ran the mgmtclassgen tool from the command-line and the Run-box but it gives an error (file not found).

    I'm pretty new to (Visual Basic) .NET, so I don't understand everything.

    What should I do?
    If my post helped you out, please rate me!
    Thread resolved? Mark it as Resolved!

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Process Monitor

    Quote Originally Posted by DumbAndDumber View Post
    I ran the mgmtclassgen tool from the command-line and the Run-box but it gives an error (file not found).

    I'm pretty new to (Visual Basic) .NET, so I don't understand everything.

    What should I do?
    You should be able to run the command from VS's command prompt (not Windows' cmd.exe one).

    Anyway, here is the whole thing that I've converted to VB.Net for you. Download the attached WMIWin32.vb file below. I haven't actually tested it but if the original C# code works then there is no reason the VB.Net version of it doesn't.
    Attached Files Attached Files
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7
    New Member
    Join Date
    May 2011
    Posts
    13

    Re: Process Monitor

    I've downloaded it, how to use that code?
    could you please explain?
    thanks

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