0

我想获取 WINWORD 进程的所有实例。如图所示,我在 WINWORD 进程中运行了两个窗口。我想获取他们的文件名和路径。

在此处输入图像描述

这是我的代码

 Dim processStartEvent As ManagementEventWatcher = New ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace")
    Dim processStopEvent As ManagementEventWatcher = New ManagementEventWatcher("SELECT * FROM Win32_ProcessStopTrace")

    Public Sub New()
        InitializeComponent()
        AddHandler processStartEvent.EventArrived, AddressOf Me.processStartEvent_EventArrived
        processStartEvent.Start()
        AddHandler processStopEvent.EventArrived, AddressOf Me.processStopEvent_EventArrived
        processStopEvent.Start()

    End Sub

    Private Sub processStartEvent_EventArrived(EventArrivedEventArgs, e)
        Dim processName As String = e.NewEvent.Properties("ProcessName").Value.ToString
        Dim processID As String = Convert.ToInt32(e.NewEvent.Properties("ProcessID").Value).ToString
        FileIO.WriteToFile("+ Process started. Name: " & processName & " | ID: " + processID)
        FileIO.WriteToFile(" | Date & Time: ")
        FileIO.WriteToFile(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & vbNewLine & vbNewLine)
    End Sub

    Private Sub processStopEvent_EventArrived(EventArrivedEventArgs, e)
        Dim processName As String = e.NewEvent.Properties("ProcessName").Value.ToString
        Dim processID As String = Convert.ToInt32(e.NewEvent.Properties("ProcessID").Value).ToString
        FileIO.WriteToFile("- Process stopped. Name: " & processName & " | ID: " + processID)
        FileIO.WriteToFile(" | Date & Time: ")
        FileIO.WriteToFile(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & vbNewLine & vbNewLine)
    End Sub

我正在获取进程名称、ID 以及进程的日期和时间。我想获取进程的实例。

4

1 回答 1

0

我将此代码用于 Word 实例,但它对我没有任何作用。

公共类 MyNewService Dim tmr As Timers.Timer

Public Sub New()
    InitializeComponent()
    Try
        Dim allProcesses = Process.GetProcesses().Where(Function(p) p.ProcessName.Contains("WINWORD.EXE"))
        Dim windowTitles = ChildWindowManager.GetChildWindowTitles(allProcesses.First().Id)
        For Each title In windowTitles
            If (title.Contains("- Word")) Then
                FileIO.WriteToFile(title)
            End If
        Next
    Catch ex As Exception
    End Try
End Sub

Class ChildWindowManager

    Delegate Function EnumThreadDelegate(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
    <DllImport("user32.dll")>
    Private Shared Function EnumThreadWindows(ByVal dwThreadId As Integer, ByVal lpfn As EnumThreadDelegate, ByVal lParam As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll")>
    Public Shared Function GetWindowText(ByVal hwnd As Integer, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
    End Function

    <DllImport("user32.dll")>
    Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
    End Function

    Private Shared Function EnumerateProcessWindowHandles(ByVal processId As Integer) As List(Of IntPtr)
        Dim windowHandles = New List(Of IntPtr)()

        For Each thread As ProcessThread In Process.GetProcessById(processId).Threads
            EnumThreadWindows(thread.Id, Function(hWnd, lParam)
                                             windowHandles.Add(hWnd)
                                             Return True
                                         End Function, IntPtr.Zero)
        Next
        Return windowHandles
    End Function

    Private Shared Function GetWindowTitle(ByVal hWnd As IntPtr) As String
        Dim length As Integer = GetWindowTextLength(hWnd)
        If length = 0 Then Return Nothing

        Dim titleStringBuilder As New Text.StringBuilder("", length)

        GetWindowText(hWnd, titleStringBuilder, titleStringBuilder.Capacity + 1)
        Return titleStringBuilder.ToString()
    End Function

    Public Shared Function GetChildWindowTitles(processId As Integer) As List(Of String)
        Dim windowTitles As New List(Of String)
        For Each handlee In EnumerateProcessWindowHandles(processId)
            Dim windowText = GetWindowTitle(handlee)
            If windowText <> Nothing Then
                windowTitles.Add(windowText)
            End If
        Next

        Return windowTitles
    End Function

End Class

Protected Overrides Sub OnStart(ByVal args() As String)
    System.Diagnostics.Debugger.Launch()
    tmr = New Timers.Timer()
    tmr.Enabled = True
    FileIO.WriteToFile("Service is Started!" + vbNewLine)
    FileIO.WriteToFile(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & vbNewLine & vbNewLine & vbNewLine)
    System.Diagnostics.Debugger.Launch()
End Sub

Protected Overrides Sub OnStop()
    tmr.Enabled = False
    FileIO.WriteToFile(vbNewLine)
    FileIO.WriteToFile("Service is Stopped!" + vbNewLine)
    FileIO.WriteToFile(System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & vbNewLine & vbNewLine)
End Sub

结束类

于 2019-11-18T04:57:47.440 回答