///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
-// Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
#ifndef _INC_WINDOWS
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
+#include <ObjIdl.h> // required by QWinMime in QtWinExtras
#endif //_INC_WINDOWS
//Qt
#include <QReadWriteLock>
#include <QLibrary>
#include <QHash>
+#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
+#include <QtWinExtras>
+#endif
+
+//Qt5 support
+#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
+#define PIXMAP2HICON(X) QtWin::toHICON((X))
+#else
+#define PIXMAP2HICON(X) (X).toWinHICON()
+#endif
///////////////////////////////////////////////////////////////////////////////
// QICON TO HICON
///////////////////////////////////////////////////////////////////////////////
-uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon &icon, const int w, const int h)
+uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon *const icon, const int w, const int h)
{
- if(!icon.isNull())
+ if(!icon->isNull())
{
- QPixmap pixmap = icon.pixmap(w, h);
+ QPixmap pixmap = icon->pixmap(w, h);
if(!pixmap.isNull())
{
- return (uintptr_t) pixmap.toWinHICON();
+ return (uintptr_t) PIXMAP2HICON(pixmap);
}
}
return NULL;
static QReadWriteLock g_resolve_lock;
static QHash<QString, LibraryItem> g_resolve_libs;
-uintptr_t MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
+const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
{
- QReadLocker rdLock(&g_resolve_lock);
+ const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
+ const QString functionIdTrimmed = functionName.trimmed();
//Fuction already loaded?
- const QString libNameLower = libraryName.toLower();
- if (g_resolve_libs.contains(libNameLower))
+ QReadLocker rdLock(&g_resolve_lock);
+ if (g_resolve_libs.contains(libraryNameFolded))
{
- LibraryItem &lib = g_resolve_libs[libNameLower];
- if (lib.second.contains(functionName))
+ LibraryItem &lib = g_resolve_libs[libraryNameFolded];
+ if (lib.second.contains(functionIdTrimmed))
{
- qWarning("TEST: Function already there!");
- return lib.second[functionName];
+ return lib.second[functionIdTrimmed];
}
}
QWriteLocker wrLock(&g_resolve_lock);
//Load library
- while (!g_resolve_libs.contains(libNameLower))
+ if (!g_resolve_libs.contains(libraryNameFolded))
{
- qWarning("TEST: Library not there -> going to load now!");
- QSharedPointer<QLibrary> lib(new QLibrary(libNameLower));
+ QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
if (!(lib->isLoaded() || lib->load()))
{
- qWarning("Failed to load library: \"%s\"", MUTILS_UTF8(libNameLower));
- return NULL;
+ qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
+ lib.clear();
}
- g_resolve_libs.insert(libNameLower, qMakePair(lib, FunctionMap()));
+ g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
+ }
+
+ //Is library available?
+ LibraryItem &lib = g_resolve_libs[libraryNameFolded];
+ if (lib.first.isNull() || (!lib.first->isLoaded()))
+ {
+ static const uintptr_t null = NULL;
+ return null; /*library unavailable*/
}
//Lookup the function
- LibraryItem &lib = g_resolve_libs[libNameLower];
- while (!lib.second.contains(functionName))
+ if (!lib.second.contains(functionIdTrimmed))
{
- qWarning("TEST: Function not there -> going to resolve now!");
- void *const ptr = lib.first->resolve(functionName.toLatin1().constData());
+ void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
if (!ptr)
{
- lib.second.insert(functionName, NULL);
- qWarning("Failed to resolve function: \"%s\"", MUTILS_UTF8(functionName));
- return NULL;
+ qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
}
- qWarning("TEST: Function resolved to 0x%p", ptr);
- lib.second.insert(functionName, reinterpret_cast<uintptr_t>(ptr));
+ lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
}
//Return function pointer
- return lib.second[functionName];
+ return lib.second[functionIdTrimmed];
}