From 4c2af150a6d9638f76356799d4074a3aa956c444 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 27 Jul 2023 23:15:22 +0800 Subject: [PATCH] fix: use win32api to get application location --- app/settings.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/settings.cpp b/app/settings.cpp index c296c6b..b1233e6 100644 --- a/app/settings.cpp +++ b/app/settings.cpp @@ -105,19 +105,37 @@ void Settings::setHiDpiScaleFactorBehavior(Qt::HighDpiScaleFactorRoundingPolicy m_qsettings->sync(); } +#if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN) +#include +// QCoreApplication::applicationDirPath() parses the "applicationDirPath" from arg0, which... +// 1. rely on a QApplication object instance +// but we need to call QGuiApplication::setHighDpiScaleFactorRoundingPolicy() before QApplication get created +// 2. arg0 is NOT garanteed to be the path of execution +// see also: https://stackoverflow.com/questions/383973/is-args0-guaranteed-to-be-the-path-of-execution +// This function is here mainly for #1. +QString getApplicationDirPath() +{ + WCHAR buffer[MAX_PATH]; + GetModuleFileNameW(NULL, buffer, MAX_PATH); + QString appPath = QString::fromWCharArray(buffer); + + return appPath.left(appPath.lastIndexOf('\\')); +} +#endif // defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN) + Settings::Settings() : QObject(qApp) { QString configPath; -#ifdef FLAG_PORTABLE_MODE_SUPPORT - QString portableConfigDirPath = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath("data"); +#if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN) + QString portableConfigDirPath = QDir(getApplicationDirPath()).absoluteFilePath("data"); QFileInfo portableConfigDirInfo(portableConfigDirPath); if (portableConfigDirInfo.exists() && portableConfigDirInfo.isDir() && portableConfigDirInfo.isWritable()) { // we can use it. configPath = portableConfigDirPath; } -#endif // FLAG_PORTABLE_MODE_SUPPORT +#endif // defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN) if (configPath.isEmpty()) { // %LOCALAPPDATA%\ under Windows, ~/.config/ under Linux.