WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Process ID

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get Username from a Process

 Keywords: get retrieve username user id name process procid 

#DefineFunction  GetProcessUsername( ProcessId, InclDomain )
   ;
   ; GetProcessUsername( ProcessHandle, InclDomain )
   ;
   ; Get username and domain from a supplied process handle.
   ;
   ; ProcessId : is a pointer to the process handle of which
   ;   to get the username from. If phProcess is
   ;   NULL the current process is used.
   ;
   ; InclDomain : if true will prepend the DOMAIN and  to
   ;   the returned string.
   ;
   ; Returns a reference to a static string containing the
   ; username or NULL on error.
   ;
   ;

   objWMI = ObjectGet('winmgmts:{impersonationLevel=impersonate}!//./root/cimv2')
   query = "Select * from Win32_Process Where ProcessID = '" : ProcessId : "'"
   colSWbemObjectSet  = objWMI.ExecQuery( query )
   ForEach obj In colSWbemObjectSet
     ;retrieves the user name and domain name under which the process is running.
     user = ObjectType( 'BSTR', '' )
     domain = ObjectType( 'BSTR', '' )
     obj.GetOwner(user, domain)
     objWMI = 0
     ;Build string based on the InclDomain Flag
      If InclDomain
         Return domain : '\' : user
     Else
         Return user
     EndIf
   Next
   ;Return Null if we get to here
   objWMI = 0
   Return('')
#EndFunction

AddExtender("wwprc44i.dll")
list=tListProc()
process=AskItemlist("Process List",list,@TAB,@UNSORTED,@SINGLE)
procname = ItemExtract( 1, process, '|' )
pid = ItemExtract( 2, process, '|' )

result = GetProcessUsername( pid, @TRUE )
Pause( procname, result )

result = GetProcessUsername( pid, @FALSE )
Pause( procname, result )
Exit




Article ID:   W17598
Filename:   Get Username from a Process.txt
File Created: 2013:04:01:09:10:16
Last Updated: 2013:04:01:09:10:16