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 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // wrap some functions from version.dll: load them dynamically and provide a
41 // load version.dll and bind to its functions
44 // return the file version as string, e.g. "x.y.z.w"
45 wxString
GetFileVersion(const wxString
& filename
) const;
48 typedef DWORD (APIENTRY
*GetFileVersionInfoSize_t
)(PTSTR
, PDWORD
);
49 typedef BOOL (APIENTRY
*GetFileVersionInfo_t
)(PTSTR
, DWORD
, DWORD
, PVOID
);
50 typedef BOOL (APIENTRY
*VerQueryValue_t
)(const PVOID
, PTSTR
, PVOID
*, PUINT
);
52 #define DO_FOR_ALL_VER_FUNCS(what) \
53 what(GetFileVersionInfoSize); \
54 what(GetFileVersionInfo); \
57 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
59 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION
);
61 #undef DECLARE_VER_FUNCTION
64 wxDynamicLibrary m_dll
;
67 wxDECLARE_NO_COPY_CLASS(wxVersionDLL
);
70 // class used to create wxDynamicLibraryDetails objects
71 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
74 // type of parameters being passed to EnumModulesProc
75 struct EnumModulesProcParams
77 wxDynamicLibraryDetailsArray
*dlls
;
81 // TODO: fix EnumerateLoadedModules() to use EnumerateLoadedModules64()
83 typedef DWORD64 DWORD_32_64
;
85 typedef DWORD DWORD_32_64
;
89 EnumModulesProc(PCSTR name
, DWORD_32_64 base
, ULONG size
, void *data
);
92 // ============================================================================
93 // wxVersionDLL implementation
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 wxVersionDLL::wxVersionDLL()
102 // don't give errors if DLL can't be loaded or used, we're prepared to
106 if ( m_dll
.Load(wxT("version.dll"), wxDL_VERBATIM
) )
108 // the functions we load have either 'A' or 'W' suffix depending on
109 // whether we're in ANSI or Unicode build
114 #endif // UNICODE/ANSI
116 #define LOAD_VER_FUNCTION(name) \
117 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
118 if ( !m_pfn ## name ) \
124 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
126 #undef LOAD_VER_FUNCTION
130 // ----------------------------------------------------------------------------
131 // wxVersionDLL operations
132 // ----------------------------------------------------------------------------
134 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
137 if ( m_dll
.IsLoaded() )
139 wxChar
*pc
= const_cast<wxChar
*>((const wxChar
*) filename
.t_str());
142 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
145 wxCharBuffer
buf(sizeVerInfo
);
146 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
150 if ( m_pfnVerQueryValue(buf
.data(),
151 const_cast<wxChar
*>(wxT("\\")),
155 VS_FIXEDFILEINFO
*info
= (VS_FIXEDFILEINFO
*)pVer
;
156 ver
.Printf(wxT("%d.%d.%d.%d"),
157 HIWORD(info
->dwFileVersionMS
),
158 LOWORD(info
->dwFileVersionMS
),
159 HIWORD(info
->dwFileVersionLS
),
160 LOWORD(info
->dwFileVersionLS
));
165 //else: we failed to load DLL, can't retrieve version info
170 // ============================================================================
171 // wxDynamicLibraryDetailsCreator implementation
172 // ============================================================================
176 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCSTR name
,
181 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
183 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
185 // fill in simple properties
186 details
->m_name
= name
;
187 details
->m_address
= wxUIntToPtr(base
);
188 details
->m_length
= size
;
190 // to get the version, we first need the full path
191 const HMODULE hmod
= wxDynamicLibrary::MSWGetModuleHandle
198 wxString fullname
= wxGetFullModuleName(hmod
);
199 if ( !fullname
.empty() )
201 details
->m_path
= fullname
;
202 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
206 params
->dlls
->Add(details
);
208 // continue enumeration (returning FALSE would have stopped it)
212 // ============================================================================
213 // wxDynamicLibrary implementation
214 // ============================================================================
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 wxDllType
wxDynamicLibrary::GetProgramHandle()
222 return (wxDllType
)::GetModuleHandle(NULL
);
225 // ----------------------------------------------------------------------------
226 // loading/unloading DLLs
227 // ----------------------------------------------------------------------------
230 #define MAX_PATH 260 // from VC++ headers
235 wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
237 if (flags
& wxDL_GET_LOADED
)
238 return ::GetModuleHandle(libname
.t_str());
240 // Explicitly look in the same path as where the main wx HINSTANCE module
241 // is located (usually the executable or the DLL that uses wx). Normally
242 // this is automatically part of the default search path but in some cases
243 // it may not be, such as when the wxPython extension modules need to load
244 // a DLL, but the intperpreter executable is located elsewhere. Doing
245 // this allows us to always be able to dynamically load a DLL that is
246 // located at the same place as the wx modules.
247 wxString modpath
, path
;
248 ::GetModuleFileName(wxGetInstance(),
249 wxStringBuffer(modpath
, MAX_PATH
+1),
252 wxFileName::SplitPath(modpath
, &path
, NULL
, NULL
);
254 typedef BOOL (WINAPI
*SetDllDirectory_t
)(LPCTSTR lpPathName
);
256 static SetDllDirectory_t s_pfnSetDllDirectory
= (SetDllDirectory_t
) -1;
258 if ( s_pfnSetDllDirectory
== (SetDllDirectory_t
) -1 )
261 Should wxLoadedDLL ever not be used here (or rather, the
262 wxDL_GET_LOADED flag isn't used), infinite recursion will take
263 place (unless s_pfnSetDllDirectory is set to NULL here right
264 before loading the DLL).
266 wxLoadedDLL
dllKernel("kernel32.dll");
268 wxDL_INIT_FUNC_AW(s_pfn
, SetDllDirectory
, dllKernel
);
271 if (s_pfnSetDllDirectory
)
273 s_pfnSetDllDirectory(path
.t_str());
276 wxDllType handle
= ::LoadLibrary(libname
.t_str());
278 // reset the search path
279 if (s_pfnSetDllDirectory
)
281 s_pfnSetDllDirectory(NULL
);
288 void wxDynamicLibrary::Unload(wxDllType handle
)
290 ::FreeLibrary(handle
);
294 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
296 return (void *)::GetProcAddress(handle
,
301 #endif // __WXWINCE__
305 // ----------------------------------------------------------------------------
306 // enumerating loaded DLLs
307 // ----------------------------------------------------------------------------
310 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
312 wxDynamicLibraryDetailsArray dlls
;
315 if ( wxDbgHelpDLL::Init() )
317 // prepare to use functions for version info extraction
320 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
322 params
.verDLL
= &verDLL
;
324 // Note that the cast of EnumModulesProc is needed because the type of
325 // PENUMLOADED_MODULES_CALLBACK changed: in old SDK versions its first
326 // argument was non-const PSTR while now it's PCSTR. By explicitly
327 // casting to whatever the currently used headers require we ensure
328 // that the code compilers in any case.
329 if ( !wxDbgHelpDLL::EnumerateLoadedModules
331 ::GetCurrentProcess(),
332 (PENUMLOADED_MODULES_CALLBACK
)
333 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
337 wxLogLastError(wxT("EnumerateLoadedModules"));
340 #endif // wxUSE_DBGHELP
346 WXHMODULE
wxDynamicLibrary::MSWGetModuleHandle(const wxString
& name
, void *addr
)
348 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
349 // because the former works correctly for comctl32.dll while the latter
350 // returns NULL when comctl32.dll version 6 is used under XP (note that
351 // GetModuleHandleEx() is only available under XP and later, coincidence?)
353 // check if we can use GetModuleHandleEx
354 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCTSTR
, HMODULE
*);
356 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
358 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
359 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
361 wxDynamicLibrary
dll(wxT("kernel32.dll"), wxDL_VERBATIM
);
362 s_pfnGetModuleHandleEx
=
363 (GetModuleHandleEx_t
)dll
.GetSymbolAorW(wxT("GetModuleHandleEx"));
365 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
368 // get module handle from its address
369 if ( s_pfnGetModuleHandleEx
)
371 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
372 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
374 if ( s_pfnGetModuleHandleEx(6, (LPCTSTR
)addr
, &hmod
) && hmod
)
378 return ::GetModuleHandle(name
.t_str());
381 #endif // wxUSE_DYNLIB_CLASS