exe在Vista或Win7下以管理員權限運行——C#與Delphi解決方案

來源:互聯網
上載者:User

原地址:http://clacklin.blog.163.com/blog/static/103209820106108538269/

 開啟Vs2005或vs2008工程,看在Properties下是否有app.manifest這個檔案;如沒有,右擊工程在菜單中選擇“屬性”,

選中“安全性”,在介面中勾選“啟用ClickOnce安全設定”後,在Properties下就有自動產生app.manifest檔案。

開啟app.manifest檔案,在<security>下加入
<requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator" cess="false"/>
      </requestedPrivileges>,重新編譯即可,
全部代碼如下所示:
<xml version="1.0" encoding="utf-8">
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <applicationRequestMinimum>
        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
        <defaultAssemblyRequest permissionSetReference="Custom" />
      </applicationRequestMinimum>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

2) Delphi:Delphi程式必須在資源裡面嵌入MANIFEST資訊。
         首先編輯一個檔案,內容如下:
<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"/>
      </requestedPrivileges>
    </security>
</trustInfo>
</assembly>

儲存為UAC.manifest,這裡檔案是隨意的。特別注意紅色的“requireAdministrator”,這個表示程式需要管理員(Administrator)才能正常運行。
         然後編輯一個RC檔案,名稱為uac.rc 如下所示:

1 24 UAC.manifest
其中:
1-代表資源編號
24-資源類型為RTMAINIFEST
UAC.manifest-前面的檔案名稱
         用brcc32編譯這個rc檔案為res檔案,如下所示:
brcc32 uac.rc -fouac.res
         在程式裡面加入
{$R uac.res}
讓Delphi編譯的時候,把uac.res編譯進exe檔案
         把檔案放到vista或win7下運行,就會看程式表徵圖下面顯示UAC盾牌標誌了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.