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)
7 // Copyright: (c) 1998-2005 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #if wxUSE_DYNLIB_CLASS
27 #include "wx/msw/private.h"
28 #include "wx/msw/debughlp.h"
29 #include "wx/filename.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // wrap some functions from version.dll: load them dynamically and provide a
40 // load version.dll and bind to its functions
43 // return the file version as string, e.g. "x.y.z.w"
44 wxString
GetFileVersion(const wxString
& filename
) const;
47 typedef DWORD (APIENTRY
*GetFileVersionInfoSize_t
)(PTSTR
, PDWORD
);
48 typedef BOOL (APIENTRY
*GetFileVersionInfo_t
)(PTSTR
, DWORD
, DWORD
, PVOID
);
49 typedef BOOL (APIENTRY
*VerQueryValue_t
)(const PVOID
, PTSTR
, PVOID
*, PUINT
);
51 #define DO_FOR_ALL_VER_FUNCS(what) \
52 what(GetFileVersionInfoSize); \
53 what(GetFileVersionInfo); \
56 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
58 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION
);
60 #undef DECLARE_VER_FUNCTION
63 wxDynamicLibrary m_dll
;
66 wxDECLARE_NO_COPY_CLASS(wxVersionDLL
);
69 // class used to create wxDynamicLibraryDetails objects
70 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
73 // type of parameters being passed to EnumModulesProc
74 struct EnumModulesProcParams
76 wxDynamicLibraryDetailsArray
*dlls
;
80 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
82 typedef DWORD64 DWORD_32_64
;
84 typedef DWORD DWORD_32_64
;
88 EnumModulesProc(PCSTR name
, DWORD_32_64 base
, ULONG size
, void *data
);
91 // ============================================================================
92 // wxVersionDLL implementation
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 wxVersionDLL::wxVersionDLL()
101 // don't give errors if DLL can't be loaded or used, we're prepared to
105 if ( m_dll
.Load(wxT("version.dll"), wxDL_VERBATIM
) )
107 // the functions we load have either 'A' or 'W' suffix depending on
108 // whether we're in ANSI or Unicode build
113 #endif // UNICODE/ANSI
115 #define LOAD_VER_FUNCTION(name) \
116 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
117 if ( !m_pfn ## name ) \
123 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
125 #undef LOAD_VER_FUNCTION
129 // ----------------------------------------------------------------------------
130 // wxVersionDLL operations
131 // ----------------------------------------------------------------------------
133 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
136 if ( m_dll
.IsLoaded() )
138 wxChar
*pc
= const_cast<wxChar
*>((const wxChar
*) filename
.t_str());
141 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
144 wxCharBuffer
buf(sizeVerInfo
);
145 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
149 if ( m_pfnVerQueryValue(buf
.data(),
150 const_cast<wxChar
*>(wxT("\\")),
154 VS_FIXEDFILEINFO
*info
= (VS_FIXEDFILEINFO
*)pVer
;
155 ver
.Printf(wxT("%d.%d.%d.%d"),
156 HIWORD(info
->dwFileVersionMS
),
157 LOWORD(info
->dwFileVersionMS
),
158 HIWORD(info
->dwFileVersionLS
),
159 LOWORD(info
->dwFileVersionLS
));
164 //else: we failed to load DLL, can't retrieve version info
169 // ============================================================================
170 // wxDynamicLibraryDetailsCreator implementation
171 // ============================================================================
175 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCSTR name
,
180 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
182 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
184 // fill in simple properties
185 details
->m_name
= name
;
186 details
->m_address
= wxUIntToPtr(base
);
187 details
->m_length
= size
;
189 // to get the version, we first need the full path
190 const HMODULE hmod
= wxDynamicLibrary::MSWGetModuleHandle
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
);
253 typedef BOOL (WINAPI
*SetDllDirectory_t
)(LPCTSTR lpPathName
);
255 static SetDllDirectory_t s_pfnSetDllDirectory
= (SetDllDirectory_t
) -1;
257 if ( s_pfnSetDllDirectory
== (SetDllDirectory_t
) -1 )
260 Should wxLoadedDLL ever not be used here (or rather, the
261 wxDL_GET_LOADED flag isn't used), infinite recursion will take
262 place (unless s_pfnSetDllDirectory is set to NULL here right
263 before loading the DLL).
265 wxLoadedDLL
dllKernel("kernel32.dll");
267 wxDL_INIT_FUNC_AW(s_pfn
, SetDllDirectory
, dllKernel
);
270 if (s_pfnSetDllDirectory
)
272 s_pfnSetDllDirectory(path
.t_str());
275 wxDllType handle
= ::LoadLibrary(libname
.t_str());
277 // reset the search path
278 if (s_pfnSetDllDirectory
)
280 s_pfnSetDllDirectory(NULL
);
287 void wxDynamicLibrary::Unload(wxDllType handle
)
289 ::FreeLibrary(handle
);
293 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
295 return (void *)::GetProcAddress(handle
,
300 #endif // __WXWINCE__
304 // ----------------------------------------------------------------------------
305 // enumerating loaded DLLs
306 // ----------------------------------------------------------------------------
309 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
311 wxDynamicLibraryDetailsArray dlls
;
314 if ( wxDbgHelpDLL::Init() )
316 // prepare to use functions for version info extraction
319 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
321 params
.verDLL
= &verDLL
;
323 // Note that the cast of EnumModulesProc is needed because the type of
324 // PENUMLOADED_MODULES_CALLBACK changed: in old SDK versions its first
325 // argument was non-const PSTR while now it's PCSTR. By explicitly
326 // casting to whatever the currently used headers require we ensure
327 // that the code compilers in any case.
328 if ( !wxDbgHelpDLL::EnumerateLoadedModules
330 ::GetCurrentProcess(),
331 (PENUMLOADED_MODULES_CALLBACK
)
332 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
336 wxLogLastError(wxT("EnumerateLoadedModules"));
339 #endif // wxUSE_DBGHELP
345 WXHMODULE
wxDynamicLibrary::MSWGetModuleHandle(const wxString
& name
, void *addr
)
347 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
348 // because the former works correctly for comctl32.dll while the latter
349 // returns NULL when comctl32.dll version 6 is used under XP (note that
350 // GetModuleHandleEx() is only available under XP and later, coincidence?)
352 // check if we can use GetModuleHandleEx
353 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCTSTR
, HMODULE
*);
355 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
357 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
358 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
360 wxDynamicLibrary
dll(wxT("kernel32.dll"), wxDL_VERBATIM
);
361 s_pfnGetModuleHandleEx
=
362 (GetModuleHandleEx_t
)dll
.GetSymbolAorW(wxT("GetModuleHandleEx"));
364 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
367 // get module handle from its address
368 if ( s_pfnGetModuleHandleEx
)
370 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
371 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
373 if ( s_pfnGetModuleHandleEx(6, (LPCTSTR
)addr
, &hmod
) && hmod
)
377 return ::GetModuleHandle(name
.t_str());
380 #endif // wxUSE_DYNLIB_CLASS