2015-11-22 21:45:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MuldeR's Utilities for Qt
|
|
|
|
// Copyright (C) 2004-2015 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
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/lgpl-2.1.txt
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Utils_Win32.h"
|
|
|
|
|
|
|
|
//Win32 API
|
|
|
|
#ifndef _INC_WINDOWS
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif //_INC_WINDOWS
|
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QReadWriteLock>
|
|
|
|
#include <QLibrary>
|
|
|
|
#include <QHash>
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// QICON TO HICON
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon &icon, const int w, const int h)
|
|
|
|
{
|
|
|
|
if(!icon.isNull())
|
|
|
|
{
|
|
|
|
QPixmap pixmap = icon.pixmap(w, h);
|
|
|
|
if(!pixmap.isNull())
|
|
|
|
{
|
|
|
|
return (uintptr_t) pixmap.toWinHICON();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RESOLVE FUNCTION
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
typedef QHash<QString, uintptr_t> FunctionMap;
|
|
|
|
typedef QPair<QSharedPointer<QLibrary>, FunctionMap> LibraryItem;
|
|
|
|
|
|
|
|
static QReadWriteLock g_resolve_lock;
|
|
|
|
static QHash<QString, LibraryItem> g_resolve_libs;
|
|
|
|
|
2015-11-25 20:50:48 +01:00
|
|
|
const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
|
2015-11-22 21:45:09 +01:00
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
|
|
|
|
const QString functionIdTrimmed = functionName.trimmed();
|
2015-11-22 21:45:09 +01:00
|
|
|
|
|
|
|
//Fuction already loaded?
|
2015-11-23 22:28:52 +01:00
|
|
|
QReadLocker rdLock(&g_resolve_lock);
|
|
|
|
if (g_resolve_libs.contains(libraryNameFolded))
|
2015-11-22 21:45:09 +01:00
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
LibraryItem &lib = g_resolve_libs[libraryNameFolded];
|
|
|
|
if (lib.second.contains(functionIdTrimmed))
|
2015-11-22 21:45:09 +01:00
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
return lib.second[functionIdTrimmed];
|
2015-11-22 21:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Accquire write access!
|
|
|
|
rdLock.unlock();
|
|
|
|
QWriteLocker wrLock(&g_resolve_lock);
|
|
|
|
|
|
|
|
//Load library
|
2015-11-23 22:28:52 +01:00
|
|
|
if (!g_resolve_libs.contains(libraryNameFolded))
|
2015-11-22 21:45:09 +01:00
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
|
2015-11-22 21:45:09 +01:00
|
|
|
if (!(lib->isLoaded() || lib->load()))
|
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
|
|
|
|
lib.clear();
|
2015-11-22 21:45:09 +01:00
|
|
|
}
|
2015-11-23 22:28:52 +01:00
|
|
|
g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Is library available?
|
|
|
|
LibraryItem &lib = g_resolve_libs[libraryNameFolded];
|
|
|
|
if (lib.first.isNull() || (!lib.first->isLoaded()))
|
|
|
|
{
|
2015-11-25 20:50:48 +01:00
|
|
|
static const uintptr_t null = NULL;
|
|
|
|
return null; /*library unavailable*/
|
2015-11-22 21:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Lookup the function
|
2015-11-23 22:28:52 +01:00
|
|
|
if (!lib.second.contains(functionIdTrimmed))
|
2015-11-22 21:45:09 +01:00
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
|
2015-11-22 21:45:09 +01:00
|
|
|
if (!ptr)
|
|
|
|
{
|
2015-11-23 22:28:52 +01:00
|
|
|
qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
|
2015-11-22 21:45:09 +01:00
|
|
|
}
|
2015-11-23 22:28:52 +01:00
|
|
|
lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
|
2015-11-22 21:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Return function pointer
|
2015-11-23 22:28:52 +01:00
|
|
|
return lib.second[functionIdTrimmed];
|
2015-11-22 21:45:09 +01:00
|
|
|
}
|