如何通过WMI从'Win32_ProcessStopTrace'类中获取其他信息?

时间:2015-10-30 01:44:08

标签: c# .net vb.net wmi

方案

Foe hobbie或者练习我正在开发一个(非常)简单的流程监视器,它暴露两个事件以限制进程启动并通过两个ManagementEventWatcher对象停止进程。

使用ManagementEventWatcher对象使用对Win32_ProcessStartTraceWin32_ProcessStopTrace类的查询,我会依赖于处理启动和停止发现,然后我引发相应的EventArrivedEventHandler事件以显示已经开始或停止的过程,以面向事件的方式。

问题

问题是我想获取其他(基本)信息,例如已启动或停止的进程的可执行路径和PID,但Win32_ProcessStartTraceWin32_ProcessStopTrace类不揭露那个信息/财产。

另一方面,他们公开了一个ProcessName属性,在文档中说明了这一点:

  

您可以使用此名称来获取Win32_Process的实例   同样的过程。

然而,MSDN如何期望我能够有效地做到这一点,同时考虑到同一个文件名的各种进程可以同时运行?

如果这些类提供给我的唯一有用的信息是进程名称和父PID,那么在效率方面,MSDN期望我可以做什么来匹配目标进程我想获得进行WMI查询的其他信息Win32_Process类当它可以存在具有相同名称并由同一进程创建的各种进程时,什么都没有,所以......我现在非常困难。

我认为执行高效的Win32_Process查询更为一致,例如进程的 PID (不是父PID)。

问题

在C#或VB.Net中,并且在效率方面(因为为了避免我上面解释的内容),在EventArrivedEventHandler到达之后我可以做些什么来获得正确过程的其他信息?

我的期望是获得一些像PID这样的唯一标识符,然后我可以对Win32_Process类执行WMI查询以获取我想要的所有其他信息,而不仅仅是进程名称和父PID。

源代码

这是代码的相关部分:

Public Class ProcessWatcher : Implements IDisposable

    Private WithEvents processStartWatcher As ManagementEventWatcher
    Private WithEvents processStopWatcher As ManagementEventWatcher

    ''' <summary>
    ''' Occurs when a process starts (run).
    ''' </summary>
    Public Event ProcessStarted As EventArrivedEventHandler

    ''' <summary>
    ''' Occurs when a process stops (exit).
    ''' </summary>
    Public Event ProcessStopped As EventArrivedEventHandler

    Public Sub New()
        Me.processStartWatcher = New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"))
        Me.processStopWatcher = New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))
    End Sub

    ''' <summary>
    ''' Start monitoring for process starts and stops.
    ''' </summary>
    <DebuggerStepThrough>
    Public Sub Start()
        Me.processStartWatcher.Start()
        Me.processStopWatcher.Start()
    End Sub

    ''' <summary>
    ''' Stop monitoring for process starts and stops.
    ''' </summary>
    <DebuggerStepThrough>
    Public Sub [Stop]()

        Me.processStartWatcher.Stop()
        Me.processStopWatcher.Stop()

    End Sub

    Protected Overridable Sub RaiseProcessStartedEvent(ByVal e As EventArrivedEventArgs)
        RaiseEvent ProcessStarted(Me, e)
    End Sub

    Protected Overridable Sub RaiseProcessStoppedEvent(ByVal e As EventArrivedEventArgs)
        RaiseEvent ProcessStopped(Me, e)
    End Sub

    Private Sub ProcessStartWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs) _
    Handles processStartWatcher.EventArrived

        If (Me.ProcessStartedEvent IsNot Nothing) Then
            Me.RaiseProcessStartedEvent(e)
        End If

    End Sub

    Private Sub ProcessStopWatcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs) _
    Handles processStopWatcher.EventArrived

        If (Me.ProcessStoppedEvent IsNot Nothing) Then
            Me.RaiseProcessStoppedEvent(e)
        End If

    End Sub

End Class

一个用法示例:

Imports System.Management

Public Class Form1 : Inherits Form

    Private WithEvents processWatcher As New ProcessWatcher

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles MyBase.Load

        Me.processWatcher.Start()

    End Sub

    Private Sub ProcessWatcher_ProcessStarted(ByVal sender As Object, ByVal e As EventArrivedEventArgs) _
    Handles processWatcher.ProcessStarted

        Console.WriteLine(String.Format("Process started | Name: {0}", e.NewEvent.Properties("ProcessName").Value))
        Console.WriteLine(String.Format("Process started | PPid: {0}", e.NewEvent.SystemProperties("ParentProcessID").Value))

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
    Handles MyBase.FormClosing

        Me.processWatcher.Stop()

    End Sub

End Class

1 个答案:

答案 0 :(得分:2)

虽然文档没有讨论它,但Win32_ProcessStopTrace和Win32_ProcessStartTrace确实存在'ProcessID'属性,因为这两个类派生自Win32_ProcessTrace,它具有它。您可以使用WMIExplorer

等工具进行检查

enter image description here