]>
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 | |
9a83f860 | 32 | const wxString wxDynamicLibrary::ms_dllext(wxT(".dll")); |
defbed48 VZ |
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 | ||
c0c133e1 | 69 | wxDECLARE_NO_COPY_CLASS(wxVersionDLL); |
defbed48 VZ |
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 | ||
e7f28a58 VZ |
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 | ||
defbed48 | 90 | static BOOL CALLBACK |
ba97f6f8 | 91 | EnumModulesProc(PCSTR name, DWORD_32_64 base, ULONG size, void *data); |
defbed48 VZ |
92 | }; |
93 | ||
defbed48 VZ |
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 | ||
9a83f860 | 108 | if ( m_dll.Load(wxT("version.dll"), wxDL_VERBATIM) ) |
defbed48 VZ |
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) \ | |
9a83f860 | 119 | m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \ |
defbed48 VZ |
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 | { | |
5c33522f | 141 | wxChar *pc = const_cast<wxChar *>((const wxChar*) filename.t_str()); |
defbed48 VZ |
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; | |
83a42976 | 152 | if ( m_pfnVerQueryValue(buf.data(), |
9a83f860 | 153 | const_cast<wxChar *>(wxT("\\")), |
83a42976 VZ |
154 | &pVer, |
155 | &sizeInfo) ) | |
defbed48 VZ |
156 | { |
157 | VS_FIXEDFILEINFO *info = (VS_FIXEDFILEINFO *)pVer; | |
9a83f860 | 158 | ver.Printf(wxT("%d.%d.%d.%d"), |
defbed48 VZ |
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 | |
ba97f6f8 | 178 | wxDynamicLibraryDetailsCreator::EnumModulesProc(PCSTR name, |
e7f28a58 | 179 | DWORD_32_64 base, |
defbed48 VZ |
180 | ULONG size, |
181 | void *data) | |
182 | { | |
183 | EnumModulesProcParams *params = (EnumModulesProcParams *)data; | |
184 | ||
185 | wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails; | |
186 | ||
187 | // fill in simple properties | |
252e3b2c | 188 | details->m_name = name; |
3f49efdb | 189 | details->m_address = wxUIntToPtr(base); |
defbed48 VZ |
190 | details->m_length = size; |
191 | ||
192 | // to get the version, we first need the full path | |
252e3b2c VZ |
193 | const HMODULE hmod = wxDynamicLibrary::MSWGetModuleHandle |
194 | ( | |
195 | details->m_name, | |
196 | details->m_address | |
197 | ); | |
defbed48 VZ |
198 | if ( hmod ) |
199 | { | |
200 | wxString fullname = wxGetFullModuleName(hmod); | |
201 | if ( !fullname.empty() ) | |
202 | { | |
203 | details->m_path = fullname; | |
204 | details->m_version = params->verDLL->GetFileVersion(fullname); | |
205 | } | |
206 | } | |
207 | ||
208 | params->dlls->Add(details); | |
209 | ||
210 | // continue enumeration (returning FALSE would have stopped it) | |
211 | return TRUE; | |
212 | } | |
213 | ||
214 | // ============================================================================ | |
215 | // wxDynamicLibrary implementation | |
216 | // ============================================================================ | |
217 | ||
6d94009f VZ |
218 | // ---------------------------------------------------------------------------- |
219 | // misc functions | |
220 | // ---------------------------------------------------------------------------- | |
221 | ||
222 | wxDllType wxDynamicLibrary::GetProgramHandle() | |
223 | { | |
224 | return (wxDllType)::GetModuleHandle(NULL); | |
225 | } | |
226 | ||
defbed48 VZ |
227 | // ---------------------------------------------------------------------------- |
228 | // loading/unloading DLLs | |
229 | // ---------------------------------------------------------------------------- | |
230 | ||
932d0768 RD |
231 | #ifndef MAX_PATH |
232 | #define MAX_PATH 260 // from VC++ headers | |
233 | #endif | |
234 | ||
defbed48 | 235 | /* static */ |
da55d064 | 236 | wxDllType |
e2d4ce7d | 237 | wxDynamicLibrary::RawLoad(const wxString& libname, int flags) |
defbed48 | 238 | { |
932d0768 RD |
239 | if (flags & wxDL_GET_LOADED) |
240 | return ::GetModuleHandle(libname.t_str()); | |
241 | ||
242 | // Explicitly look in the same path as where the main wx HINSTANCE module | |
243 | // is located (usually the executable or the DLL that uses wx). Normally | |
244 | // this is automatically part of the default search path but in some cases | |
245 | // it may not be, such as when the wxPython extension modules need to load | |
246 | // a DLL, but the intperpreter executable is located elsewhere. Doing | |
247 | // this allows us to always be able to dynamically load a DLL that is | |
248 | // located at the same place as the wx modules. | |
249 | wxString modpath, path; | |
250 | ::GetModuleFileName(wxGetInstance(), | |
251 | wxStringBuffer(modpath, MAX_PATH+1), | |
252 | MAX_PATH); | |
253 | ||
254 | wxFileName::SplitPath(modpath, &path, NULL, NULL); | |
685a9fa6 DS |
255 | |
256 | typedef BOOL (WINAPI *SetDllDirectory_t)(LPCTSTR lpPathName); | |
257 | ||
258 | static SetDllDirectory_t s_pfnSetDllDirectory = (SetDllDirectory_t) -1; | |
259 | ||
260 | if ( s_pfnSetDllDirectory == (SetDllDirectory_t) -1 ) | |
261 | { | |
262 | /* | |
263 | Should wxLoadedDLL ever not be used here (or rather, the | |
264 | wxDL_GET_LOADED flag isn't used), infinite recursion will take | |
265 | place (unless s_pfnSetDllDirectory is set to NULL here right | |
266 | before loading the DLL). | |
267 | */ | |
268 | wxLoadedDLL dllKernel("kernel32.dll"); | |
269 | ||
270 | wxDL_INIT_FUNC_AW(s_pfn, SetDllDirectory, dllKernel); | |
271 | } | |
272 | ||
273 | if (s_pfnSetDllDirectory) | |
274 | { | |
275 | s_pfnSetDllDirectory(path.t_str()); | |
276 | } | |
277 | ||
932d0768 RD |
278 | wxDllType handle = ::LoadLibrary(libname.t_str()); |
279 | ||
280 | // reset the search path | |
685a9fa6 DS |
281 | if (s_pfnSetDllDirectory) |
282 | { | |
283 | s_pfnSetDllDirectory(NULL); | |
284 | } | |
285 | ||
932d0768 | 286 | return handle; |
defbed48 VZ |
287 | } |
288 | ||
289 | /* static */ | |
290 | void wxDynamicLibrary::Unload(wxDllType handle) | |
291 | { | |
292 | ::FreeLibrary(handle); | |
293 | } | |
294 | ||
295 | /* static */ | |
296 | void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name) | |
297 | { | |
23ee4b48 WS |
298 | return (void *)::GetProcAddress(handle, |
299 | #ifdef __WXWINCE__ | |
300 | name.c_str() | |
301 | #else | |
302 | name.ToAscii() | |
303 | #endif // __WXWINCE__ | |
304 | ); | |
defbed48 VZ |
305 | } |
306 | ||
307 | // ---------------------------------------------------------------------------- | |
308 | // enumerating loaded DLLs | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
311 | /* static */ | |
312 | wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded() | |
313 | { | |
314 | wxDynamicLibraryDetailsArray dlls; | |
315 | ||
94534bf8 | 316 | #if wxUSE_DBGHELP |
defbed48 VZ |
317 | if ( wxDbgHelpDLL::Init() ) |
318 | { | |
319 | // prepare to use functions for version info extraction | |
320 | wxVersionDLL verDLL; | |
321 | ||
322 | wxDynamicLibraryDetailsCreator::EnumModulesProcParams params; | |
323 | params.dlls = &dlls; | |
324 | params.verDLL = &verDLL; | |
325 | ||
ba97f6f8 VZ |
326 | // Note that the cast of EnumModulesProc is needed because the type of |
327 | // PENUMLOADED_MODULES_CALLBACK changed: in old SDK versions its first | |
328 | // argument was non-const PSTR while now it's PCSTR. By explicitly | |
329 | // casting to whatever the currently used headers require we ensure | |
330 | // that the code compilers in any case. | |
defbed48 VZ |
331 | if ( !wxDbgHelpDLL::EnumerateLoadedModules |
332 | ( | |
333 | ::GetCurrentProcess(), | |
ba97f6f8 | 334 | (PENUMLOADED_MODULES_CALLBACK) |
defbed48 VZ |
335 | wxDynamicLibraryDetailsCreator::EnumModulesProc, |
336 | ¶ms | |
337 | ) ) | |
338 | { | |
9a83f860 | 339 | wxLogLastError(wxT("EnumerateLoadedModules")); |
defbed48 VZ |
340 | } |
341 | } | |
94534bf8 | 342 | #endif // wxUSE_DBGHELP |
defbed48 VZ |
343 | |
344 | return dlls; | |
345 | } | |
346 | ||
c118a476 | 347 | /* static */ |
252e3b2c | 348 | WXHMODULE wxDynamicLibrary::MSWGetModuleHandle(const wxString& name, void *addr) |
c118a476 VZ |
349 | { |
350 | // we want to use GetModuleHandleEx() instead of usual GetModuleHandle() | |
351 | // because the former works correctly for comctl32.dll while the latter | |
352 | // returns NULL when comctl32.dll version 6 is used under XP (note that | |
353 | // GetModuleHandleEx() is only available under XP and later, coincidence?) | |
354 | ||
355 | // check if we can use GetModuleHandleEx | |
252e3b2c | 356 | typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCTSTR, HMODULE *); |
c118a476 VZ |
357 | |
358 | static const GetModuleHandleEx_t INVALID_FUNC_PTR = (GetModuleHandleEx_t)-1; | |
359 | ||
360 | static GetModuleHandleEx_t s_pfnGetModuleHandleEx = INVALID_FUNC_PTR; | |
361 | if ( s_pfnGetModuleHandleEx == INVALID_FUNC_PTR ) | |
362 | { | |
363 | wxDynamicLibrary dll(wxT("kernel32.dll"), wxDL_VERBATIM); | |
364 | s_pfnGetModuleHandleEx = | |
252e3b2c | 365 | (GetModuleHandleEx_t)dll.GetSymbolAorW(wxT("GetModuleHandleEx")); |
c118a476 VZ |
366 | |
367 | // dll object can be destroyed, kernel32.dll won't be unloaded anyhow | |
368 | } | |
369 | ||
370 | // get module handle from its address | |
371 | if ( s_pfnGetModuleHandleEx ) | |
372 | { | |
373 | // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | | |
374 | // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
375 | HMODULE hmod; | |
252e3b2c | 376 | if ( s_pfnGetModuleHandleEx(6, (LPCTSTR)addr, &hmod) && hmod ) |
c118a476 VZ |
377 | return hmod; |
378 | } | |
379 | ||
5ae1a669 | 380 | return ::GetModuleHandle(name.t_str()); |
c118a476 VZ |
381 | } |
382 | ||
defbed48 VZ |
383 | #endif // wxUSE_DYNLIB_CLASS |
384 |