0

Let's say app.exe has been built with requireAdministrator manifest as detailed here (this is really needed for a reason that would be out of topic here). When launching the app, when Windows UAC is enabled, there is a normal prompt:

Now this application needs to run on Windows startup: in an Options dialog, there is a "Launch at Windows startup" checkbox that can be checked or not. When checked, it adds a key in the registry in

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

to enable this "launch on Windows startup feature".

Problem: At Windows startup, when app.exe is (automatically) launched in background, there is this UAC dialog box (see screenshot before), which is annoying for the user.

Question: How to make that app.exe can be launched 1. with requireAdminstrator 2. silently at Windows startup ?

What is the clean way to do this? Lots of software do this (indexing software that both run on background since Windows startup, and don't show such a dialog box).


Note: I would like to avoid TaskScheduler method if possible: https://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html

Basj
  • 41,386
  • 99
  • 383
  • 673
  • Finally, what works is ... a [TaskScheduler task](https://stackoverflow.com/a/45244583/1422096). – Basj Jul 21 '17 at 18:38

1 Answers1

2
  1. The indexing software "Everything" does show such a dlalog.
  2. The others you are thinking of run only services with admin rights, not UI.
  3. Running a service requires the software to be architected with that intent, ordinary user applications can be started with the service rules with the help of the "at" service, which you already know how to do (Task Scheduler).
  4. You can substitute some other service for Task Scheduler, at the risk of annoying the user through needless duplication, waste of resources, and likely new security flaws.

Set aside your preference, and use Task Scheduler. Or remove the background parts of your app and write a service to do those instead.

And be very careful when giving your code administrator rights. Any bug can result in subverting the entire security infrastructure. With great power comes great responsibility.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks for your answer. I wanted to make life easy for the end-users of my `app.exe`. Is there a way to achieve this by simply having a "Launch at startup" checkbox in my Options dialog? When checked, it would create this Task Scheduler task (or other method) automatically? – Basj Jul 18 '17 at 14:00
  • @Basj: You will need to prompt for administrator rights in order to create the auto-elevated task (UAC wouldn't be much good if non-elevated software could auto-run itself elevated), but yes it is possible. – Ben Voigt Jul 18 '17 at 14:01
  • Well, there is one way to avoid needing elevation for the "Launch at startup" option. Have your installer (which runs as admin already) create the task, but disabled. Then the options dialog can toggle that task between enabled/disabled. – Ben Voigt Jul 18 '17 at 14:03
  • Ok, so the prompt will only happen once (first startup), and then, once the task is created, it will silently run as Admin on each startup? Would you have an idea how to create such a task from code (any demo example)? – Basj Jul 18 '17 at 14:04
  • I don't have any installer, it's a portable `.exe`. – Basj Jul 18 '17 at 14:04
  • 1
    Here is the official example: [Using the Task Scheduler -- Starting an Executable When a User Logs On](https://msdn.microsoft.com/en-us/library/windows/desktop/aa382527(v=vs.85).aspx) – Ben Voigt Jul 18 '17 at 14:05
  • @Basj: Portable EXE and auto-elevation is not a great combination, because unelevated code can replace your program with something malicious, and then your auto-run entry will give the malware full privileges on next login. You might want your autorun option to copy the program to a more secure location, one that is only writable by administrators. – Ben Voigt Jul 18 '17 at 14:13
  • @Basj I suggest making this a new question. – zett42 Jul 18 '17 at 17:51
  • @zett42 Here it is : https://stackoverflow.com/questions/7770497/error-when-trying-to-register-a-task-with-task-scheduler-win7 – Basj Jul 18 '17 at 17:54