Win10下应用程序默认管理员权限运行

 系统升级到win10后,普通应用程序不再默认以管理员权限运行了。当应用程序需要访问系统盘或调CreateFile函数进行IO驱动操作时,往往会失败,同时,通过GetLastError函数获取错误码为5——拒绝访问,权限不够。一般的解决办法是,右键***.exe,选择”以管理身份运行“,或者在“右键属性->兼容性”对话框中勾选“以管理员身份运行此程序”。(快捷方式也可以)

        有没有更彻底的解决办法?用户拿到程序后,无须任何设置,即可以管理员身份运行它。通过浏览网友的博客,大致找到了如下几种方法:


一、在VS中设置Manifest File中的UAC Execution Level

        按下图修改对应工程属性的选项:



        点击确认后,重新生成***.exe,即可以看到它的图标上有一个小盾牌。

        该方法来自博客:http://blog.csdn.net/whatday/article/details/39403199

        

二、VC++程序导入manifest文件

1,生成一个模板manifest文件

        以上面的Server工程为例,除了设置UAC之外,还需要进“工程属性 -> 清单工具 -> 输入输出 -> 嵌入清单”,将该选项修改为“否”。点击确认后,重新生成***.exe。在生成目录下可以看到“Server.exe.manifest”文件。默认是将manifest文件嵌入到了***.exe中,所以看不到。

        用notepad++打开该文件,如下:

  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" uiAccess= "false"></requestedExecutionLevel>
  7. </requestedPrivileges>
  8. </security>
  9. </trustInfo>
  10. </assembly>

2,修改模板文件名

        假设我有一个VC++的应用程序文件“Client.exe”,现将上面生成的“Server.exe.manifest”文件放到它的同一目录下,并修改其文件名为“Client.exe.manifest”文件,你会发现,“Client.exe”文件的图标上立即多了一个小盾牌。


        至此,方法2就OK啦。


三、QT程序设置Manifest File中的UAC Execution Level

        如果QT使用的VC++编译器,可以安装上面的两种方法设置。如果,QT使用的是QCreator+MinGW编译器,则需要在.pro文件中添加:
QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\" #以管理员运行
QMAKE_LFLAGS += /SUBSYSTEM:WINDOWS,\"5.01\" #VS2013 在XP运行


四、QT程序通过mt.exe导入Manifest File

        假设需要管理员权限的程序为MyApp.exe。把MyApp.exe,MyApp.exe.manifest,mt.exe放到同一个目录,打开命令提示符,cd到该目录,执行下面命令
mt.exe -manifest "MyApp.exe.manifest" -outputresource:"MyApp.exe";#1

mt.exe在C:\Program Files\Windows Kits下,装过visual studio的都有。

清单文件如下:

  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" uiAccess= "false"></requestedExecutionLevel>
  7. </requestedPrivileges>
  8. </security>
  9. </trustInfo>
  10. <dependency>
  11. <dependentAssembly>
  12. <assemblyIdentity type= "win32" name= "Microsoft.Windows.Common-Controls" version= "6.0.0.0" publicKeyToken= "6595b64144ccf1df" language= "*" processorArchitecture= "*"></assemblyIdentity>
  13. </dependentAssembly>
  14. </dependency>
  15. </assembly>

        比一般的vc++清单文件要多一项


注:方法3和4参考博客:http://blog.sina.com.cn/s/blog_a6fb6cc90101feia.html

                                             http://blog.csdn.net/u014699749/article/details/28204789


        

总结:

        无论哪种方法,都是要改变UAC —— User Account Control。理论如下:

UAC官方文档:https://technet.microsoft.com/en-us/library/cc709691(v=ws.10).aspx 点击打开链接

关于manifest:一分钟明白manifest的原理

MSDN:Manifest


转自:https://blog.csdn.net/sagittarius_warrior/article/details/51996305



猜你喜欢

转载自blog.csdn.net/phenixyf/article/details/80907765