Looking for Delphi 7 code to detect if a program i

2019-02-05 01:44发布

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

Thanks in advance

[--- IMPORTANT UPDATE ---]

Having reviewed the code in the answers so far, I realise that my question maybe is not so clear, or at least is not complete:

  • I want to know whether my Delphi 7 program is started with the 'Run as admin' check box set.

  • In other words: I want to know whether it is possible for my Delphi 7 program to create/update files in the c:\Program Files... folders.

Just checking if you have administrator rights is not enough for this.

7条回答
男人必须洒脱
2楼-- · 2019-02-05 02:10

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.

查看更多
登录 后发表回答