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