unit LocalSys;
interface
function IsLocalSystem(): Boolean;
implementation
uses
Windows;
function IsLocalSystem(): Boolean;
type
PTokenUser = ^TTokenUser;
TTokenUser =
record
User: TSidAndAttributes;
end;
const
SECURITY_NT_AUTHORITY: TSidIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
const
SECURITY_LOCAL_SYSTEM_RID = $00000012;
var
TokenHandle: THandle;
TokenInformation: PTokenUser;
TokenInformationLength: DWORD;
LocalSystemSid: PSid;
begin
Result := False;
if not OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, TokenHandle)
then
Exit;
try
TokenInformationLength := 0;
GetTokenInformation(TokenHandle, TokenUser,
nil, 0, TokenInformationLength);
TokenInformation := GetMemory(TokenInformationLength);
if nil = TokenInformation
then
Exit;
try
if not GetTokenInformation(TokenHandle, TokenUser, TokenInformation,
TokenInformationLength, TokenInformationLength)
then
Exit;
if not AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 1,
SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, LocalSystemSid)
then
Exit;
try
Result := EqualSid(LocalSystemSid, TokenInformation.User.Sid);
finally
FreeSid(LocalSystemSid);
end;
finally
FreeMemory(TokenInformation);
end;
finally
CloseHandle(TokenHandle);
end;
end;
end.