0

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
IT researcher
  • 3,274
  • 17
  • 79
  • 143

1 Answers1

1

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

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Ok. Is there any Win32 API which does the same? They will also require elevated privileges? – IT researcher Jun 11 '14 at 13:37
  • 1
    @ITresearcher Yes, any Win32 function called from your process will have the same requirement (elevated privileges). – ken2k Jun 11 '14 at 13:44