From 993c3fc35b9a7d7c1f51cebe71444661fd15b41c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 16 Jan 2025 19:54:05 -0800 Subject: [PATCH] QCoreApplication/Win: add a better way for ActiveQt to override qAppFileName Right now, the DLLMain in qaxserverdll.cpp calls GetModuleFileName() on the hInstance for the DLL and passes the path to QCoreApplication to override the path. This adds a way for it to simply pass the hInstance handle, which will automatically cause qAppFileName() & QCoreApplication::applicationFilePath() return the DLL's path, which in turn influences the default QCoreApplication::libraryPaths(). Change-Id: I3cc172288c54c3938566fffdfb62985000ce2e9c Reviewed-by: Oliver Wolff (cherry picked from commit 507024317fec0c1deeb62c8c2ba5a43af7d65f21) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qcoreapplication_p.h | 1 + src/corelib/kernel/qcoreapplication_win.cpp | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h index e676500e8aa..eee4918737e 100644 --- a/src/corelib/kernel/qcoreapplication_p.h +++ b/src/corelib/kernel/qcoreapplication_p.h @@ -132,6 +132,7 @@ public: std::unique_ptr origArgv; bool consoleAllocated = false; + static void *mainInstanceHandle; // HINSTANCE without #endif void appendApplicationPathToLibraryPaths(void); diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp index 9d146afa713..9011e3fe9e6 100644 --- a/src/corelib/kernel/qcoreapplication_win.cpp +++ b/src/corelib/kernel/qcoreapplication_win.cpp @@ -20,6 +20,9 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; +// By default, we get the path to the host .exe. ActiveQt can override this +// with the component's DLL. +Q_CONSTINIT void *QCoreApplicationPrivate::mainInstanceHandle = nullptr; QString qAppFileName() // get application file name { /* @@ -45,7 +48,8 @@ QString qAppFileName() // get application file name do { size += MAX_PATH; space.resize(int(size)); - v = GetModuleFileName(NULL, space.data(), DWORD(space.size())); + auto hInstance = reinterpret_cast(QCoreApplicationPrivate::mainInstanceHandle); + v = GetModuleFileName(hInstance, space.data(), DWORD(space.size())); } while (Q_UNLIKELY(v >= size)); return QString::fromWCharArray(space.data(), v);