我有一个应用程序正在使用 managementeventwatcher 监视设备上执行的进程,就像这样......
Dim wmiq As String = "SELECT TargetInstance FROM __InstanceCreationEvent WITHIN .025 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name like '%'"
Dim scope As String = "\\.\root\CIMV2"
startProcWatcher = New ManagementEventWatcher(scope, wmiq)
AddHandler startProcWatcher.EventArrived, AddressOf ProcessStarted
startProcWatcher.Start()
还有我的处理程序(现在只是登录)......
Private Shared Sub ProcessStarted(sender As Object, e As EventArrivedEventArgs)
Dim targetinstance As ManagementBaseObject = e.NewEvent.Properties("TargetInstance").Value
Dim processname As String = targetinstance.Properties("Name").Value.ToString
Dim exepath As String = targetinstance.Properties("ExecutablePath").Value.ToString
Dim thisexeinfo As New FileInfo(exepath)
If Not ProcessExclusionList.Contains(processname) Then
MyApp.DoLogging("Process Started : " & processname & "(" & exepath & ")")
End If
End Sub
这是一种享受,我以最少的资源使用成功地捕获了事件创建(而不是 Process.GetProcesses(),它正在锤击资源!),但是我注意到如果运行同一进程的第二个实例,我不会在第二次执行时获得一个事件。
例如,我可以运行计算器,而我的观察者将记录 calc.exe 是否使用所有相关属性执行。如果我然后打开第二个计算器,我的观察者什么也看不到。
我猜我需要稍微修改 WMI 查询,但我的 WMI 是有限的,我没有挣扎。
有人可以帮忙吗?
TIA