3

I wrote an application using Qt under Windows 7. The application starts up with normal user privileges, but I want to gain the Administrator privileges because I want to modify the registry to auto-start the application.

How could I do this?

houbysoft
  • 32,532
  • 24
  • 103
  • 156
Jichao
  • 40,341
  • 47
  • 125
  • 198
  • possible duplicate of [Is it possible for the executable to ask for Administrator rights? (Windows 7)](http://stackoverflow.com/questions/8915744/is-it-possible-for-the-executable-to-ask-for-administrator-rights-windows-7). Did you try the [search](http://stackoverflow.com/search)? – Deanna Jun 26 '12 at 10:31
  • @Deanna: not exactly a duplicate, because this has a much simpler solution : the admin rights are not needed if writing to `HKEY_CURRENT_USER` instead of `HKEY_LOCAL_MACHINE`. – houbysoft Jun 26 '12 at 14:37

6 Answers6

6

Take a look at the MSDN sample: UAC self-elevation (CSUACSelfElevation)

Also, Wikipedia actually has a pretty good reference including information on the ShellExecuteEx() "runas" verb and application manifest for elevation requests.

NuSkooler
  • 5,391
  • 1
  • 34
  • 58
5

You should embedd correct manifest to your exe:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb756929.aspx.

I know, you said using win32 API, but consider this standard and recommended way.

rkosegi
  • 14,165
  • 5
  • 50
  • 83
3

You do not need administrator privileges to autostart your application.

Simply write the appropriate keys to:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

which does not require administrator privileges (unlike the same path under HKEY_LOCAL_MACHINE).

houbysoft
  • 32,532
  • 24
  • 103
  • 156
  • but the app would only start if the logged user is the user who installed the app. – Jichao Mar 04 '13 at 08:22
  • 1
    @Jichao: that is what you want 99% of the time. Applications that auto start themselves for other users without the other users' permission are very annoying. – houbysoft Mar 06 '13 at 19:32
2

to auto-startup the application, you don't need admin rights! Instead of adding the registry key under HKLM (where you need admin rights), use HKCU and you're fine.

Stefan
  • 43,293
  • 10
  • 75
  • 117
1

Note that privileges cannot be changed while the program is running. It has to be killed and restarted with the correct privileges.

Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35
-1

To make an application auto start in HKLM, the administrative privilege is needed. But the privilege of an application could not change while running.

So I made a new small application whose task is only write the registry to make the main application auto start.

The main application could call this application with ShellExecuteEx to require administrative privlege.

Jichao
  • 40,341
  • 47
  • 125
  • 198