Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way is using the <a href="http://msdn.microsoft.com/en-us/library/ms686837%28VS.85%29.aspx" rel="noreferrer">Tool Help library</a> (see TlHelp32 unit), or <a href="http://msdn.microsoft.com/en-us/library/ms682629%28VS.85%29.aspx" rel="noreferrer">EnumProcesses</a> on Windows NT (see PsAPI unit). Have a look at <code>JclSysInfo.RunningProcessesList</code> in the <a href="http://jcl.delphi-jedi.org/" rel="noreferrer" title="JEDI Code Library">JCL</a> for an example.</p> <p>Here's a quick example of how to get the user name of a process:</p> <pre><code>type PTokenUser = ^TTokenUser; TTokenUser = packed record User: SID_AND_ATTRIBUTES; end; function GetProcessUserName(ProcessID: Cardinal; out DomainName, UserName: string): Boolean; var ProcessHandle, ProcessToken: THandle; InfoSize, UserNameSize, DomainNameSize: Cardinal; User: PTokenUser; Use: SID_NAME_USE; _DomainName, _UserName: array[0..255] of Char; begin Result := False; DomainName := ''; UserName := ''; ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID); if ProcessHandle = 0 then Exit; try if not OpenProcessToken(ProcessHandle, TOKEN_QUERY, ProcessToken) then Exit; try GetTokenInformation(ProcessToken, TokenUser, nil, 0, InfoSize); User := AllocMem(InfoSize * 2); try if GetTokenInformation(ProcessToken, TokenUser, User, InfoSize * 2, InfoSize) then begin DomainNameSize := SizeOf(_DomainName); UserNameSize := SizeOf(_UserName); Result := LookupAccountSid(nil, User^.User.Sid, _UserName, UserNameSize, _DomainName, DomainNameSize, Use); if Result then begin SetString(DomainName, _DomainName, StrLen(_DomainName)); SetString(UserName, _UserName, StrLen(_UserName)); end; end; finally FreeMem(User); end; finally CloseHandle(ProcessToken); end; finally CloseHandle(ProcessHandle); end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload