Run application on startup

微笑、不失礼 提交于 2019-12-03 13:11:53

You won't need admiministrator privileges if you use following key:

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

Notice

HKEY_CURRENT_USER

instead of using

HKEY_LOCAL_MACHINE
zeFree

My 2 cents! : )

Why not simply put the app shortcut in "Startup" folder.
Qt provides a cross-platform way of determining the paths to many default system directories using the QDesktopServices class.
(Source: Thanks to Dave Mateer for his answer to this question.)

The method is:

QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation)

This gives (on my Win 7):

C:\Users\user_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

and all we need is:

C:\Users\user_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Simple!

I use this without any hassle of UAC or any kind of rights problem in most of my apps.
This may not be the best way... but it is certainly an easy way.
(Please pitch in thoughts/comments if this approach has any big disadvantages.)

Update: To create the short-cut for the application in startup folder, use this code:

QFileInfo fileInfo(QCoreApplication::applicationFilePath());
QFile::link(QCoreApplication::applicationFilePath(), QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation) + QDir::separator() + "Startup" + QDir::separator() + fileInfo.completeBaseName() + ".lnk");

I hope this helps! : )

Include this header QSettings

#include <QSettings>

And add this into your code.

QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); settings.setValue("YourApplicationName", QCoreApplication::applicationFilePath().replace('/', '\\'));

Anton Giertli

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

How can I ask the user for elevated permissions at runtime?

  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);
    
  4. app2.exe, for example

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!