IT researcher
IT researcher

Reputation: 3304

Get all users of the process

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

Upvotes: 0

Views: 654

Answers (1)

ken2k
ken2k

Reputation: 48985

Starting from Windows Vista thanks to a new security model, you now need to have elevated privileges to access to properties of process the current user is not running. Same for plenty of other things such as writing to Program Files, accessing to HKLM...etc.

So basically you'll need to start your process elevated. See for example: Run elevated process

Upvotes: 1

Related Questions