[转]exe在Vista或Win7下以管理员权限运行——C#与Delphi解决方案
2010-08-24 11:01
555 查看
原地址:http://clacklin.blog.163.com/blog/static/103209820106108538269/
打开Vs2005或vs2008工程,看在Properties下是否有app.manifest这个文件;如没有,右击工程在菜单中选择“属性”,
选中“安全性”,在界面中勾选“启用ClickOnce安全设置”后,在Properties下就有自动生成app.manifest文件。
打开app.manifest文件,在<security>下加入
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" cess="false"/>
</requestedPrivileges>,重新编译即可,
全部代码如下所示:
<xml version="1.0" encoding="utf-8">
<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">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<applicationRequestMinimum>
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
<defaultAssemblyRequest permissionSetReference="Custom" />
</applicationRequestMinimum>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
2) Delphi:Delphi程序必须在资源里面嵌入MANIFEST信息。
首先编辑一个文件,内容如下:
<xml version="1.0" encoding="UTF-8" standalone="yes">
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
保存为UAC.manifest,这里文件是随意的。特别注意红色的“requireAdministrator”,这个表示程序需要管理员(Administrator)才能正常运行。
然后编辑一个RC文件,名称为uac.rc 如下所示:
1 24 UAC.manifest
其中:
1-代表资源编号
24-资源类型为RTMAINIFEST
UAC.manifest-前面的文件名称
用brcc32编译这个rc文件为res文件,如下所示:
brcc32 uac.rc -fouac.res
在程序里面加入
{$R uac.res}
让Delphi编译的时候,把uac.res编译进exe文件
把文件放到vista或win7下运行,就会看程序图标下面显示UAC盾牌标志了。
打开Vs2005或vs2008工程,看在Properties下是否有app.manifest这个文件;如没有,右击工程在菜单中选择“属性”,
选中“安全性”,在界面中勾选“启用ClickOnce安全设置”后,在Properties下就有自动生成app.manifest文件。
打开app.manifest文件,在<security>下加入
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" cess="false"/>
</requestedPrivileges>,重新编译即可,
全部代码如下所示:
<xml version="1.0" encoding="utf-8">
<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">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<applicationRequestMinimum>
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
<defaultAssemblyRequest permissionSetReference="Custom" />
</applicationRequestMinimum>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
2) Delphi:Delphi程序必须在资源里面嵌入MANIFEST信息。
首先编辑一个文件,内容如下:
<xml version="1.0" encoding="UTF-8" standalone="yes">
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
保存为UAC.manifest,这里文件是随意的。特别注意红色的“requireAdministrator”,这个表示程序需要管理员(Administrator)才能正常运行。
然后编辑一个RC文件,名称为uac.rc 如下所示:
1 24 UAC.manifest
其中:
1-代表资源编号
24-资源类型为RTMAINIFEST
UAC.manifest-前面的文件名称
用brcc32编译这个rc文件为res文件,如下所示:
brcc32 uac.rc -fouac.res
在程序里面加入
{$R uac.res}
让Delphi编译的时候,把uac.res编译进exe文件
把文件放到vista或win7下运行,就会看程序图标下面显示UAC盾牌标志了。
相关文章推荐
- VC 编写 Vista 或 win7 下需要管理员权限运行的程序
- win7使cmd.exe默认以管理员权限运行并添加到右键菜单
- VS 中引入app.manifest(c#程序在win7及以上windows版本下以管理员权限运行方法)
- 将exe程序在Win7下以管理员权限运行的方法
- Vista、Win7上.net程序如何自动使用管理员权限运行
- 将exe程序在Win7下以管理员权限运行的方法
- vista和win7下如何用管理员身份运行cmd.exe
- Delphi下如何使程序在Vista上用管理员权限运行
- C#程序 Win7 下提示以管理员权限运行
- C#程序 Win7 下提示以管理员权限运行
- 设置C#程序在Windows 7 Vista下以管理员权限运行(转)
- VC编写vista或win7下需要管理员权限运行的程序
- VC编写vista或win7下需要管理员权限运行的程序
- win7 Vista下让程序默认以管理员权限运行
- vs2005 ,2008,2010中引入app.manifest(即c#程序在win7下以管理员权限运行方法)
- 编写C#程序让其在Win7 下以管理员权限运行
- vs2005 中引入app.manifest(c#程序在win7下以管理员权限运行方法)
- VC编写vista或win7下需要管理员权限运行的程序
- Vista、Win7上.net程序如何自动使用管理员权限运行
- 编译完成的工程在WIN7或者VISTA下自动使用管理员权限运行