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