程序请求管理员权限

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’)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-小蛙-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值