]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dlmsw.cpp
Don't use "Cancel" button in the about dialog of the listctrl sample.
[wxWidgets.git] / src / msw / dlmsw.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #include "wx/filename.h"
31
32 const wxString wxDynamicLibrary::ms_dllext(wxT(".dll"));
33
34 // ----------------------------------------------------------------------------
35 // private classes
36 // ----------------------------------------------------------------------------
37
38 // wrap some functions from version.dll: load them dynamically and provide a
39 // clean interface
40 class wxVersionDLL
41 {
42 public:
43 // load version.dll and bind to its functions
44 wxVersionDLL();
45
46 // return the file version as string, e.g. "x.y.z.w"
47 wxString GetFileVersion(const wxString& filename) const;
48
49 private:
50 typedef DWORD (APIENTRY *GetFileVersionInfoSize_t)(PTSTR, PDWORD);
51 typedef BOOL (APIENTRY *GetFileVersionInfo_t)(PTSTR, DWORD, DWORD, PVOID);
52 typedef BOOL (APIENTRY *VerQueryValue_t)(const PVOID, PTSTR, PVOID *, PUINT);
53
54 #define DO_FOR_ALL_VER_FUNCS(what) \
55 what(GetFileVersionInfoSize); \
56 what(GetFileVersionInfo); \
57 what(VerQueryValue)
58
59 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
60
61 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION);
62
63 #undef DECLARE_VER_FUNCTION
64
65
66 wxDynamicLibrary m_dll;
67
68
69 wxDECLARE_NO_COPY_CLASS(wxVersionDLL);
70 };
71
72 // class used to create wxDynamicLibraryDetails objects
73 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
74 {
75 public:
76 // type of parameters being passed to EnumModulesProc
77 struct EnumModulesProcParams
78 {
79 wxDynamicLibraryDetailsArray *dlls;
80 wxVersionDLL *verDLL;
81 };
82
83 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
84 #ifdef __WIN64__
85 typedef DWORD64 DWORD_32_64;
86 #else
87 typedef DWORD DWORD_32_64;
88 #endif
89
90 static BOOL CALLBACK
91 EnumModulesProc(PCSTR name, DWORD_32_64 base, ULONG size, void *data);
92 };
93
94 // ============================================================================
95 // wxVersionDLL implementation
96 // ============================================================================
97
98 // ----------------------------------------------------------------------------
99 // loading
100 // ----------------------------------------------------------------------------
101
102 wxVersionDLL::wxVersionDLL()
103 {
104 // don't give errors if DLL can't be loaded or used, we're prepared to
105 // handle it
106 wxLogNull noLog;
107
108 if ( m_dll.Load(wxT("version.dll"), wxDL_VERBATIM) )
109 {
110 // the functions we load have either 'A' or 'W' suffix depending on
111 // whether we're in ANSI or Unicode build
112 #ifdef UNICODE
113 #define SUFFIX L"W"
114 #else // ANSI
115 #define SUFFIX "A"
116 #endif // UNICODE/ANSI
117
118 #define LOAD_VER_FUNCTION(name) \
119 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
120 if ( !m_pfn ## name ) \
121 { \
122 m_dll.Unload(); \
123 return; \
124 }
125
126 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION);
127
128 #undef LOAD_VER_FUNCTION
129 }
130 }
131
132 // ----------------------------------------------------------------------------
133 // wxVersionDLL operations
134 // ----------------------------------------------------------------------------
135
136 wxString wxVersionDLL::GetFileVersion(const wxString& filename) const
137 {
138 wxString ver;
139 if ( m_dll.IsLoaded() )
140 {
141 wxChar *pc = const_cast<wxChar *>((const wxChar*) filename.t_str());
142
143 DWORD dummy;
144 DWORD sizeVerInfo = m_pfnGetFileVersionInfoSize(pc, &dummy);
145 if ( sizeVerInfo )
146 {
147 wxCharBuffer buf(sizeVerInfo);
148 if ( m_pfnGetFileVersionInfo(pc, 0, sizeVerInfo, buf.data()) )
149 {
150 void *pVer;
151 UINT sizeInfo;
152 if ( m_pfnVerQueryValue(buf.data(),
153 const_cast<wxChar *>(wxT("\\")),
154 &pVer,
155 &sizeInfo) )
156 {
157 VS_FIXEDFILEINFO *info = (VS_FIXEDFILEINFO *)pVer;
158 ver.Printf(wxT("%d.%d.%d.%d"),
159 HIWORD(info->dwFileVersionMS),
160 LOWORD(info->dwFileVersionMS),
161 HIWORD(info->dwFileVersionLS),
162 LOWORD(info->dwFileVersionLS));
163 }
164 }
165 }
166 }
167 //else: we failed to load DLL, can't retrieve version info
168
169 return ver;
170 }
171
172 // ============================================================================
173 // wxDynamicLibraryDetailsCreator implementation
174 // ============================================================================
175
176 /* static */
177 BOOL CALLBACK
178 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCSTR name,
179 DWORD_32_64 base,
180 ULONG size,
181 void *data)
182 {
183 EnumModulesProcParams *params = (EnumModulesProcParams *)data;
184
185 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
186
187 // fill in simple properties
188 details->m_name = wxString::FromAscii(name);
189 details->m_address = wxUIntToPtr(base);
190 details->m_length = size;
191
192 // to get the version, we first need the full path
193 const HMODULE
194 hmod = wxDynamicLibrary::MSWGetModuleHandle(name, details->m_address);
195 if ( hmod )
196 {
197 wxString fullname = wxGetFullModuleName(hmod);
198 if ( !fullname.empty() )
199 {
200 details->m_path = fullname;
201 details->m_version = params->verDLL->GetFileVersion(fullname);
202 }
203 }
204
205 params->dlls->Add(details);
206
207 // continue enumeration (returning FALSE would have stopped it)
208 return TRUE;
209 }
210
211 // ============================================================================
212 // wxDynamicLibrary implementation
213 // ============================================================================
214
215 // ----------------------------------------------------------------------------
216 // misc functions
217 // ----------------------------------------------------------------------------
218
219 wxDllType wxDynamicLibrary::GetProgramHandle()
220 {
221 return (wxDllType)::GetModuleHandle(NULL);
222 }
223
224 // ----------------------------------------------------------------------------
225 // loading/unloading DLLs
226 // ----------------------------------------------------------------------------
227
228 #ifndef MAX_PATH
229 #define MAX_PATH 260 // from VC++ headers
230 #endif
231
232 /* static */
233 wxDllType
234 wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
235 {
236 if (flags & wxDL_GET_LOADED)
237 return ::GetModuleHandle(libname.t_str());
238
239 // Explicitly look in the same path as where the main wx HINSTANCE module
240 // is located (usually the executable or the DLL that uses wx). Normally
241 // this is automatically part of the default search path but in some cases
242 // it may not be, such as when the wxPython extension modules need to load
243 // a DLL, but the intperpreter executable is located elsewhere. Doing
244 // this allows us to always be able to dynamically load a DLL that is
245 // located at the same place as the wx modules.
246 wxString modpath, path;
247 ::GetModuleFileName(wxGetInstance(),
248 wxStringBuffer(modpath, MAX_PATH+1),
249 MAX_PATH);
250
251 wxFileName::SplitPath(modpath, &path, NULL, NULL);
252
253 typedef BOOL (WINAPI *SetDllDirectory_t)(LPCTSTR lpPathName);
254
255 static SetDllDirectory_t s_pfnSetDllDirectory = (SetDllDirectory_t) -1;
256
257 if ( s_pfnSetDllDirectory == (SetDllDirectory_t) -1 )
258 {
259 /*
260 Should wxLoadedDLL ever not be used here (or rather, the
261 wxDL_GET_LOADED flag isn't used), infinite recursion will take
262 place (unless s_pfnSetDllDirectory is set to NULL here right
263 before loading the DLL).
264 */
265 wxLoadedDLL dllKernel("kernel32.dll");
266
267 wxDL_INIT_FUNC_AW(s_pfn, SetDllDirectory, dllKernel);
268 }
269
270 if (s_pfnSetDllDirectory)
271 {
272 s_pfnSetDllDirectory(path.t_str());
273 }
274
275 wxDllType handle = ::LoadLibrary(libname.t_str());
276
277 // reset the search path
278 if (s_pfnSetDllDirectory)
279 {
280 s_pfnSetDllDirectory(NULL);
281 }
282
283 return handle;
284 }
285
286 /* static */
287 void wxDynamicLibrary::Unload(wxDllType handle)
288 {
289 ::FreeLibrary(handle);
290 }
291
292 /* static */
293 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
294 {
295 return (void *)::GetProcAddress(handle,
296 #ifdef __WXWINCE__
297 name.c_str()
298 #else
299 name.ToAscii()
300 #endif // __WXWINCE__
301 );
302 }
303
304 // ----------------------------------------------------------------------------
305 // enumerating loaded DLLs
306 // ----------------------------------------------------------------------------
307
308 /* static */
309 wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
310 {
311 wxDynamicLibraryDetailsArray dlls;
312
313 #if wxUSE_DBGHELP
314 if ( wxDbgHelpDLL::Init() )
315 {
316 // prepare to use functions for version info extraction
317 wxVersionDLL verDLL;
318
319 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params;
320 params.dlls = &dlls;
321 params.verDLL = &verDLL;
322
323 // Note that the cast of EnumModulesProc is needed because the type of
324 // PENUMLOADED_MODULES_CALLBACK changed: in old SDK versions its first
325 // argument was non-const PSTR while now it's PCSTR. By explicitly
326 // casting to whatever the currently used headers require we ensure
327 // that the code compilers in any case.
328 if ( !wxDbgHelpDLL::EnumerateLoadedModules
329 (
330 ::GetCurrentProcess(),
331 (PENUMLOADED_MODULES_CALLBACK)
332 wxDynamicLibraryDetailsCreator::EnumModulesProc,
333 &params
334 ) )
335 {
336 wxLogLastError(wxT("EnumerateLoadedModules"));
337 }
338 }
339 #endif // wxUSE_DBGHELP
340
341 return dlls;
342 }
343
344 /* static */
345 WXHMODULE wxDynamicLibrary::MSWGetModuleHandle(const char *name, void *addr)
346 {
347 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
348 // because the former works correctly for comctl32.dll while the latter
349 // returns NULL when comctl32.dll version 6 is used under XP (note that
350 // GetModuleHandleEx() is only available under XP and later, coincidence?)
351
352 // check if we can use GetModuleHandleEx
353 typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCSTR, HMODULE *);
354
355 static const GetModuleHandleEx_t INVALID_FUNC_PTR = (GetModuleHandleEx_t)-1;
356
357 static GetModuleHandleEx_t s_pfnGetModuleHandleEx = INVALID_FUNC_PTR;
358 if ( s_pfnGetModuleHandleEx == INVALID_FUNC_PTR )
359 {
360 wxDynamicLibrary dll(wxT("kernel32.dll"), wxDL_VERBATIM);
361 s_pfnGetModuleHandleEx =
362 (GetModuleHandleEx_t)dll.RawGetSymbol(wxT("GetModuleHandleExA"));
363
364 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
365 }
366
367 // get module handle from its address
368 if ( s_pfnGetModuleHandleEx )
369 {
370 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
371 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
372 HMODULE hmod;
373 if ( s_pfnGetModuleHandleEx(6, (char *)addr, &hmod) && hmod )
374 return hmod;
375 }
376
377 // Windows CE only has Unicode API, so even we have an ANSI string here, we
378 // still need to use GetModuleHandleW() there
379 #ifdef __WXWINCE__
380 return ::GetModuleHandleW(wxConvLibc.cMB2WC(name).data());
381 #else
382 return ::GetModuleHandleA((char *)name);
383 #endif
384 }
385
386 #endif // wxUSE_DYNLIB_CLASS
387