Some programs are automatically asking for administrator rights when you run them. These programs are marked with a little shield in the bottom right corner:
Now I'm wondering how I could accomplish this in C++.
Is there a function for this or do I need some special compiler/linker commands for that?
Asked
Active
Viewed 4.0k times
20

Forivin
- 14,780
- 27
- 106
- 199
-
3It's not about [tag:c++], but how the properties of the program link are set! See _'Run as Administrator'_ checkbox. – πάντα ῥεῖ Oct 27 '13 at 12:24
-
4Include a properly crafted *manifest file* in your executable's resources. – syam Oct 27 '13 at 12:24
-
2It is operating system specific. On Linux & Unix (or POSIX) read about [Seteuid](https://en.wikipedia.org/wiki/Setuid) executables – Basile Starynkevitch May 24 '17 at 07:30
2 Answers
27
You must be probably looking for CreateProcessAsUser or CreateProcessWithLogonW function.
There is one more option like this:-
- Go to the project's Property Pages dialog box.
- Now open the Configuration Properties node.
- Next you have to open the Linker node.
- then select the Manifest File property.
- Finally modify the Enable User Account Control (UAC), UAC Execution Level, and UAC Bypass UI Protection properties.
Soemthing like this:
And as Syam correctly mentioned in the comments the solution could be to embed a manifest in the executable.

Rahul Tripathi
- 168,305
- 31
- 280
- 331
-
No, this doesn't achieve what OP wants ("shield" layer on the icon + automatically runs as administrator with a UAC prompt first). As I said in a comment, the solution is to embed a manifest in the executable. – syam Oct 27 '13 at 12:27
-
1Programs calling these functions automatically get marked with the icon extension when linked to, sure? – πάντα ῥεῖ Oct 27 '13 at 12:28
-
@syam:- Updated my answer with the one you mentioned. Hope that makes sense now ? – Rahul Tripathi Oct 27 '13 at 12:30
-
1@RahulTripathi I believe your solution involving changing the project's properties does exactly what I meant, just with a friendly UI instead of an awful XML file. :) – syam Oct 27 '13 at 12:51
5
Actually, this shield which means the application will need to run from administrator is set from the operating system if the application going to use any feature of that machine which needs permission from administrator.
You do not need to set anything from your app for that.
Hope the answer may help you.

Community
- 1
- 1

Mustakimur Khandaker
- 715
- 6
- 24