1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 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
28 #include <ObjIdl.h> // required by QWinMime in QtWinExtras
34 #include <QReadWriteLock>
37 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
38 #include <QtWinExtras>
42 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
43 #define PIXMAP2HICON(X) QtWin::toHICON((X))
45 #define PIXMAP2HICON(X) (X).toWinHICON()
48 ///////////////////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////////////////
52 uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon
*const icon
, const int w
, const int h
)
56 QPixmap pixmap
= icon
->pixmap(w
, h
);
59 return (uintptr_t) PIXMAP2HICON(pixmap
);
65 ///////////////////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////////////////
69 typedef QHash
<QString
, uintptr_t> FunctionMap
;
70 typedef QPair
<QSharedPointer
<QLibrary
>, FunctionMap
> LibraryItem
;
72 static QReadWriteLock g_resolve_lock
;
73 static QHash
<QString
, LibraryItem
> g_resolve_libs
;
75 const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString
&libraryName
, const QString
&functionName
)
77 const QString libraryNameFolded
= libraryName
.toCaseFolded().trimmed();
78 const QString functionIdTrimmed
= functionName
.trimmed();
80 //Fuction already loaded?
81 QReadLocker
rdLock(&g_resolve_lock
);
82 if (g_resolve_libs
.contains(libraryNameFolded
))
84 LibraryItem
&lib
= g_resolve_libs
[libraryNameFolded
];
85 if (lib
.second
.contains(functionIdTrimmed
))
87 return lib
.second
[functionIdTrimmed
];
91 //Accquire write access!
93 QWriteLocker
wrLock(&g_resolve_lock
);
96 if (!g_resolve_libs
.contains(libraryNameFolded
))
98 QSharedPointer
<QLibrary
> lib(new QLibrary(libraryNameFolded
));
99 if (!(lib
->isLoaded() || lib
->load()))
101 qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded
));
104 g_resolve_libs
.insert(libraryNameFolded
, qMakePair(lib
, FunctionMap()));
107 //Is library available?
108 LibraryItem
&lib
= g_resolve_libs
[libraryNameFolded
];
109 if (lib
.first
.isNull() || (!lib
.first
->isLoaded()))
111 static const uintptr_t null
= NULL
;
112 return null
; /*library unavailable*/
115 //Lookup the function
116 if (!lib
.second
.contains(functionIdTrimmed
))
118 void *const ptr
= lib
.first
->resolve(functionIdTrimmed
.toLatin1().constData());
121 qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded
), MUTILS_UTF8(functionIdTrimmed
));
123 lib
.second
.insert(functionIdTrimmed
, reinterpret_cast<uintptr_t>(ptr
));
126 //Return function pointer
127 return lib
.second
[functionIdTrimmed
];