I used WMI to get user name of a required process which are running now. It works properly in windows XP and returns all the users who runs a process(ex:notepad.exe)
But in windows 8 it returns only the current user , other users which runs that process will not appear. When I checked I found that returnVal
was 2(access denied) instead of 0. But i was running it in administrative user.Run as administrator works properly. So is there any solution to this? Or Please provide me a alternative which can get all user of the process.
Public Function GetProcessOwner(ByVal processName As String) As String
Dim query As String = (Convert.ToString("Select * from Win32_Process Where Name = """) & processName) + """"
Dim searcher As New ManagementObjectSearcher(query)
Dim owner As String
Dim processList As ManagementObjectCollection = searcher.[Get]()
For Each obj As ManagementObject In processList
Dim argList As String() = New String() {String.Empty, String.Empty}
Dim returnVal As Integer = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList))
If returnVal = 0 Then
owner = owner & " " & argList(0)
End If
Next
Return Owner
End Function