David Thielen
David Thielen

Reputation: 32854

mark a .exe to request (not require) run as admin

Is there a way to mark a .EXE to request it be run as admin? So that:

  1. If UAC is set to runas admin with no prompt - it runs as admin.
  2. If user cannot runas admin (reqires different login), runs as user.
  3. If prompts, prompts user. If accepted, runs as admin.
  4. If prompt declined, runs as user.

I know how to do this with 2 .exe programs. But I'd like to do it with one. This program enters the user's license key. In HKLM if the app has admin rights (so all users have the key). In HKCU if no admin rights.

Upvotes: 2

Views: 2613

Answers (2)

Kate Gregory
Kate Gregory

Reputation: 18944

You can get 1 and 2 by requesting highestAvailable instead of requireAdmin. I don't like it, though, because throughout the rest of the app you may need to test to see if you're elevated or not.

You can't get #4. If an app tries to launch, shows a UAC dialog, and the user rejects the UAC, the app does not launch. If you have just part of your application that needs elevation, you are best to move that part to a separate exe and put a manifest on that exe that requires elevation, then leave the main app not requiring it.

Upvotes: 1

Harry Johnston
Harry Johnston

Reputation: 36308

There is no way to mark an executable so that it will continue to run without admin privilege if the user rejects the elevation prompt. However, a process can attempt to launch a second copy of itself from the same executable with elevated privileges, and either pass the work to the new process (if it launches successfully) or continue the work itself (if not).

See this answer for an example of how to elevate yourself.

Upvotes: 3

Related Questions