程式請求管理員許可權

-小蛙-發表於2016-09-03

1)ShellExecuteEx啟動程式提許可權

 #include <stdio.h>
 #include<windows.h>
 #include<tchar.h>

int _tmain(int argc,TCHAR* argv[])
{
    SHELLEXECUTEINFO sei={sizeof(SHELLEXECUTEINFO)};
    sei.lpVerb=TEXT("runas");
    sei.lpFile=TEXT("cmd.exe"); //add  application  which you want to run as administrator here
    sei.nShow=SW_SHOWNORMAL;    //without this,the windows will be hiden

    if(!ShellExecuteEx(&sei))
    {
        DWORD dwStatus=GetLastError();
        if(dwStatus==ERROR_CANCELLED){
        printf("提升許可權被使用者拒絕\n");
    }
    else if(dwStatus==ERROR_FILE_NOT_FOUND)
    {
        printf("所要執行的檔案沒有找到\n");
    }
    }

    return 0;
}

2)工程加入清單 app.manifest(C++builder中 Application –> Runtime Themes –> Use custom mainfest),使用 Resource Hacker 軟體開啟 EXE 檔案可以看到內嵌的 manifest (最後一欄)。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
          <assemblyIdentity type="win32" name="App" version="3.1.0.0" processorArchitecture="*"/>
          <dependency>
            <dependentAssembly>
              <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"/>
            </dependentAssembly>
          </dependency>
          <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
            <security>
              <requestedPrivileges>
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
                </requestedPrivileges>
            </security>
          </trustInfo>
          <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
            <application>
               <!--The ID below indicates application support for Windows 10 --> 
               <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>  
               <!--The ID below indicates application support for Windows 8.1 --> 
               <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>  
               <!--The ID below indicates application support for Windows Vista -->
               <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>   
               <!--The ID below indicates application support for Windows 7 -->
               <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>  
               <!--The ID below indicates application support for Windows 8 --> 
               <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>  
            </application>
          </compatibility>
        </assembly>

3)VC/S 中

1
找到VS2010的快捷方式:右擊——“開啟檔案位置”
找到VS2010的啟動專案devenv.exe:右擊——屬性——相容性——特權等級,以管理員許可權執行;如果需要每個使用者都以管理員許可權執行,還可以“更改所有使用者的設定”——特權等級,以管理員許可權執行。
然後在專案的開啟方式中確保以VS2010為預設開啟程式就好了。
2
屬性–聯結器–清單檔案-》UAC執行級別-》requireAdministrator
(/level=’requireAdministrator’)

相關文章