Reputation: 12247
I am am deleting a shortcut in windows Startup menu as part of delete/create based on settings in application to auto-start.
I originally create the shortcut via installation (I am using InnoSetup). The problem is my code will not remove the shortcut. But if delete the shortcut myself and let my code create it than it deletes it fine. But the shortcut name is exactly the same, I even check it in code if it exist and it does every time. What do I need to do so I can delete it when created as part of installation as well?
void SettingsDialog::on_checkBoxAutoStart_clicked()
{
QSettings settings;
QString startupFolder = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/Startup";
QString installPath = settings.value( INSTALL_PATH, "").toString();
// if path is empty, return
if (installPath.isEmpty() )
return;
QString appPath = installPath+ "\\MyApp.exe";
// if the exe doesn't exist for any reason, return
if( !QFile(appPath).exists() )
return;
QString shortcutName = startupFolder + "/MyApp.lnk";
if ( ui->checkBoxAutoStart->checkState() == Qt::Checked )
{
QFile::link( appPath, shortcutName);
settings.setValue( AUTO_START, "true" );
}
else
{
QFile shortcut( shortcutName);
if ( !shortcut.exists() )
qDebug() << "shortcut don't exist";
int shortcut_permissions = shortcut.permissions();
shortcut.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser | QFile::ExeUser);
shortcut_permissions = shortcut.permissions();
shortcut.remove();
qDebug() << shortcut.errorString();
settings.setValue( AUTO_START, "false" );
}
}
I am running this on Windows 7.
Update
The inno-setup line which creates the shortcut is this( I have renamed my application name to generic).
[Icons]
Name: "{commonstartup}\MyApp"; Filename: "{app}\MyApp.exe"
Upvotes: 1
Views: 1442
Reputation: 98495
TL;DR The problem lies entirely with your installer and your mistaken belief that a non-privileged process (like your Qt application) can mess with common profile shortcuts etc.
The installer does install it in current user profile.
This is false. See here for reference.
A shortcut created in the {commonstartup}
location applies to all users and can only be modified by a process running as administrator.
If you wish to create the shortcut for the current user, create it in {userstartup}
, and then your process, running with user privileges, will be able to modify it.
Be mindful that simply because you run on an administrative account, your Qt process is not running with administrative privileges unless you forcibly start it so, or unless you've set the binary flags on it to make Windows start it as an administrative process.
I would there has to be a way to remove the shortcut by the application so it auto starts or not, that's pretty standard.
It was pretty standard on Windows 95. On Windows XP, it was not supposed to be, but nobody paid any attention to the documentation. Since Vista and the arrival of UAC, it simply cannot be done by a non-administrative process if the startup shortcut is in the Public
account (formerly AllUsers
).
Furthermore, you can easily delete or modify that shortcut from Explorer, since you are an administrator, but that's because Explorer runs with administrative capabilities. Your Qt process doesn't. Neither will a non-administrative (default) command prompt.
Upvotes: 4