QT打开外部“以管理员身份运行”权限的exe应用程序失败

问题原因:需要提升用户权限

解决办法:

.cpp文件

void XXX::StartPocess()
{
    QDir filedir;
    QString filePath,exeName,exepath;;
    exeName = "/XXX.exe";    //此处更改为你自己的exe文件
    filePath = QCoreApplication::applicationDirPath();//filedir.currentPath();
   
    exepath = filePath + exeName;

#ifdef Q_OS_WIN
  
    SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
    sei.lpVerb = TEXT("runas");
  
    sei.lpFile = exepath.toStdWString().c_str();
    sei.nShow = SW_SHOWNORMAL;//without this,the windows will be hiden
    if (!ShellExecuteEx(&sei))
    {
        DWORD dwStatus = GetLastError();
        if (dwStatus == ERROR_CANCELLED)
        {
           
            qDebug() <<"提升权限被用户拒绝\n";
        }
        else if (dwStatus == ERROR_FILE_NOT_FOUND)
        {
           
            qDebug() <<"所要执行的文件没有找到\n";
        }
    }
#else
    qDebug()<<"Not windows os!"
    return ;
#endif

    QStringList arguments;
    arguments << "-style" << "fusion";

    if(NULL == myProcess)
    {
        myProcess = new QProcess(this);
        myProcess->start(exepath, arguments);
    }else {
        myProcess->start(exepath, arguments);
    }

}

.h文件

#include <QCoreApplication>
#include <qdebug.h>

#ifdef Q_OS_WIN
#include <windows.h>
#endif


//此部分放入类中
 QProcess *myProcess; 

借鉴:https://blog.csdn.net/github_35960067/article/details/90410523

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值