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