Added missing linebreak.
[MUtilities.git] / src / Utils_Win32.cpp
blob7be27575022464802c585f4f069d7612bfc7d589
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 #include "Utils_Win32.h"
24 //Win32 API
25 #ifndef _INC_WINDOWS
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <Windows.h>
28 #endif //_INC_WINDOWS
30 //Qt
31 #include <QIcon>
32 #include <QPair>
33 #include <QReadWriteLock>
34 #include <QLibrary>
35 #include <QHash>
37 ///////////////////////////////////////////////////////////////////////////////
38 // QICON TO HICON
39 ///////////////////////////////////////////////////////////////////////////////
41 uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon &icon, const int w, const int h)
43 if(!icon.isNull())
45 QPixmap pixmap = icon.pixmap(w, h);
46 if(!pixmap.isNull())
48 return (uintptr_t) pixmap.toWinHICON();
51 return NULL;
54 ///////////////////////////////////////////////////////////////////////////////
55 // RESOLVE FUNCTION
56 ///////////////////////////////////////////////////////////////////////////////
58 typedef QHash<QString, uintptr_t> FunctionMap;
59 typedef QPair<QSharedPointer<QLibrary>, FunctionMap> LibraryItem;
61 static QReadWriteLock g_resolve_lock;
62 static QHash<QString, LibraryItem> g_resolve_libs;
64 const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
66 const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
67 const QString functionIdTrimmed = functionName.trimmed();
69 //Fuction already loaded?
70 QReadLocker rdLock(&g_resolve_lock);
71 if (g_resolve_libs.contains(libraryNameFolded))
73 LibraryItem &lib = g_resolve_libs[libraryNameFolded];
74 if (lib.second.contains(functionIdTrimmed))
76 return lib.second[functionIdTrimmed];
80 //Accquire write access!
81 rdLock.unlock();
82 QWriteLocker wrLock(&g_resolve_lock);
84 //Load library
85 if (!g_resolve_libs.contains(libraryNameFolded))
87 QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
88 if (!(lib->isLoaded() || lib->load()))
90 qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
91 lib.clear();
93 g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
96 //Is library available?
97 LibraryItem &lib = g_resolve_libs[libraryNameFolded];
98 if (lib.first.isNull() || (!lib.first->isLoaded()))
100 static const uintptr_t null = NULL;
101 return null; /*library unavailable*/
104 //Lookup the function
105 if (!lib.second.contains(functionIdTrimmed))
107 void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
108 if (!ptr)
110 qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
112 lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
115 //Return function pointer
116 return lib.second[functionIdTrimmed];