fix: use win32api to get application location

This commit is contained in:
Gary Wang 2023-07-27 23:15:22 +08:00
parent 9a9f61ae2f
commit 4c2af150a6
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760

View File

@ -105,19 +105,37 @@ void Settings::setHiDpiScaleFactorBehavior(Qt::HighDpiScaleFactorRoundingPolicy
m_qsettings->sync(); m_qsettings->sync();
} }
#if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN)
#include <windows.h>
// 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() Settings::Settings()
: QObject(qApp) : QObject(qApp)
{ {
QString configPath; QString configPath;
#ifdef FLAG_PORTABLE_MODE_SUPPORT #if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN)
QString portableConfigDirPath = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath("data"); QString portableConfigDirPath = QDir(getApplicationDirPath()).absoluteFilePath("data");
QFileInfo portableConfigDirInfo(portableConfigDirPath); QFileInfo portableConfigDirInfo(portableConfigDirPath);
if (portableConfigDirInfo.exists() && portableConfigDirInfo.isDir() && portableConfigDirInfo.isWritable()) { if (portableConfigDirInfo.exists() && portableConfigDirInfo.isDir() && portableConfigDirInfo.isWritable()) {
// we can use it. // we can use it.
configPath = portableConfigDirPath; configPath = portableConfigDirPath;
} }
#endif // FLAG_PORTABLE_MODE_SUPPORT #endif // defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN)
if (configPath.isEmpty()) { if (configPath.isEmpty()) {
// %LOCALAPPDATA%\<APPNAME> under Windows, ~/.config/<APPNAME> under Linux. // %LOCALAPPDATA%\<APPNAME> under Windows, ~/.config/<APPNAME> under Linux.