Delphi与Windows 7下的用户账户控制(UAC)机制 及 禁用兼容性助手

 WIN7, Vista提供的UAC机制,它的主要目的是防止对于操作系统本身的恶意修改。
对于Delphi程序的影响,UAC主要在于以下几点:
1、由于UAC机制,Delphi对于系统的操作可能无声的失败,而同样的程序,在2000/X下面可能运行正常。譬如注册表的改动。。。
2、为了避免这样的情况,Delphi程序必须支持Vista UAC标注,也就是说,在UAC程序的图标下面显示盾牌标志。这样可以在需要系统更高权限的时候,提醒用户。
为了让程序显示UAC标志,现在看来Vista是通过读取程序的资源(Resource)里面的MANIFEST资源,来决定是否显示“UAC盾牌”。
为了支持UAC,Delphi程序必须在资源里面嵌入MANIFEST信息。

1、首先编辑一个文件,内容如下:

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  3. <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
  4. <application>
  5. <!--The ID below indicates application support for Windows Vista -->
  6. <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
  7. <!--The ID below indicates application support for Windows 7 -->
  8. <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
  9. <!--The ID below indicates application support for Windows 8 -->
  10. <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
  11. </application>
  12. </compatibility>
  13. <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  14. <security>
  15. <requestedPrivileges>
  16. <requestedExecutionLevel level="requireAdministrator" />
  17. </requestedPrivileges>
  18. </security>
  19. </trustInfo>
  20. </assembly>

【注】compatibility字段是为了禁止兼容性助手弹出,trustInfo字段是为了解决uca提权。
保持为UAC.manifest,这里文件是随意的。特别注意红色的“requireAdministrator”,这个表示程序需要管理员(Administrator)才能正常运行。
 UAC Manifest 选项
  1. <requestedExecutionLevel level="asInvoker" uiAccess="false" />
  2. <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  3. <requestedExecutionLevel level="highestAvailable" uiAccess="false" />

2、然后编辑一个RC文件,名为uac.rc
  1. 1 24 UAC.manifest

其中:
1-代表资源编号
24-资源类型为RTMAINIFEST
UAC.manifest-前面的文件名称

3、用brcc32编译这个rc文件为res文件

  1. brcc32 uac.rc -fo uac.res
其中 -fo 设置输出文件名
4、在程序里面加入
{$R uac.res}

或者将3,4合并为 {$R 'uac.res' 'uac.rc'}

需要将uac.rc添加到项目中

让Delphi编译的时候,把uac.res编译进exe文件
程序图标下面显示UAC盾牌标志了。

5、注意,这个程序不能运行在subst 虚拟驱动器上,否则会提示“指定路径不存在”
6、在编译时若产生错误:在project->options->application->enable runtime themes前面的勾取消,再编译就可以了!


 



附件列表

    原文地址:https://www.cnblogs.com/zzmiot/p/4281207.html