1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dlmsw.cpp
3 // Purpose: Win32-specific part of wxDynamicLibrary and related classes
4 // Author: Vadim Zeitlin
5 // Modified by: Suzumizaki-kimitaka 2013-04-09
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 // defined for TDM's GCC/mingw32
33 #define PCTSTR LPCTSTR
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // wrap some functions from version.dll: load them dynamically and provide a
45 // load version.dll and bind to its functions
48 // return the file version as string, e.g. "x.y.z.w"
49 wxString
GetFileVersion(const wxString
& filename
) const;
52 typedef DWORD (APIENTRY
*GetFileVersionInfoSize_t
)(PTSTR
, PDWORD
);
53 typedef BOOL (APIENTRY
*GetFileVersionInfo_t
)(PTSTR
, DWORD
, DWORD
, PVOID
);
54 typedef BOOL (APIENTRY
*VerQueryValue_t
)(const PVOID
, PTSTR
, PVOID
*, PUINT
);
56 #define DO_FOR_ALL_VER_FUNCS(what) \
57 what(GetFileVersionInfoSize); \
58 what(GetFileVersionInfo); \
61 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
63 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION
);
65 #undef DECLARE_VER_FUNCTION
68 wxDynamicLibrary m_dll
;
71 wxDECLARE_NO_COPY_CLASS(wxVersionDLL
);
74 // class used to create wxDynamicLibraryDetails objects
75 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
78 // type of parameters being passed to EnumModulesProc
79 struct EnumModulesProcParams
81 wxDynamicLibraryDetailsArray
*dlls
;
86 EnumModulesProc(PCTSTR name
, DWORD64 base
, ULONG size
, PVOID data
);
89 // ============================================================================
90 // wxVersionDLL implementation
91 // ============================================================================
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 wxVersionDLL::wxVersionDLL()
99 // don't give errors if DLL can't be loaded or used, we're prepared to
103 if ( m_dll
.Load(wxT("version.dll"), wxDL_VERBATIM
) )
105 // the functions we load have either 'A' or 'W' suffix depending on
106 // whether we're in ANSI or Unicode build
111 #endif // UNICODE/ANSI
113 #define LOAD_VER_FUNCTION(name) \
114 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
115 if ( !m_pfn ## name ) \
121 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION
);
123 #undef LOAD_VER_FUNCTION
127 // ----------------------------------------------------------------------------
128 // wxVersionDLL operations
129 // ----------------------------------------------------------------------------
131 wxString
wxVersionDLL::GetFileVersion(const wxString
& filename
) const
134 if ( m_dll
.IsLoaded() )
136 wxChar
*pc
= const_cast<wxChar
*>((const wxChar
*) filename
.t_str());
139 DWORD sizeVerInfo
= m_pfnGetFileVersionInfoSize(pc
, &dummy
);
142 wxCharBuffer
buf(sizeVerInfo
);
143 if ( m_pfnGetFileVersionInfo(pc
, 0, sizeVerInfo
, buf
.data()) )
147 if ( m_pfnVerQueryValue(buf
.data(),
148 const_cast<wxChar
*>(wxT("\\")),
152 VS_FIXEDFILEINFO
*info
= (VS_FIXEDFILEINFO
*)pVer
;
153 ver
.Printf(wxT("%d.%d.%d.%d"),
154 HIWORD(info
->dwFileVersionMS
),
155 LOWORD(info
->dwFileVersionMS
),
156 HIWORD(info
->dwFileVersionLS
),
157 LOWORD(info
->dwFileVersionLS
));
162 //else: we failed to load DLL, can't retrieve version info
167 // ============================================================================
168 // wxDynamicLibraryDetailsCreator implementation
169 // ============================================================================
173 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCTSTR name
,
178 EnumModulesProcParams
*params
= (EnumModulesProcParams
*)data
;
180 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
182 // fill in simple properties
184 details
->m_name
= name
;
186 details
->m_name
= wxString(name
, wxConvLocal
);
188 details
->m_address
= wxUIntToPtr(base
);
189 details
->m_length
= size
;
191 // to get the version, we first need the full path
192 const HMODULE hmod
= wxDynamicLibrary::MSWGetModuleHandle
199 wxString fullname
= wxGetFullModuleName(hmod
);
200 if ( !fullname
.empty() )
202 details
->m_path
= fullname
;
203 details
->m_version
= params
->verDLL
->GetFileVersion(fullname
);
207 params
->dlls
->Add(details
);
209 // continue enumeration (returning FALSE would have stopped it)
213 // ============================================================================
214 // wxDynamicLibrary implementation
215 // ============================================================================
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 wxDllType
wxDynamicLibrary::GetProgramHandle()
223 return (wxDllType
)::GetModuleHandle(NULL
);
226 // ----------------------------------------------------------------------------
227 // loading/unloading DLLs
228 // ----------------------------------------------------------------------------
231 #define MAX_PATH 260 // from VC++ headers
236 wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
238 if (flags
& wxDL_GET_LOADED
)
239 return ::GetModuleHandle(libname
.t_str());
241 // Explicitly look in the same path as where the main wx HINSTANCE module
242 // is located (usually the executable or the DLL that uses wx). Normally
243 // this is automatically part of the default search path but in some cases
244 // it may not be, such as when the wxPython extension modules need to load
245 // a DLL, but the intperpreter executable is located elsewhere. Doing
246 // this allows us to always be able to dynamically load a DLL that is
247 // located at the same place as the wx modules.
248 wxString modpath
, path
;
249 ::GetModuleFileName(wxGetInstance(),
250 wxStringBuffer(modpath
, MAX_PATH
+1),
253 wxFileName::SplitPath(modpath
, &path
, NULL
, NULL
);
255 typedef BOOL (WINAPI
*SetDllDirectory_t
)(LPCTSTR lpPathName
);
257 static SetDllDirectory_t s_pfnSetDllDirectory
= (SetDllDirectory_t
) -1;
259 if ( s_pfnSetDllDirectory
== (SetDllDirectory_t
) -1 )
262 Should wxLoadedDLL ever not be used here (or rather, the
263 wxDL_GET_LOADED flag isn't used), infinite recursion will take
264 place (unless s_pfnSetDllDirectory is set to NULL here right
265 before loading the DLL).
267 wxLoadedDLL
dllKernel("kernel32.dll");
269 wxDL_INIT_FUNC_AW(s_pfn
, SetDllDirectory
, dllKernel
);
272 if (s_pfnSetDllDirectory
)
274 s_pfnSetDllDirectory(path
.t_str());
277 wxDllType handle
= ::LoadLibrary(libname
.t_str());
279 // reset the search path
280 if (s_pfnSetDllDirectory
)
282 s_pfnSetDllDirectory(NULL
);
289 void wxDynamicLibrary::Unload(wxDllType handle
)
291 ::FreeLibrary(handle
);
295 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
297 return (void *)::GetProcAddress(handle
,
302 #endif // __WXWINCE__
306 // ----------------------------------------------------------------------------
307 // enumerating loaded DLLs
308 // ----------------------------------------------------------------------------
311 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
313 wxDynamicLibraryDetailsArray dlls
;
316 if ( wxDbgHelpDLL::Init() )
318 // prepare to use functions for version info extraction
321 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params
;
323 params
.verDLL
= &verDLL
;
325 if ( !wxDbgHelpDLL::EnumerateLoadedModulesT
327 ::GetCurrentProcess(),
328 wxDynamicLibraryDetailsCreator::EnumModulesProc
,
332 wxLogLastError(wxT("EnumerateLoadedModulesT"));
335 #endif // wxUSE_DBGHELP
341 WXHMODULE
wxDynamicLibrary::MSWGetModuleHandle(const wxString
& name
, void *addr
)
343 // we want to use GetModuleHandleEx() instead of usual GetModuleHandle()
344 // because the former works correctly for comctl32.dll while the latter
345 // returns NULL when comctl32.dll version 6 is used under XP (note that
346 // GetModuleHandleEx() is only available under XP and later, coincidence?)
348 // check if we can use GetModuleHandleEx
349 typedef BOOL (WINAPI
*GetModuleHandleEx_t
)(DWORD
, LPCTSTR
, HMODULE
*);
351 static const GetModuleHandleEx_t INVALID_FUNC_PTR
= (GetModuleHandleEx_t
)-1;
353 static GetModuleHandleEx_t s_pfnGetModuleHandleEx
= INVALID_FUNC_PTR
;
354 if ( s_pfnGetModuleHandleEx
== INVALID_FUNC_PTR
)
356 wxDynamicLibrary
dll(wxT("kernel32.dll"), wxDL_VERBATIM
);
357 s_pfnGetModuleHandleEx
=
358 (GetModuleHandleEx_t
)dll
.GetSymbolAorW(wxT("GetModuleHandleEx"));
360 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
363 // get module handle from its address
364 if ( s_pfnGetModuleHandleEx
)
366 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
367 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
369 if ( s_pfnGetModuleHandleEx(6, (LPCTSTR
)addr
, &hmod
) && hmod
)
373 return ::GetModuleHandle(name
.t_str());
376 #endif // wxUSE_DYNLIB_CLASS