]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dlmsw.cpp
Show full screen windows with WS_POPUP style in wxMSW.
[wxWidgets.git] / src / msw / dlmsw.cpp
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 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_DYNLIB_CLASS
26
27 #include "wx/msw/private.h"
28 #include "wx/msw/debughlp.h"
29 #include "wx/filename.h"
30
31 // defined for TDM's GCC/mingw32
32 #ifndef PCTSTR
33 #define PCTSTR LPCTSTR
34 #endif
35
36 // ----------------------------------------------------------------------------
37 // private classes
38 // ----------------------------------------------------------------------------
39
40 // wrap some functions from version.dll: load them dynamically and provide a
41 // clean interface
42 class wxVersionDLL
43 {
44 public:
45 // load version.dll and bind to its functions
46 wxVersionDLL();
47
48 // return the file version as string, e.g. "x.y.z.w"
49 wxString GetFileVersion(const wxString& filename) const;
50
51 private:
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);
55
56 #define DO_FOR_ALL_VER_FUNCS(what) \
57 what(GetFileVersionInfoSize); \
58 what(GetFileVersionInfo); \
59 what(VerQueryValue)
60
61 #define DECLARE_VER_FUNCTION(func) func ## _t m_pfn ## func
62
63 DO_FOR_ALL_VER_FUNCS(DECLARE_VER_FUNCTION);
64
65 #undef DECLARE_VER_FUNCTION
66
67
68 wxDynamicLibrary m_dll;
69
70
71 wxDECLARE_NO_COPY_CLASS(wxVersionDLL);
72 };
73
74 // class used to create wxDynamicLibraryDetails objects
75 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
76 {
77 public:
78 // type of parameters being passed to EnumModulesProc
79 struct EnumModulesProcParams
80 {
81 wxDynamicLibraryDetailsArray *dlls;
82 wxVersionDLL *verDLL;
83 };
84
85 static BOOL CALLBACK
86 EnumModulesProc(PCTSTR name, DWORD64 base, ULONG size, PVOID data);
87 };
88
89 // ============================================================================
90 // wxVersionDLL implementation
91 // ============================================================================
92
93 // ----------------------------------------------------------------------------
94 // loading
95 // ----------------------------------------------------------------------------
96
97 wxVersionDLL::wxVersionDLL()
98 {
99 // don't give errors if DLL can't be loaded or used, we're prepared to
100 // handle it
101 wxLogNull noLog;
102
103 if ( m_dll.Load(wxT("version.dll"), wxDL_VERBATIM) )
104 {
105 // the functions we load have either 'A' or 'W' suffix depending on
106 // whether we're in ANSI or Unicode build
107 #ifdef UNICODE
108 #define SUFFIX L"W"
109 #else // ANSI
110 #define SUFFIX "A"
111 #endif // UNICODE/ANSI
112
113 #define LOAD_VER_FUNCTION(name) \
114 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
115 if ( !m_pfn ## name ) \
116 { \
117 m_dll.Unload(); \
118 return; \
119 }
120
121 DO_FOR_ALL_VER_FUNCS(LOAD_VER_FUNCTION);
122
123 #undef LOAD_VER_FUNCTION
124 }
125 }
126
127 // ----------------------------------------------------------------------------
128 // wxVersionDLL operations
129 // ----------------------------------------------------------------------------
130
131 wxString wxVersionDLL::GetFileVersion(const wxString& filename) const
132 {
133 wxString ver;
134 if ( m_dll.IsLoaded() )
135 {
136 wxChar *pc = const_cast<wxChar *>((const wxChar*) filename.t_str());
137
138 DWORD dummy;
139 DWORD sizeVerInfo = m_pfnGetFileVersionInfoSize(pc, &dummy);
140 if ( sizeVerInfo )
141 {
142 wxCharBuffer buf(sizeVerInfo);
143 if ( m_pfnGetFileVersionInfo(pc, 0, sizeVerInfo, buf.data()) )
144 {
145 void *pVer;
146 UINT sizeInfo;
147 if ( m_pfnVerQueryValue(buf.data(),
148 const_cast<wxChar *>(wxT("\\")),
149 &pVer,
150 &sizeInfo) )
151 {
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));
158 }
159 }
160 }
161 }
162 //else: we failed to load DLL, can't retrieve version info
163
164 return ver;
165 }
166
167 // ============================================================================
168 // wxDynamicLibraryDetailsCreator implementation
169 // ============================================================================
170
171 /* static */
172 BOOL CALLBACK
173 wxDynamicLibraryDetailsCreator::EnumModulesProc(PCTSTR name,
174 DWORD64 base,
175 ULONG size,
176 PVOID data)
177 {
178 EnumModulesProcParams *params = (EnumModulesProcParams *)data;
179
180 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
181
182 // fill in simple properties
183 #ifdef UNICODE
184 details->m_name = name;
185 #else
186 details->m_name = wxString(name, wxConvLocal);
187 #endif
188 details->m_address = wxUIntToPtr(base);
189 details->m_length = size;
190
191 // to get the version, we first need the full path
192 const HMODULE hmod = wxDynamicLibrary::MSWGetModuleHandle
193 (
194 details->m_name,
195 details->m_address
196 );
197 if ( hmod )
198 {
199 wxString fullname = wxGetFullModuleName(hmod);
200 if ( !fullname.empty() )
201 {
202 details->m_path = fullname;
203 details->m_version = params->verDLL->GetFileVersion(fullname);
204 }
205 }
206
207 params->dlls->Add(details);
208
209 // continue enumeration (returning FALSE would have stopped it)
210 return TRUE;
211 }
212
213 // ============================================================================
214 // wxDynamicLibrary implementation
215 // ============================================================================
216
217 // ----------------------------------------------------------------------------
218 // misc functions
219 // ----------------------------------------------------------------------------
220
221 wxDllType wxDynamicLibrary::GetProgramHandle()
222 {
223 return (wxDllType)::GetModuleHandle(NULL);
224 }
225
226 // ----------------------------------------------------------------------------
227 // loading/unloading DLLs
228 // ----------------------------------------------------------------------------
229
230 #ifndef MAX_PATH
231 #define MAX_PATH 260 // from VC++ headers
232 #endif
233
234 /* static */
235 wxDllType
236 wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
237 {
238 if (flags & wxDL_GET_LOADED)
239 return ::GetModuleHandle(libname.t_str());
240
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),
251 MAX_PATH);
252
253 wxFileName::SplitPath(modpath, &path, NULL, NULL);
254
255 typedef BOOL (WINAPI *SetDllDirectory_t)(LPCTSTR lpPathName);
256
257 static SetDllDirectory_t s_pfnSetDllDirectory = (SetDllDirectory_t) -1;
258
259 if ( s_pfnSetDllDirectory == (SetDllDirectory_t) -1 )
260 {
261 /*
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).
266 */
267 wxLoadedDLL dllKernel("kernel32.dll");
268
269 wxDL_INIT_FUNC_AW(s_pfn, SetDllDirectory, dllKernel);
270 }
271
272 if (s_pfnSetDllDirectory)
273 {
274 s_pfnSetDllDirectory(path.t_str());
275 }
276
277 wxDllType handle = ::LoadLibrary(libname.t_str());
278
279 // reset the search path
280 if (s_pfnSetDllDirectory)
281 {
282 s_pfnSetDllDirectory(NULL);
283 }
284
285 return handle;
286 }
287
288 /* static */
289 void wxDynamicLibrary::Unload(wxDllType handle)
290 {
291 ::FreeLibrary(handle);
292 }
293
294 /* static */
295 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
296 {
297 return (void *)::GetProcAddress(handle,
298 #ifdef __WXWINCE__
299 name.c_str()
300 #else
301 name.ToAscii()
302 #endif // __WXWINCE__
303 );
304 }
305
306 // ----------------------------------------------------------------------------
307 // enumerating loaded DLLs
308 // ----------------------------------------------------------------------------
309
310 /* static */
311 wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
312 {
313 wxDynamicLibraryDetailsArray dlls;
314
315 #if wxUSE_DBGHELP
316 if ( wxDbgHelpDLL::Init() )
317 {
318 // prepare to use functions for version info extraction
319 wxVersionDLL verDLL;
320
321 wxDynamicLibraryDetailsCreator::EnumModulesProcParams params;
322 params.dlls = &dlls;
323 params.verDLL = &verDLL;
324
325 if ( !wxDbgHelpDLL::EnumerateLoadedModulesT
326 (
327 ::GetCurrentProcess(),
328 wxDynamicLibraryDetailsCreator::EnumModulesProc,
329 &params
330 ) )
331 {
332 wxLogLastError(wxT("EnumerateLoadedModulesT"));
333 }
334 }
335 #endif // wxUSE_DBGHELP
336
337 return dlls;
338 }
339
340 /* static */
341 WXHMODULE wxDynamicLibrary::MSWGetModuleHandle(const wxString& name, void *addr)
342 {
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?)
347
348 // check if we can use GetModuleHandleEx
349 typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCTSTR, HMODULE *);
350
351 static const GetModuleHandleEx_t INVALID_FUNC_PTR = (GetModuleHandleEx_t)-1;
352
353 static GetModuleHandleEx_t s_pfnGetModuleHandleEx = INVALID_FUNC_PTR;
354 if ( s_pfnGetModuleHandleEx == INVALID_FUNC_PTR )
355 {
356 wxDynamicLibrary dll(wxT("kernel32.dll"), wxDL_VERBATIM);
357 s_pfnGetModuleHandleEx =
358 (GetModuleHandleEx_t)dll.GetSymbolAorW(wxT("GetModuleHandleEx"));
359
360 // dll object can be destroyed, kernel32.dll won't be unloaded anyhow
361 }
362
363 // get module handle from its address
364 if ( s_pfnGetModuleHandleEx )
365 {
366 // flags are GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
367 // GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
368 HMODULE hmod;
369 if ( s_pfnGetModuleHandleEx(6, (LPCTSTR)addr, &hmod) && hmod )
370 return hmod;
371 }
372
373 return ::GetModuleHandle(name.t_str());
374 }
375
376 #endif // wxUSE_DYNLIB_CLASS
377