]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dlmsw.cpp
Added wxAutoOleInterface<T> template.
[wxWidgets.git] / src / msw / dlmsw.cpp
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
31 const wxString wxDynamicLibrary::ms_dllext(wxT(".dll"));
32
33 // ----------------------------------------------------------------------------
34 // private classes
35 // ----------------------------------------------------------------------------
36
37 // wrap some functions from version.dll: load them dynamically and provide a
38 // clean interface
39 class wxVersionDLL
40 {
41 public:
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
48 private:
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 wxDECLARE_NO_COPY_CLASS(wxVersionDLL);
69 };
70
71 // class used to create wxDynamicLibraryDetails objects
72 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
73 {
74 public:
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 // wxVersionDLL implementation
105 // ============================================================================
106
107 // ----------------------------------------------------------------------------
108 // loading
109 // ----------------------------------------------------------------------------
110
111 wxVersionDLL::wxVersionDLL()
112 {
113 // don't give errors if DLL can't be loaded or used, we're prepared to
114 // handle it
115 wxLogNull noLog;
116
117 if ( m_dll.Load(wxT("version.dll"), wxDL_VERBATIM) )
118 {
119 // the functions we load have either 'A' or 'W' suffix depending on
120 // whether we're in ANSI or Unicode build
121 #ifdef UNICODE
122 #define SUFFIX L"W"
123 #else // ANSI
124 #define SUFFIX "A"
125 #endif // UNICODE/ANSI
126
127 #define LOAD_VER_FUNCTION(name) \
128 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
129 if ( !m_pfn ## name ) \
130 { \
131 m_dll.Unload(); \
132 return; \
133 }
134
135 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION);
136
137 #undef LOAD_VER_FUNCTION
138 }
139 }
140
141 // ----------------------------------------------------------------------------
142 // wxVersionDLL operations
143 // ----------------------------------------------------------------------------
144
145 wxString wxVersionDLL::GetFileVersion(const wxString& filename) const
146 {
147 wxString ver;
148 if ( m_dll.IsLoaded() )
149 {
150 wxChar *pc = const_cast<wxChar *>((const wxChar*) filename.t_str());
151
152 DWORD dummy;
153 DWORD sizeVerInfo = m_pfnGetFileVersionInfoSize(pc, &dummy);
154 if ( sizeVerInfo )
155 {
156 wxCharBuffer buf(sizeVerInfo);
157 if ( m_pfnGetFileVersionInfo(pc, 0, sizeVerInfo, buf.data()) )
158 {
159 void *pVer;
160 UINT sizeInfo;
161 if ( m_pfnVerQueryValue(buf.data(),
162 const_cast<wxChar *>(wxT("\\")),
163 &pVer,
164 &sizeInfo) )
165 {
166 VS_FIXEDFILEINFO *info = (VS_FIXEDFILEINFO *)pVer;
167 ver.Printf(wxT("%d.%d.%d.%d"),
168 HIWORD(info->dwFileVersionMS),
169 LOWORD(info->dwFileVersionMS),
170 HIWORD(info->dwFileVersionLS),
171 LOWORD(info->dwFileVersionLS));
172 }
173 }
174 }
175 }
176 //else: we failed to load DLL, can't retrieve version info
177
178 return ver;
179 }
180
181 // ============================================================================
182 // wxDynamicLibraryDetailsCreator implementation
183 // ============================================================================
184
185 /* static */
186 BOOL CALLBACK
187 wxDynamicLibraryDetailsCreator::EnumModulesProc(NameStr_t name,
188 DWORD_32_64 base,
189 ULONG size,
190 void *data)
191 {
192 EnumModulesProcParams *params = (EnumModulesProcParams *)data;
193
194 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
195
196 // fill in simple properties
197 details->m_name = wxString::FromAscii(name);
198 details->m_address = wxUIntToPtr(base);
199 details->m_length = size;
200
201 // to get the version, we first need the full path
202 const HMODULE
203 hmod = wxDynamicLibrary::MSWGetModuleHandle(name, details->m_address);
204 if ( hmod )
205 {
206 wxString fullname = wxGetFullModuleName(hmod);
207 if ( !fullname.empty() )
208 {
209 details->m_path = fullname;
210 details->m_version = params->verDLL->GetFileVersion(fullname);
211 }
212 }
213
214 params->dlls->Add(details);
215
216 // continue enumeration (returning FALSE would have stopped it)
217 return TRUE;
218 }
219
220 // ============================================================================
221 // wxDynamicLibrary implementation
222 // ============================================================================
223
224 // ----------------------------------------------------------------------------
225 // misc functions
226 // ----------------------------------------------------------------------------
227
228 wxDllType wxDynamicLibrary::GetProgramHandle()
229 {
230 return (wxDllType)::GetModuleHandle(NULL);
231 }
232
233 // ----------------------------------------------------------------------------
234 // loading/unloading DLLs
235 // ----------------------------------------------------------------------------
236
237 /* static */
238 wxDllType
239 wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
240 {
241 return flags & wxDL_GET_LOADED
242 ? ::GetModuleHandle(libname.t_str())
243 : ::LoadLibrary(libname.t_str());
244 }
245
246 /* static */
247 void wxDynamicLibrary::Unload(wxDllType handle)
248 {
249 ::FreeLibrary(handle);
250 }
251
252 /* static */
253 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
254 {
255 return (void *)::GetProcAddress(handle,
256 #ifdef __WXWINCE__
257 name.c_str()
258 #else
259 name.ToAscii()
260 #endif // __WXWINCE__
261 );
262 }
263
264 // ----------------------------------------------------------------------------
265 // enumerating loaded DLLs
266 // ----------------------------------------------------------------------------
267
268 /* static */
269 wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
270 {
271 wxDynamicLibraryDetailsArray dlls;
272
273 #if wxUSE_DBGHELP
274 if ( wxDbgHelpDLL::Init() )
275 {
276 // prepare to use functions for version info extraction
277 wxVersionDLL verDLL;
278
279 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params;
280 params.dlls = &dlls;
281 params.verDLL = &verDLL;
282
283 if ( !wxDbgHelpDLL::EnumerateLoadedModules
284 (
285 ::GetCurrentProcess(),
286 wxDynamicLibraryDetailsCreator::EnumModulesProc,
287 &params
288 ) )
289 {
290 wxLogLastError(wxT("EnumerateLoadedModules"));
291 }
292 }
293 #endif // wxUSE_DBGHELP
294
295 return dlls;
296 }
297
298 /* static */
299 HMODULE wxDynamicLibrary::MSWGetModuleHandle(const char *name, void *addr)
300 {
301 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
302 // because the former works correctly for comctl32.dll while the latter
303 // returns NULL when comctl32.dll version 6 is used under XP (note that
304 // GetModuleHandleEx() is only available under XP and later, coincidence?)
305
306 // check if we can use GetModuleHandleEx
307 typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCSTR, HMODULE *);
308
309 static const GetModuleHandleEx_t INVALID_FUNC_PTR = (GetModuleHandleEx_t)-1;
310
311 static GetModuleHandleEx_t s_pfnGetModuleHandleEx = INVALID_FUNC_PTR;
312 if ( s_pfnGetModuleHandleEx == INVALID_FUNC_PTR )
313 {
314 wxDynamicLibrary dll(wxT("kernel32.dll"), wxDL_VERBATIM);
315 s_pfnGetModuleHandleEx =
316 (GetModuleHandleEx_t)dll.RawGetSymbol(wxT("GetModuleHandleExA"));
317
318 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
319 }
320
321 // get module handle from its address
322 if ( s_pfnGetModuleHandleEx )
323 {
324 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
325 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
326 HMODULE hmod;
327 if ( s_pfnGetModuleHandleEx(6, (char *)addr, &hmod) && hmod )
328 return hmod;
329 }
330
331 // Windows CE only has Unicode API, so even we have an ANSI string here, we
332 // still need to use GetModuleHandleW() there
333 #ifdef __WXWINCE__
334 return ::GetModuleHandleW(wxConvLibc.cMB2WC(name).data());
335 #else
336 return ::GetModuleHandleA((char *)name);
337 #endif
338 }
339
340 #endif // wxUSE_DYNLIB_CLASS
341