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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| function IsProcessOwnedByUser(ProcessID: Cardinal): Boolean;
function GetTokenUser(ATokenHandle: THandle): PTokenUser;
var
TokenInformationLength, ReturnLength: DWORD;
LastError: DWORD ;
begin
TokenInformationLength := SizeOf(Result);
ReturnLength := 0;
GetTokenInformation(ATokenHandle, TTokenInformationClass.TokenUser, nil, 0, ReturnLength);
LastError := GetLastError();
if (LastError = ERROR_INSUFFICIENT_BUFFER) and (TokenInformationLength <= ReturnLength) then
begin
GetMem(Result, ReturnLength);
try
if not GetTokenInformation(ATokenHandle, TTokenInformationClass.TokenUser, Result, ReturnLength, ReturnLength) then
RaiseLastOSError();
except
FreeMem(Result);
raise;
end;
end
else
RaiseLastOSError(LastError);
end;
procedure ReleaseTokenUser(var ATokenUser: PTokenUser);
begin
FreeMem(ATokenUser);
ATokenUser := nil;
end;
type
_USER_NAME_FORMAT = (NameUnknown,
NameFullyQualifiedDN, NameSamCompatible,
NameDisplay, NameUniqueId = 6,
NameCanonical, NameUserPrincipal,
NameCanonicalEx, NameServicePrincipal,
NameDnsDomain = 12);
TUserNameFormat = _USER_NAME_FORMAT;
const
Secur32 = 'Secur32.dll';
UNLEN = 256; // A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.
function GetCurrentUserName(NameFormat: TUserNameFormat = NameUnknown): string;
var
AccountName: array[0..UNLEN] of WideChar;
cbAccountName: DWORD;
Secur32H: HMODULE;
GetUserNameEx: function(NameFormat: TUserNameFormat; lpBuffer: PWideChar; var nSize: DWORD): BOOL; stdcall;
begin
Result := '';
cbAccountName := MAX_PATH;
if NameFormat <> NameUnknown then
begin
Secur32H := LoadLibrary(Secur32);
if Secur32H <> 0 then
try
GetUserNameEx := GetProcAddress(Secur32H, 'GetUserNameExW');
if Assigned(GetUserNameEx) and GetUserNameEx(NameFormat, AccountName, cbAccountName) then
Result := AccountName;
finally
FreeLibrary(Secur32H);
end;
end
else
if GetUserName(AccountName, cbAccountName) then
Result := AccountName;
end;
var
hProcess: THandle;
hToken: THandle;
TokenUser: PTokenUser;
UserSID: SID_NAME_USE;
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
TokenUser := GetTokenUser(hToken);
try
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;
finally
ReleaseTokenUser(TokenUser);
end;
finally
CloseHandle(hToken);
end;
finally
CloseHandle(hProcess);
end;
end; |
Partager