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

后端 未结 3 929
逝去的感伤
逝去的感伤 2020-12-11 08:04

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 pr

相关标签:
3条回答
  • 2020-12-11 08:49

    You can also use the shell:

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

    0 讨论(0)
  • 2020-12-11 09:03

    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 讨论(0)
  • 2020-12-11 09:08

    Add requireAdministrator to your application manifest.

    0 讨论(0)
提交回复
热议问题