Guest User

Untitled

a guest
Jun 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. Imports System.Management
  2.  
  3. Public Class Form1
  4.  
  5. Dim WithEvents ProcessStartWatcher As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"))
  6. Dim WithEvents ProcessStopWatcher As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))
  7.  
  8. Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
  9. ProcessStartWatcher.Start()
  10. ProcessStopWatcher.Start()
  11. End Sub
  12.  
  13. Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs)
  14. ProcessStartWatcher.Stop()
  15. ProcessStopWatcher.Stop()
  16. End Sub
  17.  
  18. Private Sub ProcessStartWatcher_EventArrived(sender As Object, e As System.Management.EventArrivedEventArgs) Handles ProcessStartWatcher.EventArrived
  19. Dim ProcessName As String = e.NewEvent.Properties("ProcessName").Value
  20. Dim PID As Integer = e.NewEvent.Properties("ProcessID").Value
  21.  
  22. MessageBox.Show(String.Format("Process ""{0}"" with ID {1} started.", ProcessName, PID))
  23. End Sub
  24.  
  25. Private Sub ProcessStopWatcher_EventArrived(sender As Object, e As System.Management.EventArrivedEventArgs) Handles ProcessStopWatcher.EventArrived
  26. Dim ProcessName As String = e.NewEvent.Properties("ProcessName").Value
  27. Dim PID As Integer = e.NewEvent.Properties("ProcessID").Value
  28.  
  29. MessageBox.Show(String.Format("Process ""{0}"" with ID {1} stopped.", ProcessName, PID))
  30. End Sub
  31. End Class
  32.  
  33. Const PollingInterval As Double = 2.0 'Seconds.
  34.  
  35. Dim WithEvents ProcessStartWatcher As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN " & PollingInterval & " WHERE TargetInstance ISA 'Win32_Process'"))
  36. Dim WithEvents ProcessStopWatcher As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN " & PollingInterval & " WHERE TargetInstance ISA 'Win32_Process'"))
  37.  
  38. (...)
  39.  
  40. Private Sub ProcessStartWatcher_EventArrived(sender As Object, e As System.Management.EventArrivedEventArgs) Handles ProcessStartWatcher.EventArrived
  41. Dim ProcessName As String = CType(e("TargetInstance"), ManagementBaseObject)("Name")
  42. Dim PID As Integer = CType(e("TargetInstance"), ManagementBaseObject)("ProcessId")
  43.  
  44. MessageBox.Show(String.Format("Process ""{0}"" with ID {1} started.", ProcessName, PID))
  45. End Sub
  46.  
  47. Private Sub ProcessStopWatcher_EventArrived(sender As Object, e As System.Management.EventArrivedEventArgs) Handles ProcessStopWatcher.EventArrived
  48. Dim ProcessName As String = CType(e("TargetInstance"), ManagementBaseObject)("Name")
  49. Dim PID As Integer = CType(e("TargetInstance"), ManagementBaseObject)("ProcessId")
  50.  
  51. MessageBox.Show(String.Format("Process ""{0}"" with ID {1} stopped.", ProcessName, PID))
  52. End Sub
Add Comment
Please, Sign In to add comment