Skip to content

Commit 0e4edc8

Browse files
langlor-autodeskAndyShawQtkeithel-qt
authored and
GitHub Enterprise
committed
Windows: use QSystemLibrary instead of LoadLibrary directly (qt#77)
* Windows: use QSystemLibrary instead of LoadLibrary directly Using QSystemLibrary ensures that it will only use the expected copy of the system library and not one that has been placed in the application's working directory or elsewhere in the PATH environment variable. Pick-to: 5.15 6.2 6.3 Change-Id: Ic4234334f73482b38ee5f06345bf11f8c029edc5 Reviewed-by: Laszlo Agocs <[email protected]> Reviewed-by: Thiago Macieira <[email protected]> Reviewed-by: Qt CI Bot <[email protected]> * Windows: use QSystemLibrary instead of LoadLibrary directly (5.15) This is the rest of the patch that is specific to Qt 5.15. Using QSystemLibrary ensures that it will only use the expected copy of the system library and not one that has been placed in the application's working directory or elsewhere in the PATH environment variable. Change-Id: Ic559957f5ca2e53193a1c230577f877b09e90859 Co-authored-by: Andy Shaw <[email protected]> Co-authored-by: Keith Kyzivat <[email protected]>
1 parent a64e5f8 commit 0e4edc8

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/corelib/io/qlockfile_win.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include "QtCore/qdebug.h"
4949
#include "QtCore/qthread.h"
5050

51+
#include "private/qsystemlibrary_p.h"
52+
5153
QT_BEGIN_NAMESPACE
5254

5355
static inline bool fileExists(const wchar_t *fileName)
@@ -150,7 +152,7 @@ QString QLockFilePrivate::processNameByPid(qint64 pid)
150152
#if !defined(Q_OS_WINRT)
151153
typedef DWORD (WINAPI *GetModuleFileNameExFunc)(HANDLE, HMODULE, LPTSTR, DWORD);
152154

153-
HMODULE hPsapi = LoadLibraryA("psapi");
155+
HMODULE hPsapi = QSystemLibrary::load(L"psapi");
154156
if (!hPsapi)
155157
return QString();
156158
GetModuleFileNameExFunc qGetModuleFileNameEx = reinterpret_cast<GetModuleFileNameExFunc>(

src/network/kernel/qauthenticator.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <qstring.h>
5151
#include <qdatetime.h>
5252
#include <qrandom.h>
53+
#include "private/qsystemlibrary_p.h"
5354

5455
#ifdef Q_OS_WIN
5556
#include <qmutex.h>
@@ -1542,7 +1543,7 @@ static bool q_SSPI_library_load()
15421543

15431544
// Initialize security interface
15441545
if (pSecurityFunctionTable == nullptr) {
1545-
securityDLLHandle = LoadLibrary(L"secur32.dll");
1546+
securityDLLHandle = QSystemLibrary::load(L"secur32");
15461547
if (securityDLLHandle != nullptr) {
15471548
INIT_SECURITY_INTERFACE pInitSecurityInterface =
15481549
reinterpret_cast<INIT_SECURITY_INTERFACE>(

src/plugins/platforms/windows/qwindowsglcontext.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <qpa/qplatformnativeinterface.h>
4949
#include <QtPlatformHeaders/qwglnativecontext.h>
5050

51+
#include <private/qsystemlibrary_p.h>
5152
#include <algorithm>
5253

5354
#include <wingdi.h>
@@ -162,19 +163,25 @@ QFunctionPointer QWindowsOpengl32DLL::resolve(const char *name)
162163

163164
bool QWindowsOpengl32DLL::init(bool softwareRendering)
164165
{
165-
const QByteArray opengl32 = QByteArrayLiteral("opengl32.dll");
166-
const QByteArray swopengl = QByteArrayLiteral("opengl32sw.dll");
166+
const QByteArray opengl32 = QByteArrayLiteral("opengl32");
167+
const QByteArray swopengl = QByteArrayLiteral("opengl32sw");
168+
bool useSystemLib = false;
167169

168170
QByteArray openglDll = qgetenv("QT_OPENGL_DLL");
169-
if (openglDll.isEmpty())
171+
if (openglDll.isEmpty()) {
170172
openglDll = softwareRendering ? swopengl : opengl32;
173+
useSystemLib = !softwareRendering;
174+
}
171175

172176
openglDll = openglDll.toLower();
173177
m_nonOpengl32 = openglDll != opengl32;
174178

175179
qCDebug(lcQpaGl) << "Qt: Using WGL and OpenGL from" << openglDll;
176180

177-
m_lib = ::LoadLibraryA(openglDll.constData());
181+
if (useSystemLib)
182+
m_lib = QSystemLibrary::load((wchar_t*)(QString::fromLatin1(openglDll).utf16()));
183+
else
184+
m_lib = LoadLibraryA(openglDll.constData());
178185
if (!m_lib) {
179186
qErrnoWarning(::GetLastError(), "Failed to load %s", openglDll.constData());
180187
return false;
@@ -184,7 +191,7 @@ bool QWindowsOpengl32DLL::init(bool softwareRendering)
184191
// Load opengl32.dll always. GDI functions like ChoosePixelFormat do
185192
// GetModuleHandle for opengl32.dll and behave differently (and call back into
186193
// opengl32) when the module is present. This is fine for dummy contexts and windows.
187-
::LoadLibraryA("opengl32.dll");
194+
QSystemLibrary::load(L"opengl32");
188195
}
189196

190197
wglCreateContext = reinterpret_cast<HGLRC (WINAPI *)(HDC)>(resolve("wglCreateContext"));

src/plugins/platforms/windows/qwindowsopengltester.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <QtCore/qstandardpaths.h>
5050
#include <QtCore/qlibraryinfo.h>
5151
#include <QtCore/qhash.h>
52+
#include <private/qsystemlibrary_p.h>
5253

5354
#ifndef QT_NO_OPENGL
5455
#include <private/qopengl_p.h>
@@ -396,7 +397,7 @@ bool QWindowsOpenGLTester::testDesktopGL()
396397

397398
// Test #1: Load opengl32.dll and try to resolve an OpenGL 2 function.
398399
// This will typically fail on systems that do not have a real OpenGL driver.
399-
lib = LoadLibraryA("opengl32.dll");
400+
lib = QSystemLibrary::load(L"opengl32");
400401
if (lib) {
401402
CreateContext = reinterpret_cast<CreateContextType>(
402403
reinterpret_cast<QFunctionPointer>(::GetProcAddress(lib, "wglCreateContext")));

0 commit comments

Comments
 (0)