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(_T(".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 DECLARE_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 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // return the module handle for the given base name
109 HMODULE
wxGetModuleHandle(const char *name
, void *addr
)
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?)
116 // check if we can use GetModuleHandleEx
117 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCSTR
, HMODULE
*);
119 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
121 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
122 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
124 wxDynamicLibrary
dll(_T("kernel32.dll"), wxDL_VERBATIM
);
125 s_pfnGetModuleHandleEx
=
126 (GetModuleHandleEx_t
)dll
.RawGetSymbol(_T("GetModuleHandleExA"));
128 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
131 // get module handle from its address
132 if ( s_pfnGetModuleHandleEx
)
134 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
135 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
137 if ( s_pfnGetModuleHandleEx(6, (char *)addr
, &hmod
) && hmod
)
141 // Windows CE only has Unicode API, so even we have an ANSI string here, we
142 // still need to use GetModuleHandleW() there and so do it everywhere to
143 // avoid #ifdefs -- this code is not performance-critical anyhow...
145 return ::GetModuleHandleW(wxConvLibc
.cMB2WC(name
).data());
147 return ::GetModuleHandleA((char *)name
);
151 // ============================================================================
152 // wxVersionDLL implementation
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 wxVersionDLL::wxVersionDLL()
161 // don't give errors if DLL can't be loaded or used, we're prepared to
165 if ( m_dll
.Load(_T("version.dll"), wxDL_VERBATIM
) )
167 // the functions we load have either 'A' or 'W' suffix depending on
168 // whether we're in ANSI or Unicode build
173 #endif // UNICODE/ANSI
175 #define LOAD_VER_FUNCTION(name) \
176 m_pfn ## name = (name ## _t)m_dll.GetSymbol(_T(#name SUFFIX)); \
177 if ( !m_pfn ## name ) \
183 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
185 #undef LOAD_VER_FUNCTION
189 // ----------------------------------------------------------------------------
190 // wxVersionDLL operations
191 // ----------------------------------------------------------------------------
193 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
196 if ( m_dll
.IsLoaded() )
198 wxChar
*pc
= wx_const_cast(wxChar
*, filename
.wx_str());
201 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
204 wxCharBuffer
buf(sizeVerInfo
);
205 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
209 if ( m_pfnVerQueryValue(buf
.data(), _T("\\"), &pVer
, &sizeInfo
) )
211 VS_FIXEDFILEINFO
*info
= (VS_FIXEDFILEINFO
*)pVer
;
212 ver
.Printf(_T("%d.%d.%d.%d"),
213 HIWORD(info
->dwFileVersionMS
),
214 LOWORD(info
->dwFileVersionMS
),
215 HIWORD(info
->dwFileVersionLS
),
216 LOWORD(info
->dwFileVersionLS
));
221 //else: we failed to load DLL, can't retrieve version info
226 // ============================================================================
227 // wxDynamicLibraryDetailsCreator implementation
228 // ============================================================================
232 wxDynamicLibraryDetailsCreator::EnumModulesProc(NameStr_t name
,
237 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
239 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
241 // fill in simple properties
242 details
->m_name
= wxString::FromAscii(name
);
243 details
->m_address
= wx_reinterpret_cast(void *, base
);
244 details
->m_length
= size
;
246 // to get the version, we first need the full path
247 HMODULE hmod
= wxGetModuleHandle(name
, (void *)base
);
250 wxString fullname
= wxGetFullModuleName(hmod
);
251 if ( !fullname
.empty() )
253 details
->m_path
= fullname
;
254 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
258 params
->dlls
->Add(details
);
260 // continue enumeration (returning FALSE would have stopped it)
264 // ============================================================================
265 // wxDynamicLibrary implementation
266 // ============================================================================
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 wxDllType
wxDynamicLibrary::GetProgramHandle()
274 return (wxDllType
)::GetModuleHandle(NULL
);
277 // ----------------------------------------------------------------------------
278 // loading/unloading DLLs
279 // ----------------------------------------------------------------------------
283 wxDynamicLibrary::RawLoad(const wxString
& libname
, int WXUNUSED(flags
))
285 return ::LoadLibrary(libname
.wx_str());
289 void wxDynamicLibrary::Unload(wxDllType handle
)
291 ::FreeLibrary(handle
);
295 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
297 return (void *)::GetProcAddress(handle
,
302 #endif // __WXWINCE__
306 // ----------------------------------------------------------------------------
307 // enumerating loaded DLLs
308 // ----------------------------------------------------------------------------
311 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
313 wxDynamicLibraryDetailsArray dlls
;
316 if ( wxDbgHelpDLL::Init() )
318 // prepare to use functions for version info extraction
321 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
323 params
.verDLL
= &verDLL
;
325 if ( !wxDbgHelpDLL::EnumerateLoadedModules
327 ::GetCurrentProcess(),
328 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
332 wxLogLastError(_T("EnumerateLoadedModules"));
335 #endif // wxUSE_DBGHELP
340 #endif // wxUSE_DYNLIB_CLASS