9

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>

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

  1. create manifest file ( <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>)

2.apply this command

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

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");
    }
}

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 ?

Bart
  • 19,692
  • 7
  • 68
  • 77
Anton Giertli
  • 896
  • 2
  • 10
  • 29
  • A better solution would be to request root rights not on startup of the application but on setting the "start on boot" option. Could you not just create a tip next to the button "launch on boot" saying this action requires root rights and therefore UAC to pop up? The user would not be surprised then. Althoug a solution without UAC saying anything would be better, of course. – Redfox Mar 02 '12 at 13:57
  • well, i dont know how to acquire root privileges only after clicking on that checkbox. By the way, my current "solution" is causing UAC alert window to up and i dont consider it as something negative. As temporary solution, i will just probably leave the default priveleges "asInvoker" and write under that checkbox something like "this action requires admin priveleges, you have to launch applicatino with "run as administrator" option" – Anton Giertli Mar 02 '12 at 14:16
  • But still, im wondering how other application are solving this problem. For example, ive been browsing registry, and i found that Hamachi was there, and it really does start on system startup, but the application does not require admin priveleges, so how was this Registry entry made ? (probably during instalation, i guess.., but still, how user will cancel it via application if that application does not have admin priveleges) – Anton Giertli Mar 02 '12 at 14:20
  • 1
    For what @Redfox suggested, http://stackoverflow.com/questions/6108851/how-can-i-ask-the-user-for-elevated-permissions-at-runtime (and you can't use `QProcess`, because it only works with the WinAPI function `ShellExecute` and `QProcess` uses `CreateProcess`). – alexisdm Mar 02 '12 at 20:21
  • You can just create shortcut to your app main executable in current user's "Start menu / Startup" section (path may vary between different windows versions). I think this shouldn't require administrative privileges. – Aleksei Zabrodskii Mar 02 '12 at 23:21
  • @elmigranto I am doing the same thing, how to create short cut in startup folder using qt? – zar Jun 25 '15 at 18:58

5 Answers5

10

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
adnan kamili
  • 8,967
  • 7
  • 65
  • 125
6

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! : )

Community
  • 1
  • 1
zeFree
  • 2,129
  • 2
  • 31
  • 39
3

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('/', '\\'));

Mesrop
  • 31
  • 1
2

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.

Community
  • 1
  • 1
Anton Giertli
  • 896
  • 2
  • 10
  • 29
0

Using this link create stratup app and code:

void make_startup_app(){
    QString appName = "app.exe";
    QString appNameLink = appName+".lnk";
    QFile::link(appName, appNameLink);

    QString userName = QDir::home().dirName();

    QString dir_startup = "C:/Users/" + userName +
            "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/"+ appNameLink;
    QFile::copy(appNameLink, dir_startup);
}
S At
  • 422
  • 5
  • 15