Thanks for this tip. But I found a own way to fix this problem.
For the development of a solution I implemented a small prototype in VB.Net. It looks like this:
Sub Main()
Dim processUser As String
Dim processName = "chrome"
Dim currentUser As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
Dim processList = Diagnostics.Process.GetProcesses().ToList().Where(Function(process) process.ProcessName.ToString().Equals(processName.ToLower()))
Try
For Each process In processList
processUser = GetProcessOwner(process.Id.ToString())
If process.ProcessName.ToString().Equals(processName.ToLower()) And currentUser.Equals(processUser) Then
process.Kill()
End If
Next
Catch ex As System.InvalidOperationException
'The process was closes by another user or application.
End Try
End Sub
Public Function GetProcessOwner(processId As String) As String
Dim query As String = "Select * From Win32_Process Where ProcessID =" & processId
Dim searcher = New ManagementObjectSearcher(query)
Dim processList As ManagementObjectCollection = searcher.Get()
For Each obj As ManagementObject In processList
Dim argList = New String() {String.Empty, String.Empty}
Dim returnValue As Int32 = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList))
If returnValue.Equals(0) Then
Return argList(1) & "\" & argList(0)
End If
Next
Return "NO OWNER"
End Function
In this solution, I first search for a specific process and iterate through all the processes found under that name. Then I get the ProcessOwner for the process and compare it with a specified user or the current user. If the users are identical, I can assume that the process was started with the searched user and can close the process.
Here is a possible solution as UiPath implementation:
Get process owner.xaml (5.1 KB) Kill Process.xaml (8.8 KB)
I’d like to note that this solution could be significantly improved by distinguishing between Windows offline users and online users (Windows AD users).