Replace some not appropriate QSystemLibrary usages

For these use cases we consider it's more appropriate to
use GetModuleHandle rather than QSystemLibrary.

Change-Id: I30f54e3dbe42d96292b6350a26c84fdb97e56ea4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Zhao Yuhang 2025-03-21 14:34:06 +08:00
parent 762c1561a8
commit 09991b51a4
4 changed files with 17 additions and 65 deletions

View File

@ -596,6 +596,7 @@ qt_internal_extend_target(Core CONDITION WIN32
authz
kernel32
netapi32
ntdll
ole32
shell32
user32

View File

@ -17,6 +17,7 @@
#include <QtCore/private/qglobal_p.h>
#include <QtCore/qt_windows.h>
#include <winternl.h>
QT_BEGIN_NAMESPACE

View File

@ -10,10 +10,12 @@
#include <QtCore/private/wcharhelpers_win_p.h>
#include "qfilesystementry_p.h"
#include "private/qsystemlibrary_p.h"
#include "qntdll_p.h"
extern "C" NTSTATUS NTSYSCALLAPI NtQueryVolumeInformationFile(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG,
FS_INFORMATION_CLASS);
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@ -208,59 +210,8 @@ bool QStorageInfoPrivate::queryStorageProperty()
return result;
}
struct Helper
{
QBasicMutex mutex;
QSystemLibrary ntdll {u"ntdll"_s};
};
Q_GLOBAL_STATIC(Helper, gNtdllHelper)
inline QFunctionPointer resolveSymbol(QSystemLibrary *ntdll, const char *name)
{
QFunctionPointer symbolFunctionPointer = ntdll->resolve(name);
if (Q_UNLIKELY(!symbolFunctionPointer))
qWarning("Failed to resolve the symbol: %s", name);
return symbolFunctionPointer;
}
#define GENERATE_SYMBOL(symbolName, returnType, ...) \
using Qt##symbolName = returnType (NTAPI *) (__VA_ARGS__); \
static Qt##symbolName qt##symbolName = nullptr;
#define RESOLVE_SYMBOL(name) \
do { \
qt##name = reinterpret_cast<Qt##name>(resolveSymbol(ntdll, #name)); \
if (!qt##name) \
return false; \
} while (false)
GENERATE_SYMBOL(RtlInitUnicodeString, void, PUNICODE_STRING, PCWSTR);
GENERATE_SYMBOL(NtCreateFile, NTSTATUS, PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES,
PIO_STATUS_BLOCK, PLARGE_INTEGER, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG);
GENERATE_SYMBOL(NtQueryVolumeInformationFile, NTSTATUS, HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG,
FS_INFORMATION_CLASS);
void QStorageInfoPrivate::queryFileFsSectorSizeInformation()
{
static bool symbolsResolved = [](auto ntdllHelper) {
QMutexLocker locker(&ntdllHelper->mutex);
auto ntdll = &ntdllHelper->ntdll;
if (!ntdll->isLoaded()) {
if (!ntdll->load()) {
qWarning("Unable to load ntdll.dll.");
return false;
}
}
RESOLVE_SYMBOL(RtlInitUnicodeString);
RESOLVE_SYMBOL(NtCreateFile);
RESOLVE_SYMBOL(NtQueryVolumeInformationFile);
return true;
}(gNtdllHelper());
if (!symbolsResolved)
return;
FILE_FS_SECTOR_SIZE_INFORMATION ffssi;
memset(&ffssi, 0, sizeof(ffssi));
@ -277,11 +228,11 @@ void QStorageInfoPrivate::queryFileFsSectorSizeInformation()
path.append(u'\\');
UNICODE_STRING name;
qtRtlInitUnicodeString(&name, qt_castToWchar(path));
::RtlInitUnicodeString(&name, qt_castToWchar(path));
InitializeObjectAttributes(&attrs, &name, 0, nullptr, nullptr);
NTSTATUS status = qtNtCreateFile(&handle,
NTSTATUS status = ::NtCreateFile(&handle,
FILE_READ_ATTRIBUTES,
&attrs,
&isb,
@ -296,7 +247,7 @@ void QStorageInfoPrivate::queryFileFsSectorSizeInformation()
return;
memset(&isb, 0, sizeof(isb));
status = qtNtQueryVolumeInformationFile(handle,
status = ::NtQueryVolumeInformationFile(handle,
&isb,
&ffssi,
sizeof(ffssi),

View File

@ -39,7 +39,6 @@
#include <QtGui/private/qabstractfileiconengine_p.h>
#include <QtGui/private/qwindowsfontdatabase_p.h>
#include <private/qhighdpiscaling_p.h>
#include <private/qsystemlibrary_p.h>
#include <private/qwinregistry_p.h>
#include <QtCore/private/qfunctions_win_p.h>
@ -806,15 +805,15 @@ Q_GUI_EXPORT QPixmap qt_pixmapFromWinHICON(HICON icon);
static QPixmap loadIconFromShell32(int resourceId, QSizeF size)
{
if (const HMODULE hmod = QSystemLibrary::load(L"shell32")) {
auto iconHandle =
static_cast<HICON>(LoadImage(hmod, MAKEINTRESOURCE(resourceId),
IMAGE_ICON, int(size.width()), int(size.height()), 0));
if (iconHandle) {
QPixmap iconpixmap = qt_pixmapFromWinHICON(iconHandle);
DestroyIcon(iconHandle);
return iconpixmap;
}
HMODULE shell32 = ::GetModuleHandleW(L"shell32.dll");
Q_ASSERT(shell32);
auto iconHandle =
static_cast<HICON>(LoadImage(shell32, MAKEINTRESOURCE(resourceId),
IMAGE_ICON, int(size.width()), int(size.height()), 0));
if (iconHandle) {
QPixmap iconpixmap = qt_pixmapFromWinHICON(iconHandle);
DestroyIcon(iconHandle);
return iconpixmap;
}
return QPixmap();
}