I am making a C++ executable in Visual Studio that needs administrator rights for a part of the program and for the other part it doesn't need it, I was wondering if when the user presses the executable and the UAC pops up and presses yes to run as admin, it will run as admin, but when the UAC pops up and presses no, it will run without admin privileges.
Asked
Active
Viewed 248 times
0
-
Yes: write it so that if it doesn't run as admin (can be checked for instance by attempting to access the service manager), it runs itself again as admin using ShellExecute with runas verb, and if this fails then it continues running normally, otherwise it just waits for the elevated child process and forwards the exit code. – CherryDT Dec 23 '21 at 13:01
-
https://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time. – Pepijn Kramer Dec 23 '21 at 13:02
-
3@CherryDT Even better would be to defer the self-elevation to the point where the elevation is actually required. (1) This avoids a prompt under normal conditions where the special feature is not used, and (2) adheres to the principle of least privilege -- elevate only for the part that needs elevation. Once elevated, do your elevated thing and exit. Let the main program remain un-elevated. – Raymond Chen Dec 23 '21 at 15:22
-
@RaymondChen You are totally right, seems I read over the question too quickly and missed the part where they explained the reason. – CherryDT Dec 24 '21 at 09:26