Einzelnen Beitrag anzeigen

Dezipaitor

Registriert seit: 14. Apr 2003
Ort: Stuttgart
1.701 Beiträge
 
Delphi 7 Professional
 
#21

Re: [Win2000] - Abmelden vom Dienst

  Alt 28. Mär 2009, 12:31
Das sollte man lieber nicht so machen. Du personifizierst den aktuellen Benutzer und schaltest das Privileg aber vom Prozess.
Weiterhin kann JWSCL alles, was du da verwendet hast (mit Winapi)

Schau mal hier:
Delphi-Quellcode:
uses
  JwaWindows,
  JwsclToken,
  JwsclTypes,
  JwsclComUtils,
  JwsclVersion,
  JwsclUtils,
  JwsclPrivileges;


procedure ImpersonateAndExecute(const Flag : DWORD);
var
  Token : TJwSecurityToken;
  PrivScope : IJwPrivilegeScope;
begin
  //raises EJwsclUnsupportedWindowsVersionException
  // EJwsclPrivilegeCheckException, EJwsclWinCallFailedException
  Token := TJwSecurityToken.CreateWTSQueryUserToken(TOKEN_ALL_ACCESS);
  TJwAutoPointer.Wrap(Token);

  //raises EJwsclAccessTypeException, EJwsclSecurityException
  Token.ImpersonateLoggedOnUser;

  try
    if TJwWindowsVersion.IsWindows2000(true) then
    begin
      //raises EJwsclPrivilegeException
      PrivScope := JwGetPrivilegeScope([SE_SHUTDOWN_NAME]);

      if not ExitWindowsEx(Flag,0) then
        RaiseLastWin32Error;
    end;
  finally
    Token.RevertToSelf;
  end;
end;
Noch einfacher:
Delphi-Quellcode:
uses
  JwaWindows,
  JwsclImpersonation,
  JwsclTypes,
  JwsclComUtils,
  JwsclVersion,
  JwsclUtils,
  JwsclPrivileges,
  SysUtils;

procedure ImpersonateAndExecute(const Flag : DWORD);
var
  Imp : IJwImpersonation;
  PrivScope : IJwPrivilegeScope;
begin
  //raises EJwsclProcessIdNotAvailable
  //EJwsclWinCallFailedException
  Imp := JwImpersonateLoggedOnUser;

  if TJwWindowsVersion.IsWindows2000(true) then
  begin
    //raises EJwsclPrivilegeException
    PrivScope := JwGetPrivilegeScope([SE_SHUTDOWN_NAME]);

    if not ExitWindowsEx(Flag,0) then
      RaiseLastOSError;
  end;
end;
Beachte die Exceptions die da geworfen werden können + RaiseLastWin32Error.

Hoffe das hilft
Christian
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: JEDI API LIB & Windows Security Code Library (JWSCL)
  Mit Zitat antworten Zitat