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 wxChar
*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
;
83 EnumModulesProc(PSTR name
, DWORD base
, ULONG size
, void *data
);
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // return the module handle for the given base name
92 HMODULE
wxGetModuleHandle(const char *name
, void *addr
)
94 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
95 // because the former works correctly for comctl32.dll while the latter
96 // returns NULL when comctl32.dll version 6 is used under XP (note that
97 // GetModuleHandleEx() is only available under XP and later, coincidence?)
99 // check if we can use GetModuleHandleEx
100 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCTSTR
, HMODULE
*);
102 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
104 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
105 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
107 wxDynamicLibrary
dll(_T("kernel32.dll"), wxDL_VERBATIM
);
108 s_pfnGetModuleHandleEx
=
109 (GetModuleHandleEx_t
)dll
.RawGetSymbol(_T("GetModuleHandleExA"));
111 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
114 // get module handle from its address
115 if ( s_pfnGetModuleHandleEx
)
117 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
118 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
120 if ( s_pfnGetModuleHandleEx(6, (char *)addr
, &hmod
) && hmod
)
124 // if failed, try by name
125 return ::GetModuleHandleA(name
);
128 // ============================================================================
129 // wxVersionDLL implementation
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxVersionDLL::wxVersionDLL()
138 // don't give errors if DLL can't be loaded or used, we're prepared to
142 if ( m_dll
.Load(_T("version.dll"), wxDL_VERBATIM
) )
144 // the functions we load have either 'A' or 'W' suffix depending on
145 // whether we're in ANSI or Unicode build
150 #endif // UNICODE/ANSI
152 #define LOAD_VER_FUNCTION(name) \
153 m_pfn ## name = (name ## _t)m_dll.GetSymbol(_T(#name SUFFIX)); \
154 if ( !m_pfn ## name ) \
160 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
162 #undef LOAD_VER_FUNCTION
166 // ----------------------------------------------------------------------------
167 // wxVersionDLL operations
168 // ----------------------------------------------------------------------------
170 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
173 if ( m_dll
.IsLoaded() )
175 wxChar
*pc
= wx_const_cast(wxChar
*, filename
.c_str());
178 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
181 wxCharBuffer
buf(sizeVerInfo
);
182 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
186 if ( m_pfnVerQueryValue(buf
.data(), _T("\\"), &pVer
, &sizeInfo
) )
188 VS_FIXEDFILEINFO
*info
= (VS_FIXEDFILEINFO
*)pVer
;
189 ver
.Printf(_T("%d.%d.%d.%d"),
190 HIWORD(info
->dwFileVersionMS
),
191 LOWORD(info
->dwFileVersionMS
),
192 HIWORD(info
->dwFileVersionLS
),
193 LOWORD(info
->dwFileVersionLS
));
198 //else: we failed to load DLL, can't retrieve version info
203 // ============================================================================
204 // wxDynamicLibraryDetailsCreator implementation
205 // ============================================================================
209 wxDynamicLibraryDetailsCreator::EnumModulesProc(PSTR name
,
214 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
216 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
218 // fill in simple properties
219 details
->m_name
= wxString::FromAscii(name
);
220 details
->m_address
= wx_reinterpret_cast(void *, base
);
221 details
->m_length
= size
;
223 // to get the version, we first need the full path
224 HMODULE hmod
= wxGetModuleHandle(name
, (void *)base
);
227 wxString fullname
= wxGetFullModuleName(hmod
);
228 if ( !fullname
.empty() )
230 details
->m_path
= fullname
;
231 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
235 params
->dlls
->Add(details
);
237 // continue enumeration (returning FALSE would have stopped it)
241 // ============================================================================
242 // wxDynamicLibrary implementation
243 // ============================================================================
245 // ----------------------------------------------------------------------------
246 // loading/unloading DLLs
247 // ----------------------------------------------------------------------------
251 wxDynamicLibrary::RawLoad(const wxString
& libname
, int WXUNUSED(flags
))
253 return ::LoadLibrary(libname
);
257 void wxDynamicLibrary::Unload(wxDllType handle
)
259 ::FreeLibrary(handle
);
263 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
265 return ::GetProcAddress(handle
, name
);
268 // ----------------------------------------------------------------------------
269 // enumerating loaded DLLs
270 // ----------------------------------------------------------------------------
273 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
275 wxDynamicLibraryDetailsArray dlls
;
277 if ( wxDbgHelpDLL::Init() )
279 // prepare to use functions for version info extraction
282 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
284 params
.verDLL
= &verDLL
;
286 if ( !wxDbgHelpDLL::EnumerateLoadedModules
288 ::GetCurrentProcess(),
289 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
293 wxLogLastError(_T("EnumerateLoadedModules"));
300 #endif // wxUSE_DYNLIB_CLASS