Vista下讓程序以管理員權限運行

在某些時候,我們的程序需要執行一些系統的操作,需要使用管理員權限才能完成,所以,我們必須讓用戶使用管理員權限來執行我們的程序。本文主要介紹,,給應用程序添加一個manifest文件,程序運行時系統就會跳出UAC對話框,獲得管理權限。
 
以下使用VS2008+WTL8.0+Vista舉例 .
 
1. 使用VS2008建立WTL工程,在新建工程嚮導中選擇“Add Common Controls Manifest” 。
 
2. 編譯工程,在輸出目錄生成 *.exe.embed.manifest 文件,使用記事本打開,將level="asInvoker" 改成 level="requireAdministrator" .
 
3. 新建 Custom 資源,Resource types輸入24 .
 
4. 將*.exe.embed.manifest 文件的內容Copy到Custom 資源裏 .
 
5. 在resource.h 中將Custom 資源ID改成1 。
 
6. 編譯,這時,生成的EXE文件圖標上多了一個盾牌標誌。
 
7. 修改之後的Mainfest文件內容大概如下:
<?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"    
                                     processorArchitecture="x86"    
                                     publicKeyToken="6595b64144ccf1df"    
                                     language="*">
                        </assemblyIdentity>    
                </dependentAssembly>    
        </dependency>    
</assembly>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章