分给我吧,我正好要用分,呵呵。。。<br>//取进程所属的用户名和域,xp+DELPHI7下成功<br>function GetUserAndDomainFromPID(ProcessId: DWORD;<br> var User, Domain: string): Boolean;<br>var<br> hToken: THandle; <br> cbBuf: Cardinal; <br> ptiUser: PTOKEN_USER; <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 <br> begin<br> Exit; <br> end; <br><br> UserSize := 0; <br> DomainSize := 0; <br> LookupAccountSid(nil, ptiUser.User.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.User.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><br> if bSuccess then <br> begin <br> FreeMem(ptiUser); <br> end; <br> end; <br> CloseHandle(ProcessHandle); <br> end; <br>end;