C++ Console Application prompt "Run as Admin" to run as administrator?

16,861

Solution 1

You can also use the shell:

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

Solution 2

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

Solution 3

Add requireAdministrator to your application manifest.

Share:
16,861
Stian
Author by

Stian

Updated on June 04, 2022

Comments

  • Stian
    Stian almost 2 years

    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