Windows下一个程序中以管理员权限调用起另一个程序的实现

如一个程序A.exe以普通权限运行,但需要以管理员权限调起B.exe,本文主要讲述两种办法,亲测可用。

1.第一种方法

#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.第二种方法

int _tmain(int argc, _TCHAR* argv[])

{

    qDebug()<<"updateBtn_clicked";
    QString exePath = "cmd.exe";
    WCHAR exePathArray[1024] = {0};
    exePath.toWCharArray(exePathArray);

    QString command = " -Updater";
    WCHAR commandArr[1024] = {0};
    command.toWCharArray(commandArr);

    HINSTANCE hNewExe = ShellExecute(NULL, L"runas",exePathArray,commandArr,NULL,SW_SHOWMAXIMIZED);
    int nReturn = (int)hNewExe;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wang_anna

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

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

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

打赏作者

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

抵扣说明:

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

余额充值