3
votes

How can i make a C++ Command Application run as admin?

the program im creating needs administrator rights so it would be nice to let the user get the run as admin prompt. How may i do that?

NOTE: Im not using Visual Studio, im using Code::Blocks 10.05;

-Stian

4
You can right-click on the exe, choose the Compatibility tab, and check Run as administrator. Now every time you start the exe, it will run as admin.kol
This is a program that people who dont know how to do that can use it, it is ment to be for more people than just me!?Stian

4 Answers

10
votes

You can also use the shell:

Right Click on the project => Configuration Properties => Linker => Manifest File => highestAvailable (/level='highestAvailable')

7
votes

You can create a manifest file to state that the application requires elevation to administrator. It's a normal text document that you can create in Notepad and it's loaded by Windows when the application is executed.

Here's an example of a manifest, for an application called MyApplication.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
  <assemblyIdentity version="1.0.0.0"
     processorArchitecture="X86"
     name="MyApplication"
     type="win32"/> 
  <description>Description of your application</description> 
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>

Then simply name it MyApplication.exe.manifest (replacing MyApplication with whatever your executable name is) and it'll get loaded by UAC automatically.

You can also embed the manifest in the resource section of your executable, if you name it appropriately.

See this for more details: http://msdn.microsoft.com/en-us/library/bb756929.aspx

0
votes

This solution applies to Visual Studio 2019.

If your like me, and prefer a GUI method, here you go:

  1. Navigate to your Solution Explorer window.
  2. Right click your project
  3. Select Properties
  4. Navigate to Configuration Properties -> Linker -> All Options
  5. Find UAC Execution Level
  6. Set to requireAdministrator (/level='requireAdministrator)

Visual steps:

Project Properties Selection Menu

Project Property Page