1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function TForm1.IsProcessOwnedByUser(ProcessID: Cardinal): Boolean;
var
hToken: NativeUInt;
TokenInformation: array[0..1023] of Byte;
TokenUser: PTokenUser;
dwSize: Cardinal;
hProcess: NativeUInt;
UserSID: SID_NAME_USE;
LResult: LongBool;
AccountName: array[0..255] of WideChar;
DomainName: array[0..255] of WideChar;
cbAccountName, cbDomainName: Cardinal;
CurrentUserName: String;
begin
Result := False;
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID);
if hProcess = 0 then
Exit;
try
if OpenProcessToken(hProcess, TOKEN_QUERY, hToken) then
try
dwSize := SizeOf(TokenInformation);
LResult := GetTokenInformation(hToken, TTokenInformationClass.TokenUser, @TokenInformation, dwSize, dwSize);
if LResult then
begin
TokenUser := PTokenUser(@TokenInformation);
cbAccountName := SizeOf(AccountName);
cbDomainName := SizeOf(DomainName);
LookupAccountSid(nil, TokenUser.User.Sid, AccountName, cbAccountName, DomainName, cbDomainName, UserSID);
CurrentUserName := GetCurrentUserName;
Result := CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, AccountName, -1, PChar(CurrentUserName), -1) = CSTR_EQUAL;
end;
finally
CloseHandle(hToken);
end;
finally
CloseHandle(hProcess);
end;
end; |
Partager