Einzelnen Beitrag anzeigen

NicoDE
(Gast)

n/a Beiträge
 
#6

Re: Nur als Dienst starten lassen

  Alt 16. Nov 2005, 09:53
Unter welchem Account (z.B. SYSTEM) wird der Dienst ausgeführt?

Denn wenn der Dienst unter dem lokalen SYSTEM-Account ausgeführt wird, dann könnte man in den Quelltext der Projektdatei schreiben:
Delphi-Quellcode:
if not IsLocalSystem() then
begin
  MessageBox(0, 'RTFM', nil, MB_ICONERROR or MB_SERVICE_NOTIFICATION);
  Exit;
end;
Die Implementierung von IfLocalSystem() könnte so aussehen:
Delphi-Quellcode:
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.
  Mit Zitat antworten Zitat