且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++控制台应用程序提示“以管理员身份运行”以管理员身份运行?

更新时间:2023-10-20 09:56:52

您可以创建一个清单文件,说明应用程序需要升级到管理员。这是一个普通的文本文档,您可以在记事本中创建,并在应用程序执行时由Windows加载。

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.

这是一个清单的示例, c $ c> MyApplication 。

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>

然后将其命名为 MyApplication.exe.manifest MyApplication 替换为任何可执行文件名称),它将由UAC自动加载。

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

有关详细信息,请参阅此处: http://msdn.microsoft.com/en-us/library/bb756929.aspx

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