1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Win32-specific part of wxDynamicLibrary and related classes
4 // Author: Vadim Zeitlin
6 // Created: 2005-01-10 (partly extracted from common/dynlib.cpp)
8 // Copyright: (c) 1998-2005 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_DYNLIB_CLASS
28 #include "wx/msw/private.h"
29 #include "wx/msw/debughlp.h"
31 const wxString
wxDynamicLibrary::ms_dllext(wxT(".dll"));
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // wrap some functions from version.dll: load them dynamically and provide a
42 // load version.dll and bind to its functions
45 // return the file version as string, e.g. "x.y.z.w"
46 wxString
GetFileVersion(const wxString
& filename
) const;
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
);
53 #define DO_FOR_ALL_VER_FUNCS(what) \
54 what(GetFileVersionInfoSize); \
55 what(GetFileVersionInfo); \
58 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
60 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION
);
62 #undef DECLARE_VER_FUNCTION
65 wxDynamicLibrary m_dll
;
68 wxDECLARE_NO_COPY_CLASS(wxVersionDLL
);
71 // class used to create wxDynamicLibraryDetails objects
72 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
75 // type of parameters being passed to EnumModulesProc
76 struct EnumModulesProcParams
78 wxDynamicLibraryDetailsArray
*dlls
;
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
;
89 typedef PSTR NameStr_t
;
92 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
94 typedef DWORD64 DWORD_32_64
;
96 typedef DWORD DWORD_32_64
;
100 EnumModulesProc(NameStr_t name
, DWORD_32_64 base
, ULONG size
, void *data
);
103 // ============================================================================
104 // wxVersionDLL implementation
105 // ============================================================================
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 wxVersionDLL::wxVersionDLL()
113 // don't give errors if DLL can't be loaded or used, we're prepared to
117 if ( m_dll
.Load(wxT("version.dll"), wxDL_VERBATIM
) )
119 // the functions we load have either 'A' or 'W' suffix depending on
120 // whether we're in ANSI or Unicode build
125 #endif // UNICODE/ANSI
127 #define LOAD_VER_FUNCTION(name) \
128 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
129 if ( !m_pfn ## name ) \
135 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
137 #undef LOAD_VER_FUNCTION
141 // ----------------------------------------------------------------------------
142 // wxVersionDLL operations
143 // ----------------------------------------------------------------------------
145 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
148 if ( m_dll
.IsLoaded() )
150 wxChar
*pc
= const_cast<wxChar
*>((const wxChar
*) filename
.t_str());
153 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
156 wxCharBuffer
buf(sizeVerInfo
);
157 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
161 if ( m_pfnVerQueryValue(buf
.data(),
162 const_cast<wxChar
*>(wxT("\\")),
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
));
176 //else: we failed to load DLL, can't retrieve version info
181 // ============================================================================
182 // wxDynamicLibraryDetailsCreator implementation
183 // ============================================================================
187 wxDynamicLibraryDetailsCreator::EnumModulesProc(NameStr_t name
,
192 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
194 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
196 // fill in simple properties
197 details
->m_name
= wxString::FromAscii(name
);
198 details
->m_address
= wxUIntToPtr(base
);
199 details
->m_length
= size
;
201 // to get the version, we first need the full path
203 hmod
= wxDynamicLibrary::MSWGetModuleHandle(name
, details
->m_address
);
206 wxString fullname
= wxGetFullModuleName(hmod
);
207 if ( !fullname
.empty() )
209 details
->m_path
= fullname
;
210 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
214 params
->dlls
->Add(details
);
216 // continue enumeration (returning FALSE would have stopped it)
220 // ============================================================================
221 // wxDynamicLibrary implementation
222 // ============================================================================
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 wxDllType
wxDynamicLibrary::GetProgramHandle()
230 return (wxDllType
)::GetModuleHandle(NULL
);
233 // ----------------------------------------------------------------------------
234 // loading/unloading DLLs
235 // ----------------------------------------------------------------------------
239 wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
241 return flags
& wxDL_GET_LOADED
242 ? ::GetModuleHandle(libname
.t_str())
243 : ::LoadLibrary(libname
.t_str());
247 void wxDynamicLibrary::Unload(wxDllType handle
)
249 ::FreeLibrary(handle
);
253 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
255 return (void *)::GetProcAddress(handle
,
260 #endif // __WXWINCE__
264 // ----------------------------------------------------------------------------
265 // enumerating loaded DLLs
266 // ----------------------------------------------------------------------------
269 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
271 wxDynamicLibraryDetailsArray dlls
;
274 if ( wxDbgHelpDLL::Init() )
276 // prepare to use functions for version info extraction
279 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
281 params
.verDLL
= &verDLL
;
283 if ( !wxDbgHelpDLL::EnumerateLoadedModules
285 ::GetCurrentProcess(),
286 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
290 wxLogLastError(wxT("EnumerateLoadedModules"));
293 #endif // wxUSE_DBGHELP
299 HMODULE
wxDynamicLibrary::MSWGetModuleHandle(const char *name
, void *addr
)
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?)
306 // check if we can use GetModuleHandleEx
307 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCSTR
, HMODULE
*);
309 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
311 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
312 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
314 wxDynamicLibrary
dll(wxT("kernel32.dll"), wxDL_VERBATIM
);
315 s_pfnGetModuleHandleEx
=
316 (GetModuleHandleEx_t
)dll
.RawGetSymbol(wxT("GetModuleHandleExA"));
318 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
321 // get module handle from its address
322 if ( s_pfnGetModuleHandleEx
)
324 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
325 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
327 if ( s_pfnGetModuleHandleEx(6, (char *)addr
, &hmod
) && hmod
)
331 // Windows CE only has Unicode API, so even we have an ANSI string here, we
332 // still need to use GetModuleHandleW() there
334 return ::GetModuleHandleW(wxConvLibc
.cMB2WC(name
).data());
336 return ::GetModuleHandleA((char *)name
);
340 #endif // wxUSE_DYNLIB_CLASS