Is it possible to get a C++ application running in Windows to request administrator privileges from the operating system at run time?
I know it can be done at compile time, but can't seem to find anywhere whether it can be done at run time.
Thanks for your help!
EDIT: What if I want the current instance to have elevated privileges? For example, I might have data stored in memory which I want to keep.
How Do I Get Full Administrator Privileges On Windows 10? Search settings, then open the Settings App. Then, click Accounts -> Family & other users. Finally, click your user name and click Change account type – then, on the Account type drop-down, select Administrators and click OK.
To do this, right-click on the program's icon and select Run As Administrator. You will then be prompted to enter the administrator password. Once you do so, the program will run with the administrator. While it is the easiest way, it also means that users will need to know the PIN or password of the admin account.
Quick guide: Enable administrator account in Windows 10Open “Run” with [Windows] + [R]. Type “cmd” and press [Ctrl] + [Shift] + [Enter]. Type “net user administrator /active:yes”. The administrator account is now activated.
If you want the application to always elevate, you can give it a manifest, either by building one in (not compiling technically) or by putting an external manifest in the same folder as the exe. If you want to decide, as a person, to run it elevated, you right click the exe or short cut and choose Run As Administrator. If you are launching it from code, then as @vcsjones comments, you use the runas
verb when you launch that process. For example:
ShellExecute( NULL, "runas", "c:\\windows\\notepad.exe", " c:\\temp\\report.txt", NULL, // default dir SW_SHOWNORMAL );
Not quite, but you can do the opposite—you can drop privileges if you already have them. So, you can have your program start out running as an Administrator, using one of the methods listed by Kate Gregory. Then, drop your unneeded privileges; see Dropping privileges in C++ on Windows for how to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With