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 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
84 typedef DWORD64 DWORD_32_64
;
86 typedef DWORD DWORD_32_64
;
90 EnumModulesProc(PCSTR name
, DWORD_32_64 base
, ULONG size
, void *data
);
93 // ============================================================================
94 // wxVersionDLL implementation
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 wxVersionDLL::wxVersionDLL()
103 // don't give errors if DLL can't be loaded or used, we're prepared to
107 if ( m_dll
.Load(wxT("version.dll"), wxDL_VERBATIM
) )
109 // the functions we load have either 'A' or 'W' suffix depending on
110 // whether we're in ANSI or Unicode build
115 #endif // UNICODE/ANSI
117 #define LOAD_VER_FUNCTION(name) \
118 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
119 if ( !m_pfn ## name ) \
125 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
127 #undef LOAD_VER_FUNCTION
131 // ----------------------------------------------------------------------------
132 // wxVersionDLL operations
133 // ----------------------------------------------------------------------------
135 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
138 if ( m_dll
.IsLoaded() )
140 wxChar
*pc
= const_cast<wxChar
*>((const wxChar
*) filename
.t_str());
143 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
146 wxCharBuffer
buf(sizeVerInfo
);
147 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
151 if ( m_pfnVerQueryValue(buf
.data(),
152 const_cast<wxChar
*>(wxT("\\")),
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
));
166 //else: we failed to load DLL, can't retrieve version info
171 // ============================================================================
172 // wxDynamicLibraryDetailsCreator implementation
173 // ============================================================================
177 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCSTR name
,
182 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
184 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
186 // fill in simple properties
187 details
->m_name
= wxString::FromAscii(name
);
188 details
->m_address
= wxUIntToPtr(base
);
189 details
->m_length
= size
;
191 // to get the version, we first need the full path
193 hmod
= wxDynamicLibrary::MSWGetModuleHandle(name
, details
->m_address
);
196 wxString fullname
= wxGetFullModuleName(hmod
);
197 if ( !fullname
.empty() )
199 details
->m_path
= fullname
;
200 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
204 params
->dlls
->Add(details
);
206 // continue enumeration (returning FALSE would have stopped it)
210 // ============================================================================
211 // wxDynamicLibrary implementation
212 // ============================================================================
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 wxDllType
wxDynamicLibrary::GetProgramHandle()
220 return (wxDllType
)::GetModuleHandle(NULL
);
223 // ----------------------------------------------------------------------------
224 // loading/unloading DLLs
225 // ----------------------------------------------------------------------------
229 wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
231 return flags
& wxDL_GET_LOADED
232 ? ::GetModuleHandle(libname
.t_str())
233 : ::LoadLibrary(libname
.t_str());
237 void wxDynamicLibrary::Unload(wxDllType handle
)
239 ::FreeLibrary(handle
);
243 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
245 return (void *)::GetProcAddress(handle
,
250 #endif // __WXWINCE__
254 // ----------------------------------------------------------------------------
255 // enumerating loaded DLLs
256 // ----------------------------------------------------------------------------
259 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
261 wxDynamicLibraryDetailsArray dlls
;
264 if ( wxDbgHelpDLL::Init() )
266 // prepare to use functions for version info extraction
269 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
271 params
.verDLL
= &verDLL
;
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
280 ::GetCurrentProcess(),
281 (PENUMLOADED_MODULES_CALLBACK
)
282 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
286 wxLogLastError(wxT("EnumerateLoadedModules"));
289 #endif // wxUSE_DBGHELP
295 WXHMODULE
wxDynamicLibrary::MSWGetModuleHandle(const char *name
, void *addr
)
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?)
302 // check if we can use GetModuleHandleEx
303 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCSTR
, HMODULE
*);
305 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
307 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
308 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
310 wxDynamicLibrary
dll(wxT("kernel32.dll"), wxDL_VERBATIM
);
311 s_pfnGetModuleHandleEx
=
312 (GetModuleHandleEx_t
)dll
.RawGetSymbol(wxT("GetModuleHandleExA"));
314 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
317 // get module handle from its address
318 if ( s_pfnGetModuleHandleEx
)
320 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
321 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
323 if ( s_pfnGetModuleHandleEx(6, (char *)addr
, &hmod
) && hmod
)
327 // Windows CE only has Unicode API, so even we have an ANSI string here, we
328 // still need to use GetModuleHandleW() there
330 return ::GetModuleHandleW(wxConvLibc
.cMB2WC(name
).data());
332 return ::GetModuleHandleA((char *)name
);
336 #endif // wxUSE_DYNLIB_CLASS