方法一: manifest文件
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
</trustInfo>
</assembly>
2.把此文件"import"到资源文件中,资源类型为24,资源ID为1,
方法二:修改注册表:
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files\\win7test2.exe"="RUNASADMIN"
将上面的内容保存为.reg文件,基中把C:\\Program Files\\win7test2.exe改为真实环境的应用程序地址,双击运行即可。
*****************
*****************************************
方法一: VC6:导入manifest文件
1.新建一文件命名为 xxx.manifest.内容如下:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="test.exe.manifest"
type="win32"
/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
2.把此文件"import"到资源文件中,资源类型为24,资源ID为1,
注:经测试资源类型必须为24,资源ID也必须为1,
3.重新编辑即可,在windows7下该程序图标有一个盾状小图标,双击运行会出现对话框要求提升为管理员权限。
如果是VC 2005或者2008
如果是C#.NET 2005或2008,则直接在项目中添加一个manifest文件,如app.manifest,创建后,把<requestedExecutionLevel
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
方法二:修改注册表:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files\\win7test2.exe"="RUNASADMIN"
将上面的内容保存为.reg文件,基中把C:\\Program Files\\win7test2.exe改为真实环境的应用程序地址,双击运行即可。
附:XP风格的manifest
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
本文介绍两种使程序在Windows系统上以管理员权限运行的方法:一种是通过在Visual C++环境中创建并导入特定格式的manifest文件;另一种是通过修改注册表设置。这两种方法均可实现在不改变程序代码的情况下,使程序启动时请求管理员权限。
1160

被折叠的 条评论
为什么被折叠?



