Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting administrator privileges at run time

Tags:

c++

windows

uac

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.

like image 302
JonaGik Avatar asked Jun 20 '11 23:06

JonaGik


People also ask

How do I obtain administrator privileges?

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.

How do I Run a program that requires administrator privileges under standard user?

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.

How do I force administrator privileges Windows 10?

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.


2 Answers

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   );  
like image 128
Kate Gregory Avatar answered Oct 14 '22 12:10

Kate Gregory


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.

like image 32
Adam Rosenfield Avatar answered Oct 14 '22 13:10

Adam Rosenfield