Implement QLibrary::PreventUnloadHint for Windows

It's interesting that the HMODULE/HINSTANCE pointer points to something
in the actual module that got loaded, so we can use its value as
"address of something in the module" for GetModuleHandleEx. The "PIN"
flag tells the Windows DLL loader to never unload.

Change-Id: Ifea6e497f11a461db432ffff1449a2169eb6293e
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
This commit is contained in:
Thiago Macieira 2016-04-28 14:09:32 -07:00
parent 04ca11e6f9
commit a63ca3fa10
2 changed files with 10 additions and 2 deletions

View File

@ -1096,8 +1096,6 @@ QString QLibrary::errorString() const
to the library \c shr_64.o in the archive file named \c libGL.a. This
is only supported on the AIX platform.
Setting PreventUnloadHint will only apply on Unix platforms.
The interpretation of the load hints is platform dependent, and if
you use it you are probably making some assumptions on which platform
you are compiling for, so use them only if you understand the consequences

View File

@ -148,6 +148,16 @@ bool QLibraryPrivate::load_sys()
else
qualifiedFileName = dir.filePath(moduleFileName);
#endif // !Q_OS_WINRT
if (loadHints() & QLibrary::PreventUnloadHint) {
// prevent the unloading of this component
HMODULE hmod;
bool ok = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_PIN |
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<const wchar_t *>(pHnd),
&hmod);
Q_ASSERT(!ok || hmod == pHnd);
}
}
return (pHnd != 0);
}