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
<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 ?
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
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('/', '\\'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With