在启动时运行应用程序 [英] Run application on startup

查看:73
本文介绍了在启动时运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能解决这个问题.我有 qt 应用程序,如果用户勾选复选框,我希望这个应用程序在操作系统启动时启动.我已经用谷歌搜索过了,我想出了这个解决方案>

i am wondering if its possible to solve this problem. Ive got qt application and if user tick the checkbox, i want this application to launch on startup of operating system. Ive already googled, and ive come up with this solution>

我的 QT 应用程序需要管理员权限才能修改注册表,所以

my QT application needs admin privileges in order to modify registry, so

  1. 创建清单文件()

2.应用此命令

mt -manifest manifestfile -outputresource:binfile.exe;1

mt -manifest manifestfile -outputresource:binfile.exe;1

3.在QT中使用这段代码修改注册表

3.use this piece of code in QT to modify registry

void MainWindow::set_on_startup() {

QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);

       if (ui->checkBox->checkState()) {
        QString value = QCoreApplication::applicationFilePath(); //get absolute path of running exe
        QString apostroph = "\"";

        #ifdef DEBUG
        ui->textEdit->append(QCoreApplication::applicationFilePath ());
#endif

       value.replace("/","\\");
       value = apostroph + value + apostroph + " --argument";

#ifdef DEBUG
       ui->textEdit->append(value);
#endif
        //write value to the register
        settings.setValue("name", value);


    }
    else {
         settings.remove("name");
    }
}

所以,这看起来不错吧?但是...具有默认管理员权限的应用程序无法在操作系统启动时启动,但是没有管理员权限的应用程序无法修改注册表.所以,有一个解决方案 - 告诉用户,如果他想设置这个启动"选项,他首先需要以管理员身份启动应用程序,然后应用程序将能够修改注册表,并且默认权限将保持asInvoker",但这似乎真的不切实际,我认为用户会因此而气馁.

So, this looks good right ? BUT... application with default admin priveleges cant be launched on startup of operating system, BUT application without admin priveleges cant modify registry. So , there is one solution - tell a user, that if he wants to set this "startup" option, he first needs to start application as admin, then the application will be able to modify registry, and default privileges will remain "asInvoker", but this seems really impractical and i think that users will be discouraged by this.

那么,如何解决这个问题?其他应用程序如何解决这个问题?

So, how to solve this problem ? how other applications solve this problem ?

推荐答案

对于试图解决问题的每个人来说,这是 100% 可行的解决方案:

For everybody who are trying to solve the problem, this is the 100% working solution:

我如何向用户询问在运行时提升权限?

  1. 创建 app1.exe
  2. 使用管理员权限创建 app2.exe - 教程在第一篇文章中(清单文件、mt.exe 等)
  3. 当用户勾选 app1.exe 中的复选框时,我调用 app2.exe(例如不带参数)-您可以找到所有用于此的函数@上面刚刚发布的链接//好吧,实际上,您不必使用上面示例中的函数:我觉得这个电话好多了

  1. create app1.exe
  2. create app2.exe with admin priveleges - tutorial is in the first post (manifest file, mt.exe, etc..)
  3. when user tick the checkbox in app1.exe, i call the the app2.exe (for example with no arguments) - you can find all function for this @ the link ive just posted above // well, in fact, you dont have to use the function from the example above: i find this call much better

              QObject *parent = new QObject();
              QString program = AppToExec; //"/path/to/the/app2.exe"
              QStringList arguments ;
              arguments << ""; //just in case we want arguments
              QProcess *myProcess = new QProcess(parent);
              myProcess->start(program);

  • app2.exe,例如

  • app2.exe, for example

      QApplication a(argc, argv);
      MainWindow w;
    //  w.show();
     if (argc == 1) {
     w.test();
     a.quit();
    
    }
    

  • 问题已解决.

  • problem solved.

    这篇关于在启动时运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆