Looking for Delphi 7 code to detect if a program is started with administrator rights?

后端 未结 7 483
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 11:09

I am looking for working (obviously) Delphi 7 code so I can check whether my program is started with administrator rights<

7条回答
  •  滥情空心
    2020-12-30 11:27

    The Microsoft recommended way to solve this issue: Split the application into two.

    http://msdn.microsoft.com/en-us/library/aa511445.aspx

    The first app checks whether it is necessary to run the second one.

    The second app contains a "require admin" manifest (like David wrote) and you open it with the ShellExecuteEx 'runas' verb.

    In case of a web updater the workflow could be like this:

    Updater1.exe

    1. Checks if there are updates available.
    2. Optionally asks the user if he wants to install the updates.
    3. Downloads the updates to a temporary location.
    4. Runs Updater2.exe with ShellExecuteEx and the 'runas' verb.

    Updater2.exe

    1. Will get evalated on UAC if the users confirms the prompt or will not be run at all.
    2. Can then copy the files from the temp location to the final location.

    This has several advantages:

    • The Updater2 only contains the minimum operations that have to run elevated.
    • The Updater2 can be part of the files that get downloaded.
    • There is no need to check any privileges, UAC takes care of that.

    It also works on Windows XP, you get presented with a login dialog if you are not an admin.

提交回复
热议问题