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++開啟該檔案,如下:
<?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" uiAccess="false"></requestedExecutionLevel> </requestedPrivileges> </security> </trustInfo> </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的都有。
清單檔案如下:
<?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" uiAccess="false"></requestedExecutionLevel>
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"></assemblyIdentity>
</dependentAssembly>
</dependency>
</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 點選開啟連結