2

I need to restart my application itself ALTERNATELY as administrator and as NON-administrator.

The problem is: When the application is in elevated mode then the restarted application will automatically also be in elevated mode!

See this example:

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;

So how can I restart the application in non-elevated mode from the application in elevated mode?

user1580348
  • 4,654
  • 2
  • 27
  • 73
  • https://blogs.msdn.microsoft.com/aaron_margosis/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app/ – David Heffernan May 02 '18 at 15:45
  • And it's a dupe of this one https://stackoverflow.com/questions/17152790/start-program-with-user-rights-from-within-a-elevated-program/17155871#17155871 – David Heffernan May 02 '18 at 15:47
  • I suspect this is a symptom of a design flaw. Why do you need to do this? Does it have anything to do with updating your application? – J... May 02 '18 at 16:29
  • [How can I launch an unelevated process from my elevated process and vice versa?](https://blogs.msdn.microsoft.com/oldnewthing/20131118-00/?p=2643) – Remy Lebeau May 02 '18 at 17:22
  • @J... No, it is not for update purposes. – user1580348 May 03 '18 at 10:46
  • Well, you've got the answer to your question anyway. – David Heffernan May 03 '18 at 21:03
  • A man in the middle of an empty desert dying of thirst asks: "Where can I find water?" - DH answers: "You can find bottled water in a drugstore in the next big city." Then he adds: "Well, you've got the answer to your question anyway." – user1580348 May 04 '18 at 10:46

0 Answers0