Granting c++ program administrative rights through UAC at startup

1.6k views Asked by At

So I've done some research, but I'm interested in understanding how one would make his program ask for administrative privileges from the UAC before its execution. Would this require one to use "runas" inside the ShellExecute()? I realize that this is something I will need to understand into my adventure of programming, as just about every program I have installed has asked me this. So for example, if I gave a program privileges at start up it would be able to execute any batch file or cmd statement through the system() function correct?

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    system("notepad");
    return 0;
}
1

There are 1 answers

0
Angus Connell On BEST ANSWER

You can set the requireAdmin in the executables manifest file, and then this will request your application to be raised to admin, the user may have to supply additional credentials (like run as). It will put a little shield against your application icon in explorer.

In Visual Studio

  • select properties on your project,
  • then open up "Configuration Properties"
  • then open up Linker
  • Select "Manifest File"
  • and modify "UAC Execution Level" to requireAdministrator

you can do this other ways but that's one of the easiest. So, in your example if the program you wrote had UAC execution level set to "requireAdministrator" then the program you call would then also be elevated in this case notepad. (I think that there may be some way to make sure that you are not elevated, but I have never done that)