我需要以管理员和非管理员身份重新启动我的应用程序 ALTERNATELY 。
问题是:当应用程序处于提升模式时,重新启动的应用程序将自动处于提升模式!
见这个例子:
var
ThisIsElevated: Boolean;
procedure TForm3.btnRestartClick(Sender: TObject);
begin
if ThisIsElevated then
ShellExecute(Handle, 'open', PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL)
else
ShellExecute(Handle, 'runas', PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL);
Close;
end;
procedure TForm3.CheckIsElevated;
const
TokenElevationType = 18;
TokenElevation = 20;
TokenElevationTypeDefault = 1;
TokenElevationTypeFull = 2;
TokenElevationTypeLimited = 3;
const
SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
const
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;
var
token: NativeUInt;
Elevation: DWord;
dwSize: Cardinal;
begin
if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, token) then
try
if GetTokenInformation(token, TTokenInformationClass(TokenElevation), @Elevation, SizeOf(Elevation), dwSize) then
begin
if Elevation = 0 then
begin
Self.Caption := 'NOT ELEVATED';
ThisIsElevated := False;
end
else
begin
Self.Caption := 'ELEVATED!';
ThisIsElevated := True;
end;
end
else
ShowMessage(SysErrorMessage(GetLastError));
finally
CloseHandle(token);
end
else
ShowMessage(SysErrorMessage(GetLastError));
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
CheckIsElevated;
end;
那么如何在提升模式下从应用程序以非提升模式重启应用程序?