]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dlmsw.cpp
Rework the wxCusor ctor taking wx stock number to provide as many cursors as
[wxWidgets.git] / src / msw / dlmsw.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/dlmsw.cpp
3// Purpose: Win32-specific part of wxDynamicLibrary and related classes
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 2005-01-10 (partly extracted from common/dynlib.cpp)
7// RCS-ID: $Id$
8// Copyright: (c) 1998-2005 Vadim Zeitlin <vadim@wxwindows.org>
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_DYNLIB_CLASS
27
28#include "wx/msw/private.h"
29#include "wx/msw/debughlp.h"
30
31const wxString wxDynamicLibrary::ms_dllext(_T(".dll"));
32
33// ----------------------------------------------------------------------------
34// private classes
35// ----------------------------------------------------------------------------
36
37// wrap some functions from version.dll: load them dynamically and provide a
38// clean interface
39class wxVersionDLL
40{
41public:
42 // load version.dll and bind to its functions
43 wxVersionDLL();
44
45 // return the file version as string, e.g. "x.y.z.w"
46 wxString GetFileVersion(const wxString& filename) const;
47
48private:
49 typedef DWORD (APIENTRY *GetFileVersionInfoSize_t)(PTSTR, PDWORD);
50 typedef BOOL (APIENTRY *GetFileVersionInfo_t)(PTSTR, DWORD, DWORD, PVOID);
51 typedef BOOL (APIENTRY *VerQueryValue_t)(const PVOID, PTSTR, PVOID *, PUINT);
52
53 #define DO_FOR_ALL_VER_FUNCS(what) \
54 what(GetFileVersionInfoSize); \
55 what(GetFileVersionInfo); \
56 what(VerQueryValue)
57
58 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
59
60 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION);
61
62 #undef DECLARE_VER_FUNCTION
63
64
65 wxDynamicLibrary m_dll;
66
67
68 DECLARE_NO_COPY_CLASS(wxVersionDLL)
69};
70
71// class used to create wxDynamicLibraryDetails objects
72class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
73{
74public:
75 // type of parameters being passed to EnumModulesProc
76 struct EnumModulesProcParams
77 {
78 wxDynamicLibraryDetailsArray *dlls;
79 wxVersionDLL *verDLL;
80 };
81
82 // the declared type of the first EnumModulesProc() parameter changed in
83 // recent SDK versions and is no PCSTR instead of old PSTR, we know that
84 // it's const in version 11 and non-const in version 8 included with VC8
85 // (and earlier), suppose that it's only changed in version 11
86 #if defined(API_VERSION_NUMBER) && API_VERSION_NUMBER >= 11
87 typedef PCSTR NameStr_t;
88 #else
89 typedef PSTR NameStr_t;
90 #endif
91
92 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
93 #ifdef __WIN64__
94 typedef DWORD64 DWORD_32_64;
95 #else
96 typedef DWORD DWORD_32_64;
97 #endif
98
99 static BOOL CALLBACK
100 EnumModulesProc(NameStr_t name, DWORD_32_64 base, ULONG size, void *data);
101};
102
103// ----------------------------------------------------------------------------
104// private functions
105// ----------------------------------------------------------------------------
106
107// return the module handle for the given base name
108static
109HMODULE wxGetModuleHandle(const char *name, void *addr)
110{
111 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
112 // because the former works correctly for comctl32.dll while the latter
113 // returns NULL when comctl32.dll version 6 is used under XP (note that
114 // GetModuleHandleEx() is only available under XP and later, coincidence?)
115
116 // check if we can use GetModuleHandleEx
117 typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCSTR, HMODULE *);
118
119 static const GetModuleHandleEx_t INVALID_FUNC_PTR = (GetModuleHandleEx_t)-1;
120
121 static GetModuleHandleEx_t s_pfnGetModuleHandleEx = INVALID_FUNC_PTR;
122 if ( s_pfnGetModuleHandleEx == INVALID_FUNC_PTR )
123 {
124 wxDynamicLibrary dll(_T("kernel32.dll"), wxDL_VERBATIM);
125 s_pfnGetModuleHandleEx =
126 (GetModuleHandleEx_t)dll.RawGetSymbol(_T("GetModuleHandleExA"));
127
128 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
129 }
130
131 // get module handle from its address
132 if ( s_pfnGetModuleHandleEx )
133 {
134 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
135 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
136 HMODULE hmod;
137 if ( s_pfnGetModuleHandleEx(6, (char *)addr, &hmod) && hmod )
138 return hmod;
139 }
140
141 // Windows CE only has Unicode API, so even we have an ANSI string here, we
142 // still need to use GetModuleHandleW() there and so do it everywhere to
143 // avoid #ifdefs -- this code is not performance-critical anyhow...
144#ifdef __WINCE__
145 return ::GetModuleHandleW(wxConvLibc.cMB2WC(name).data());
146#else
147 return ::GetModuleHandleA((char *)name);
148#endif
149}
150
151// ============================================================================
152// wxVersionDLL implementation
153// ============================================================================
154
155// ----------------------------------------------------------------------------
156// loading
157// ----------------------------------------------------------------------------
158
159wxVersionDLL::wxVersionDLL()
160{
161 // don't give errors if DLL can't be loaded or used, we're prepared to
162 // handle it
163 wxLogNull noLog;
164
165 if ( m_dll.Load(_T("version.dll"), wxDL_VERBATIM) )
166 {
167 // the functions we load have either 'A' or 'W' suffix depending on
168 // whether we're in ANSI or Unicode build
169 #ifdef UNICODE
170 #define SUFFIX L"W"
171 #else // ANSI
172 #define SUFFIX "A"
173 #endif // UNICODE/ANSI
174
175 #define LOAD_VER_FUNCTION(name) \
176 m_pfn ## name = (name ## _t)m_dll.GetSymbol(_T(#name SUFFIX)); \
177 if ( !m_pfn ## name ) \
178 { \
179 m_dll.Unload(); \
180 return; \
181 }
182
183 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION);
184
185 #undef LOAD_VER_FUNCTION
186 }
187}
188
189// ----------------------------------------------------------------------------
190// wxVersionDLL operations
191// ----------------------------------------------------------------------------
192
193wxString wxVersionDLL::GetFileVersion(const wxString& filename) const
194{
195 wxString ver;
196 if ( m_dll.IsLoaded() )
197 {
198 wxChar *pc = wx_const_cast(wxChar *, filename.wx_str());
199
200 DWORD dummy;
201 DWORD sizeVerInfo = m_pfnGetFileVersionInfoSize(pc, &dummy);
202 if ( sizeVerInfo )
203 {
204 wxCharBuffer buf(sizeVerInfo);
205 if ( m_pfnGetFileVersionInfo(pc, 0, sizeVerInfo, buf.data()) )
206 {
207 void *pVer;
208 UINT sizeInfo;
209 if ( m_pfnVerQueryValue(buf.data(), _T("\\"), &pVer, &sizeInfo) )
210 {
211 VS_FIXEDFILEINFO *info = (VS_FIXEDFILEINFO *)pVer;
212 ver.Printf(_T("%d.%d.%d.%d"),
213 HIWORD(info->dwFileVersionMS),
214 LOWORD(info->dwFileVersionMS),
215 HIWORD(info->dwFileVersionLS),
216 LOWORD(info->dwFileVersionLS));
217 }
218 }
219 }
220 }
221 //else: we failed to load DLL, can't retrieve version info
222
223 return ver;
224}
225
226// ============================================================================
227// wxDynamicLibraryDetailsCreator implementation
228// ============================================================================
229
230/* static */
231BOOL CALLBACK
232wxDynamicLibraryDetailsCreator::EnumModulesProc(NameStr_t name,
233 DWORD_32_64 base,
234 ULONG size,
235 void *data)
236{
237 EnumModulesProcParams *params = (EnumModulesProcParams *)data;
238
239 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
240
241 // fill in simple properties
242 details->m_name = wxString::FromAscii(name);
243 details->m_address = wx_reinterpret_cast(void *, base);
244 details->m_length = size;
245
246 // to get the version, we first need the full path
247 HMODULE hmod = wxGetModuleHandle(name, (void *)base);
248 if ( hmod )
249 {
250 wxString fullname = wxGetFullModuleName(hmod);
251 if ( !fullname.empty() )
252 {
253 details->m_path = fullname;
254 details->m_version = params->verDLL->GetFileVersion(fullname);
255 }
256 }
257
258 params->dlls->Add(details);
259
260 // continue enumeration (returning FALSE would have stopped it)
261 return TRUE;
262}
263
264// ============================================================================
265// wxDynamicLibrary implementation
266// ============================================================================
267
268// ----------------------------------------------------------------------------
269// misc functions
270// ----------------------------------------------------------------------------
271
272wxDllType wxDynamicLibrary::GetProgramHandle()
273{
274 return (wxDllType)::GetModuleHandle(NULL);
275}
276
277// ----------------------------------------------------------------------------
278// loading/unloading DLLs
279// ----------------------------------------------------------------------------
280
281/* static */
282wxDllType
283wxDynamicLibrary::RawLoad(const wxString& libname, int WXUNUSED(flags))
284{
285 return ::LoadLibrary(libname.wx_str());
286}
287
288/* static */
289void wxDynamicLibrary::Unload(wxDllType handle)
290{
291 ::FreeLibrary(handle);
292}
293
294/* static */
295void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
296{
297 return (void *)::GetProcAddress(handle,
298#ifdef __WXWINCE__
299 name.c_str()
300#else
301 name.ToAscii()
302#endif // __WXWINCE__
303 );
304}
305
306// ----------------------------------------------------------------------------
307// enumerating loaded DLLs
308// ----------------------------------------------------------------------------
309
310/* static */
311wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
312{
313 wxDynamicLibraryDetailsArray dlls;
314
315#if wxUSE_DBGHELP
316 if ( wxDbgHelpDLL::Init() )
317 {
318 // prepare to use functions for version info extraction
319 wxVersionDLL verDLL;
320
321 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params;
322 params.dlls = &dlls;
323 params.verDLL = &verDLL;
324
325 if ( !wxDbgHelpDLL::EnumerateLoadedModules
326 (
327 ::GetCurrentProcess(),
328 wxDynamicLibraryDetailsCreator::EnumModulesProc,
329 &params
330 ) )
331 {
332 wxLogLastError(_T("EnumerateLoadedModules"));
333 }
334 }
335#endif // wxUSE_DBGHELP
336
337 return dlls;
338}
339
340#endif // wxUSE_DYNLIB_CLASS
341