1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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"
26 #define WIN32_LEAN_AND_MEAN 1
33 #include <QReadWriteLock>
37 ///////////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////////
41 uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon
&icon
, const int w
, const int h
)
45 QPixmap pixmap
= icon
.pixmap(w
, h
);
48 return (uintptr_t) pixmap
.toWinHICON();
54 ///////////////////////////////////////////////////////////////////////////////
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!
82 QWriteLocker
wrLock(&g_resolve_lock
);
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
));
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());
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
];