来自: lichengbin, 时间: 2005-05-13 12:18:49, ID: 3072366 <br>好象漏了点,PTOKEN_USER的定义没有。[
] <br>type <br> PTOKEN_USER = ^TOKEN_USER; <br> _TOKEN_USER = record <br> User: TSidAndAttributes; <br> end; <br> TOKEN_USER = _TOKEN_USER; <br><br>借花献佛,改动一点点。 <br>function GetUserAndDomainFromPID(ProcessId: DWORD; <br> var User, Domain: string): Boolean; <br>var <br> hToken: THandle; <br> cbBuf: Cardinal; <br> ptiUser: PSIDAndAttributes; <br> snu: SID_NAME_USE; <br> ProcessHandle: THandle; <br> UserSize, DomainSize: DWORD; <br> bSuccess: Boolean; <br>begin <br> Result := False; <br> ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId); <br> if ProcessHandle <> 0 then <br> begin <br> if OpenProcessToken(ProcessHandle, TOKEN_QUERY, hToken) then <br> begin <br> bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf); <br> ptiUser := nil; <br> while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do <br> begin <br> ReallocMem(ptiUser, cbBuf); <br> bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf, cbBuf); <br> end; <br> CloseHandle(hToken); <br><br> if not bSuccess then Exit; <br> UserSize := 0; <br> DomainSize := 0; <br> LookupAccountSid(nil, ptiUser.Sid, nil, UserSize, nil, DomainSize, snu); <br> if (UserSize <> 0) and (DomainSize <> 0) then <br> begin <br> SetLength(User, UserSize); <br> SetLength(Domain, DomainSize); <br> if LookupAccountSid(nil, ptiUser.Sid, PChar(User), UserSize, <br> PChar(Domain), DomainSize, snu) then <br> begin <br> Result := True; <br> User := StrPas(PChar(User)); <br> Domain := StrPas(PChar(Domain)); <br> end; <br> end; <br> FreeMem(ptiUser); <br> end; <br> CloseHandle(ProcessHandle); <br> end; <br>end; <br> <br>参见ID: 3071393 为贴子