1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dlmsw.cpp
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"
30 #include "wx/filename.h"
32 const wxString
wxDynamicLibrary::ms_dllext(wxT(".dll"));
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // wrap some functions from version.dll: load them dynamically and provide a
43 // load version.dll and bind to its functions
46 // return the file version as string, e.g. "x.y.z.w"
47 wxString
GetFileVersion(const wxString
& filename
) const;
50 typedef DWORD (APIENTRY
*GetFileVersionInfoSize_t
)(PTSTR
, PDWORD
);
51 typedef BOOL (APIENTRY
*GetFileVersionInfo_t
)(PTSTR
, DWORD
, DWORD
, PVOID
);
52 typedef BOOL (APIENTRY
*VerQueryValue_t
)(const PVOID
, PTSTR
, PVOID
*, PUINT
);
54 #define DO_FOR_ALL_VER_FUNCS(what) \
55 what(GetFileVersionInfoSize); \
56 what(GetFileVersionInfo); \
59 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
61 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION
);
63 #undef DECLARE_VER_FUNCTION
66 wxDynamicLibrary m_dll
;
69 wxDECLARE_NO_COPY_CLASS(wxVersionDLL
);
72 // class used to create wxDynamicLibraryDetails objects
73 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
76 // type of parameters being passed to EnumModulesProc
77 struct EnumModulesProcParams
79 wxDynamicLibraryDetailsArray
*dlls
;
83 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
85 typedef DWORD64 DWORD_32_64
;
87 typedef DWORD DWORD_32_64
;
91 EnumModulesProc(PCSTR name
, DWORD_32_64 base
, ULONG size
, void *data
);
94 // ============================================================================
95 // wxVersionDLL implementation
96 // ============================================================================
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 wxVersionDLL::wxVersionDLL()
104 // don't give errors if DLL can't be loaded or used, we're prepared to
108 if ( m_dll
.Load(wxT("version.dll"), wxDL_VERBATIM
) )
110 // the functions we load have either 'A' or 'W' suffix depending on
111 // whether we're in ANSI or Unicode build
116 #endif // UNICODE/ANSI
118 #define LOAD_VER_FUNCTION(name) \
119 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
120 if ( !m_pfn ## name ) \
126 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
128 #undef LOAD_VER_FUNCTION
132 // ----------------------------------------------------------------------------
133 // wxVersionDLL operations
134 // ----------------------------------------------------------------------------
136 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
139 if ( m_dll
.IsLoaded() )
141 wxChar
*pc
= const_cast<wxChar
*>((const wxChar
*) filename
.t_str());
144 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
147 wxCharBuffer
buf(sizeVerInfo
);
148 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
152 if ( m_pfnVerQueryValue(buf
.data(),
153 const_cast<wxChar
*>(wxT("\\")),
157 VS_FIXEDFILEINFO
*info
= (VS_FIXEDFILEINFO
*)pVer
;
158 ver
.Printf(wxT("%d.%d.%d.%d"),
159 HIWORD(info
->dwFileVersionMS
),
160 LOWORD(info
->dwFileVersionMS
),
161 HIWORD(info
->dwFileVersionLS
),
162 LOWORD(info
->dwFileVersionLS
));
167 //else: we failed to load DLL, can't retrieve version info
172 // ============================================================================
173 // wxDynamicLibraryDetailsCreator implementation
174 // ============================================================================
178 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCSTR name
,
183 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
185 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
187 // fill in simple properties
188 details
->m_name
= wxString::FromAscii(name
);
189 details
->m_address
= wxUIntToPtr(base
);
190 details
->m_length
= size
;
192 // to get the version, we first need the full path
194 hmod
= wxDynamicLibrary::MSWGetModuleHandle(name
, details
->m_address
);
197 wxString fullname
= wxGetFullModuleName(hmod
);
198 if ( !fullname
.empty() )
200 details
->m_path
= fullname
;
201 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
205 params
->dlls
->Add(details
);
207 // continue enumeration (returning FALSE would have stopped it)
211 // ============================================================================
212 // wxDynamicLibrary implementation
213 // ============================================================================
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 wxDllType
wxDynamicLibrary::GetProgramHandle()
221 return (wxDllType
)::GetModuleHandle(NULL
);
224 // ----------------------------------------------------------------------------
225 // loading/unloading DLLs
226 // ----------------------------------------------------------------------------
229 #define MAX_PATH 260 // from VC++ headers
234 wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
236 if (flags
& wxDL_GET_LOADED
)
237 return ::GetModuleHandle(libname
.t_str());
239 // Explicitly look in the same path as where the main wx HINSTANCE module
240 // is located (usually the executable or the DLL that uses wx). Normally
241 // this is automatically part of the default search path but in some cases
242 // it may not be, such as when the wxPython extension modules need to load
243 // a DLL, but the intperpreter executable is located elsewhere. Doing
244 // this allows us to always be able to dynamically load a DLL that is
245 // located at the same place as the wx modules.
246 wxString modpath
, path
;
247 ::GetModuleFileName(wxGetInstance(),
248 wxStringBuffer(modpath
, MAX_PATH
+1),
251 wxFileName::SplitPath(modpath
, &path
, NULL
, NULL
);
252 ::SetDllDirectory(path
.t_str());
254 wxDllType handle
= ::LoadLibrary(libname
.t_str());
256 // reset the search path
257 ::SetDllDirectory(NULL
);
262 void wxDynamicLibrary::Unload(wxDllType handle
)
264 ::FreeLibrary(handle
);
268 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
270 return (void *)::GetProcAddress(handle
,
275 #endif // __WXWINCE__
279 // ----------------------------------------------------------------------------
280 // enumerating loaded DLLs
281 // ----------------------------------------------------------------------------
284 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
286 wxDynamicLibraryDetailsArray dlls
;
289 if ( wxDbgHelpDLL::Init() )
291 // prepare to use functions for version info extraction
294 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
296 params
.verDLL
= &verDLL
;
298 // Note that the cast of EnumModulesProc is needed because the type of
299 // PENUMLOADED_MODULES_CALLBACK changed: in old SDK versions its first
300 // argument was non-const PSTR while now it's PCSTR. By explicitly
301 // casting to whatever the currently used headers require we ensure
302 // that the code compilers in any case.
303 if ( !wxDbgHelpDLL::EnumerateLoadedModules
305 ::GetCurrentProcess(),
306 (PENUMLOADED_MODULES_CALLBACK
)
307 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
311 wxLogLastError(wxT("EnumerateLoadedModules"));
314 #endif // wxUSE_DBGHELP
320 WXHMODULE
wxDynamicLibrary::MSWGetModuleHandle(const char *name
, void *addr
)
322 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
323 // because the former works correctly for comctl32.dll while the latter
324 // returns NULL when comctl32.dll version 6 is used under XP (note that
325 // GetModuleHandleEx() is only available under XP and later, coincidence?)
327 // check if we can use GetModuleHandleEx
328 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCSTR
, HMODULE
*);
330 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
332 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
333 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
335 wxDynamicLibrary
dll(wxT("kernel32.dll"), wxDL_VERBATIM
);
336 s_pfnGetModuleHandleEx
=
337 (GetModuleHandleEx_t
)dll
.RawGetSymbol(wxT("GetModuleHandleExA"));
339 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
342 // get module handle from its address
343 if ( s_pfnGetModuleHandleEx
)
345 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
346 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
348 if ( s_pfnGetModuleHandleEx(6, (char *)addr
, &hmod
) && hmod
)
352 // Windows CE only has Unicode API, so even we have an ANSI string here, we
353 // still need to use GetModuleHandleW() there
355 return ::GetModuleHandleW(wxConvLibc
.cMB2WC(name
).data());
357 return ::GetModuleHandleA((char *)name
);
361 #endif // wxUSE_DYNLIB_CLASS