Renan Greinert
Renan Greinert

Reputation: 3436

Is it possible for the executable to ask for Administrator rights? (Windows 7)

I am developing a partition disk program, and for me to read the \\\\.\\PhysicalDrive0 I need admin rights.

I am wondering if it is possible, in the run time, for the program to gain admin rights? Is there any win api for that?

I want to do that because I want the program to execute with admin rights only when it is reading/writing the disk. For security reasons, I don't want the program to execute all the time with admin rights, because someone could find a bug (stack or heap overflow for example) in some module and execute arbitrary commands as adm.

Upvotes: 11

Views: 7823

Answers (4)

wallyk
wallyk

Reputation: 57764

I have never seen a way to transition rights once a process has begun executing. The only way I know of is for the process to be created as privileged.

I look forward to other answers in case there is another way.

(update)
The article Teach Your Apps To Play Nicely With Windows Vista User Account Control (about halfway down) confirms that admin rights can be granted only at process creation time.

Upvotes: 4

Brackets
Brackets

Reputation: 572

Project's Propeties (Alt + Enter) -> Linker -> Manifest File -> UAC Execution level (in VS2015, in 2010 it's similar) -> requireAdministrator or highestAvailable

Edit: Also, if it's updating program, simply make your program's name starting with Update and Windows will automatically recognize it.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612794

You cannot acquire elevated privileges after the process has started. Your options are:

  1. Put the part of your application that requires elevated privileges into a separate process and manifest that with requireAdministrator.
  2. Run the part of your application that requires elevated privileges as an out-of-proc COM object.

Upvotes: 14

Kamil Klimek
Kamil Klimek

Reputation: 13130

You need to embed manifest with requireAdministrator flag

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

Upvotes: 1

Related Questions