//给你完整的程序吧://运行通过的unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, PSAPI, TlHelp32, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } function GetProcIDByName(const ProcName: string; var ProcID: Cardinal): Boolean; function GetUsernameByProcID(const ProcID: Cardinal; var UserName: string): Boolean; end;var Form1: TForm1;implementation{$R *.dfm}function TForm1.GetProcIDByName(const ProcName: string; var ProcID: Cardinal): Boolean;var ExeName: string; ProcessEntry32: TProcessEntry32; ProcessListHandle: THandle; IsFound: Boolean;begin result:= false; ProcID:= 0; ExeName:= UpperCase(ExtractFileName(Trim(ProcName))); ProcessListHandle:= CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if ProcessListHandle <= 0 then exit; IsFound:= Process32First(ProcessListHandle, ProcessEntry32); while IsFound do begin if LowerCase(ExtractFileName(ProcessEntry32.szExeFile)) = LowerCase(ExeName) then begin result:= True; ProcID:= ProcessEntry32.th32ProcessID; exit; end; IsFound:= Process32Next(ProcessListHandle, ProcessEntry32); end;end;function TForm1.GetUsernameByProcID(const ProcID: Cardinal; var UserName: string): Boolean;type PTOKEN_USER = ^TOKEN_USER; _TOKEN_USER = record User: TSidAndAttributes; end; TOKEN_USER = _TOKEN_USER;var hToken: THandle; cbBuf: Cardinal; ptiUser: PTOKEN_USER; snu: SID_NAME_USE; ProcessHandle: THandle; UserSize, DomainSize: DWORD; bSuccess: Boolean; Domain: string;begin UserName:= ''; result:= false; try ProcessHandle:= OpenProcess(PROCESS_QUERY_INFORMATION, false, ProcID); if ProcessHandle <> 0 then begin //EnableProcessPrivilege(ProcessHandle, 'SeSecurityPrivilege', True); if OpenProcessToken(ProcessHandle, TOKEN_QUERY, hToken) then begin bSuccess:= GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf); ptiUser:= nil; while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do begin ReallocMem(ptiUser, cbBuf); bSuccess:= GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf, cbBuf); end; CloseHandle(hToken); if not bSuccess then begin exit; end; UserSize:= 0; DomainSize:= 0; LookupAccountSid(nil, ptiUser.User.Sid, nil, UserSize, nil, DomainSize, snu); if (UserSize <> 0) and (DomainSize <> 0) then begin SetLength(UserName, UserSize); SetLength(Domain, DomainSize); if LookupAccountSid(nil, ptiUser.User.Sid, PChar(UserName), UserSize, PChar(Domain), DomainSize, snu) then begin result:= True; UserName:= strpas(PChar(UserName)); Domain:= strpas(PChar(Domain)); end; end; if bSuccess then begin FreeMem(ptiUser); end; end; CloseHandle(ProcessHandle); end; finally if not result then begin UserName:= ''; Domain:= ''; end; end;end;procedure TForm1.Button1Click(Sender: TObject);var ProcName, UserName: string; ProcID: Cardinal;begin ProcName:= Edit1.Text; if not GetProcIDByName(ProcName, ProcID) then exit; if not GetUsernameByProcID(ProcID, UserName) then exit; Edit2.Text:= UserName;end;end.