Einzelnen Beitrag anzeigen

Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#1

Prüfen, ob ein Prozess ein SYSTEM Prozess ist

  Alt 28. Aug 2008, 16:02
Hey,

ich würde gerne prüfen, ob der eigene Prozess ein SYSTEM Prozess, ein Prozess des LOKALEN DIENSTES oder ein Prozess des NETZWERKDIENSTES ist. Dachte mir das in etwa so:

Delphi-Quellcode:
function IsSystemProcess: Boolean;

type
  PTOKEN_USER = ^TOKEN_USER;
  _TOKEN_USER = record
    User: TSidAndAttributes;
  end;
  TOKEN_USER = _TOKEN_USER;

const
  SECURITY_LOCAL_SYSTEM_RID = $00000012;

var
  hToken: THandle;
  cbBuf: Cardinal;
  ptiUser: PTOKEN_USER;

  bSuccess: Boolean;
  SystemSid: TSIDIdentifierAuthority;
  pSystemSid: PSID;
begin
  Result := false;
  
  if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) then
  begin
    try
      ptiUser := nil;
      bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf);
      while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do
      begin
        ReallocMem(ptiUser, cbBuf);
        bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf,
          cbBuf);
      end;
      if bSuccess then
      begin
        AllocateAndInitializeSID(SystemSid, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0,
          0, 0, 0, 0, 0, pSystemSid);
        Result := EqualSid(ptiUser.User.Sid, pSystemSid);
      end;
    finally
      if Assigned(ptiUser) then
      begin
        FreeMem(ptiUser);
      end;
      CloseHandle(hToken);
    end;
  end;
end;
Gibt allerdings immer false zurück. Mach ich was falsch?

Gruß Zacherl
  Mit Zitat antworten Zitat