Skip to content

Commit

Permalink
Refactor Settings::testWrite() calls
Browse files Browse the repository at this point in the history
Wrap in a runningAsAdmin() function. Abort the program if the result is
FormatError.
  • Loading branch information
slipher committed Apr 21, 2024
1 parent f8164b3 commit c4e4586
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <QDebug>
#include <QCoreApplication>
#include <QProcess>
#include <QMessageBox>

// ExecInExplorer.cpp
HRESULT ShellExecInExplorerProcess(PCWSTR pszFile, PCWSTR pszArgs);
Expand All @@ -57,6 +58,22 @@ void setRegistryKey(HKEY root,
}
}

bool runningAsAdmin() {
switch (Settings().testWrite()) {
case QSettings::NoError:
return true;
case QSettings::AccessError:
return false;
case QSettings::FormatError:
break;
}

QMessageBox errorMessageBox;
errorMessageBox.setText("Windows registry not working");
errorMessageBox.exec();
exit(1);
}

// Create a Shortcut. Code from https://msdn.microsoft.com/en-us/library/bb776891(VS.85).aspx
bool CreateLink(const QString& sourcePath, const QString& workingDir,
const QString& linkPath, QString& linkName)
Expand Down Expand Up @@ -297,7 +314,7 @@ QString getGameCommand(const QString& installPath)

bool startGame(const QString& commandLine)
{
if (Settings().testWrite() == QSettings::AccessError) {
if (!runningAsAdmin()) {
qDebug() << "not admin, start game normally";
return QProcess::startDetached(commandLine);
}
Expand All @@ -318,7 +335,7 @@ bool startGame(const QString& commandLine)
// RelaunchElevated is skipped when --update-updater-to or --update-game is used in order to avoid this.
ElevationResult RelaunchElevated(const QString& flags)
{
if (Settings().testWrite() == QSettings::NoError) {
if (runningAsAdmin()) {
qDebug() << "Process already has administrator privileges";
return ElevationResult::UNNEEDED;
}
Expand Down

0 comments on commit c4e4586

Please sign in to comment.