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