]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dlmsw.cpp
Warning fix.
[wxWidgets.git] / src / msw / dlmsw.cpp
CommitLineData
defbed48
VZ
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 wxChar *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 static BOOL CALLBACK
83 EnumModulesProc(PSTR name, DWORD base, ULONG size, void *data);
84};
85
86// ----------------------------------------------------------------------------
87// private functions
88// ----------------------------------------------------------------------------
89
90// return the module handle for the given base name
91static
92HMODULE wxGetModuleHandle(const char *name, void *addr)
93{
94 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
95 // because the former works correctly for comctl32.dll while the latter
96 // returns NULL when comctl32.dll version 6 is used under XP (note that
97 // GetModuleHandleEx() is only available under XP and later, coincidence?)
98
99 // check if we can use GetModuleHandleEx
100 typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCTSTR, HMODULE *);
101
102 static const GetModuleHandleEx_t INVALID_FUNC_PTR = (GetModuleHandleEx_t)-1;
103
104 static GetModuleHandleEx_t s_pfnGetModuleHandleEx = INVALID_FUNC_PTR;
105 if ( s_pfnGetModuleHandleEx == INVALID_FUNC_PTR )
106 {
107 wxDynamicLibrary dll(_T("kernel32.dll"), wxDL_VERBATIM);
108 s_pfnGetModuleHandleEx =
109 (GetModuleHandleEx_t)dll.RawGetSymbol(_T("GetModuleHandleExA"));
110
111 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
112 }
113
114 // get module handle from its address
115 if ( s_pfnGetModuleHandleEx )
116 {
117 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
118 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
119 HMODULE hmod;
94534bf8 120 if ( s_pfnGetModuleHandleEx(6, (LPCTSTR)addr, &hmod) && hmod )
defbed48
VZ
121 return hmod;
122 }
123
124 // if failed, try by name
125 return ::GetModuleHandleA(name);
126}
127
128// ============================================================================
129// wxVersionDLL implementation
130// ============================================================================
131
132// ----------------------------------------------------------------------------
133// loading
134// ----------------------------------------------------------------------------
135
136wxVersionDLL::wxVersionDLL()
137{
138 // don't give errors if DLL can't be loaded or used, we're prepared to
139 // handle it
140 wxLogNull noLog;
141
142 if ( m_dll.Load(_T("version.dll"), wxDL_VERBATIM) )
143 {
144 // the functions we load have either 'A' or 'W' suffix depending on
145 // whether we're in ANSI or Unicode build
146 #ifdef UNICODE
147 #define SUFFIX L"W"
148 #else // ANSI
149 #define SUFFIX "A"
150 #endif // UNICODE/ANSI
151
152 #define LOAD_VER_FUNCTION(name) \
153 m_pfn ## name = (name ## _t)m_dll.GetSymbol(_T(#name SUFFIX)); \
154 if ( !m_pfn ## name ) \
155 { \
156 m_dll.Unload(); \
157 return; \
158 }
159
160 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION);
161
162 #undef LOAD_VER_FUNCTION
163 }
164}
165
166// ----------------------------------------------------------------------------
167// wxVersionDLL operations
168// ----------------------------------------------------------------------------
169
170wxString wxVersionDLL::GetFileVersion(const wxString& filename) const
171{
172 wxString ver;
173 if ( m_dll.IsLoaded() )
174 {
175 wxChar *pc = wx_const_cast(wxChar *, filename.c_str());
176
177 DWORD dummy;
178 DWORD sizeVerInfo = m_pfnGetFileVersionInfoSize(pc, &dummy);
179 if ( sizeVerInfo )
180 {
181 wxCharBuffer buf(sizeVerInfo);
182 if ( m_pfnGetFileVersionInfo(pc, 0, sizeVerInfo, buf.data()) )
183 {
184 void *pVer;
185 UINT sizeInfo;
186 if ( m_pfnVerQueryValue(buf.data(), _T("\\"), &pVer, &sizeInfo) )
187 {
188 VS_FIXEDFILEINFO *info = (VS_FIXEDFILEINFO *)pVer;
189 ver.Printf(_T("%d.%d.%d.%d"),
190 HIWORD(info->dwFileVersionMS),
191 LOWORD(info->dwFileVersionMS),
192 HIWORD(info->dwFileVersionLS),
193 LOWORD(info->dwFileVersionLS));
194 }
195 }
196 }
197 }
198 //else: we failed to load DLL, can't retrieve version info
199
200 return ver;
201}
202
203// ============================================================================
204// wxDynamicLibraryDetailsCreator implementation
205// ============================================================================
206
207/* static */
208BOOL CALLBACK
209wxDynamicLibraryDetailsCreator::EnumModulesProc(PSTR name,
210 DWORD base,
211 ULONG size,
212 void *data)
213{
214 EnumModulesProcParams *params = (EnumModulesProcParams *)data;
215
216 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
217
218 // fill in simple properties
219 details->m_name = wxString::FromAscii(name);
220 details->m_address = wx_reinterpret_cast(void *, base);
221 details->m_length = size;
222
223 // to get the version, we first need the full path
224 HMODULE hmod = wxGetModuleHandle(name, (void *)base);
225 if ( hmod )
226 {
227 wxString fullname = wxGetFullModuleName(hmod);
228 if ( !fullname.empty() )
229 {
230 details->m_path = fullname;
231 details->m_version = params->verDLL->GetFileVersion(fullname);
232 }
233 }
234
235 params->dlls->Add(details);
236
237 // continue enumeration (returning FALSE would have stopped it)
238 return TRUE;
239}
240
241// ============================================================================
242// wxDynamicLibrary implementation
243// ============================================================================
244
245// ----------------------------------------------------------------------------
246// loading/unloading DLLs
247// ----------------------------------------------------------------------------
248
249/* static */
da55d064
VZ
250wxDllType
251wxDynamicLibrary::RawLoad(const wxString& libname, int WXUNUSED(flags))
defbed48
VZ
252{
253 return ::LoadLibrary(libname);
254}
255
256/* static */
257void wxDynamicLibrary::Unload(wxDllType handle)
258{
259 ::FreeLibrary(handle);
260}
261
262/* static */
263void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
264{
94534bf8 265 return ::GetProcAddress(handle, name.ToAscii());
defbed48
VZ
266}
267
268// ----------------------------------------------------------------------------
269// enumerating loaded DLLs
270// ----------------------------------------------------------------------------
271
272/* static */
273wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
274{
275 wxDynamicLibraryDetailsArray dlls;
276
94534bf8 277#if wxUSE_DBGHELP
defbed48
VZ
278 if ( wxDbgHelpDLL::Init() )
279 {
280 // prepare to use functions for version info extraction
281 wxVersionDLL verDLL;
282
283 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params;
284 params.dlls = &dlls;
285 params.verDLL = &verDLL;
286
287 if ( !wxDbgHelpDLL::EnumerateLoadedModules
288 (
289 ::GetCurrentProcess(),
290 wxDynamicLibraryDetailsCreator::EnumModulesProc,
291 &params
292 ) )
293 {
294 wxLogLastError(_T("EnumerateLoadedModules"));
295 }
296 }
94534bf8 297#endif // wxUSE_DBGHELP
defbed48
VZ
298
299 return dlls;
300}
301
302#endif // wxUSE_DYNLIB_CLASS
303