I want to get all the instances of WINWORD processes. As shown in image i have running two windows in WINWORD process. I want to get their file name and path.
Here is my code
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
I am getting Process name, ID and Date and Time of a Process. I want to get instances of a Processes.
I am using this code for Word Instances but it is doing nothing for me.
Public Class 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
End Class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.