Delphi与Windows 7下的用户账户控制(UAC)机制(有可能需要取消enable runtime themes)

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

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

[delphi] view plain copy
 
 
 
 print?
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">    
  3. <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">  
  4.     <security>  
  5.       <requestedPrivileges>  
  6.         <requestedExecutionLevel level="requireAdministrator"/>  
  7.       </requestedPrivileges>  
  8.     </security>  
  9. </trustInfo>  
  10. </assembly>  


保持为UAC.manifest,这里文件是随意的。特别注意红色的“requireAdministrator”,这个表示程序需要管理员(Administrator)才能正常运行。
 UAC Manifest 选项:

[delphi] view plain copy
 
 
 
 print?
  1. <requestedExecutionLevel  level="asInvoker" uiAccess="false" />  
  2. <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />  
  3. <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />  


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

[delphi] view plain copy
 
 
 
 print?
  1. 24 UAC.manifest  
  2.   
  3. 其中:<p>1-代表资源编号,24-资源类型为RTMAINIFEST,UAC.manifest-前面的文件名称  
  4. </p>  

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

[delphi] view plain copy
 
 
 
 print?
  1. brcc32 uac.rc -fouac.res  


其中 -fo 设置输出文件名
4、在Project程序里面加入:

[delphi] view plain copy
 
 
 
 print?
  1. {$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前面的勾取消,再编译就可以了!

http://blog.csdn.net/zisongjia/article/details/68941974

原文地址:https://www.cnblogs.com/findumars/p/6533812.html