]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dlmsw.cpp
Add code showing stereo support to the OpenGL cube sample.
[wxWidgets.git] / src / msw / dlmsw.cpp
CommitLineData
defbed48 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/dlmsw.cpp
defbed48
VZ
3// Purpose: Win32-specific part of wxDynamicLibrary and related classes
4// Author: Vadim Zeitlin
bf9ce7b5 5// Modified by: Suzumizaki-kimitaka 2013-04-09
defbed48 6// Created: 2005-01-10 (partly extracted from common/dynlib.cpp)
defbed48
VZ
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"
932d0768 29#include "wx/filename.h"
defbed48 30
bf9ce7b5
VZ
31// defined for TDM's GCC/mingw32
32#ifndef PCTSTR
33#define PCTSTR LPCTSTR
34#endif
35
defbed48
VZ
36// ----------------------------------------------------------------------------
37// private classes
38// ----------------------------------------------------------------------------
39
40// wrap some functions from version.dll: load them dynamically and provide a
41// clean interface
42class wxVersionDLL
43{
44public:
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
51private:
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
c0c133e1 71 wxDECLARE_NO_COPY_CLASS(wxVersionDLL);
defbed48
VZ
72};
73
74// class used to create wxDynamicLibraryDetails objects
75class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
76{
77public:
78 // type of parameters being passed to EnumModulesProc
79 struct EnumModulesProcParams
80 {
81 wxDynamicLibraryDetailsArray *dlls;
82 wxVersionDLL *verDLL;
83 };
84
85 static BOOL CALLBACK
bf9ce7b5 86 EnumModulesProc(PCTSTR name, DWORD64 base, ULONG size, PVOID data);
defbed48
VZ
87};
88
defbed48
VZ
89// ============================================================================
90// wxVersionDLL implementation
91// ============================================================================
92
93// ----------------------------------------------------------------------------
94// loading
95// ----------------------------------------------------------------------------
96
97wxVersionDLL::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
9a83f860 103 if ( m_dll.Load(wxT("version.dll"), wxDL_VERBATIM) )
defbed48
VZ
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) \
bf9ce7b5 114 m_pfn ## name = (name ## _t)m_dll.GetSymbol(wxT(#name SUFFIX)); \
defbed48
VZ
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
131wxString wxVersionDLL::GetFileVersion(const wxString& filename) const
132{
133 wxString ver;
134 if ( m_dll.IsLoaded() )
135 {
5c33522f 136 wxChar *pc = const_cast<wxChar *>((const wxChar*) filename.t_str());
defbed48
VZ
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;
83a42976 147 if ( m_pfnVerQueryValue(buf.data(),
9a83f860 148 const_cast<wxChar *>(wxT("\\")),
83a42976
VZ
149 &pVer,
150 &sizeInfo) )
defbed48
VZ
151 {
152 VS_FIXEDFILEINFO *info = (VS_FIXEDFILEINFO *)pVer;
9a83f860 153 ver.Printf(wxT("%d.%d.%d.%d"),
defbed48
VZ
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 */
172BOOL CALLBACK
bf9ce7b5
VZ
173wxDynamicLibraryDetailsCreator::EnumModulesProc(PCTSTR name,
174 DWORD64 base,
defbed48 175 ULONG size,
bf9ce7b5 176 PVOID data)
defbed48
VZ
177{
178 EnumModulesProcParams *params = (EnumModulesProcParams *)data;
179
180 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
181
182 // fill in simple properties
bf9ce7b5 183#ifdef UNICODE
252e3b2c 184 details->m_name = name;
bf9ce7b5
VZ
185#else
186 details->m_name = wxString(name, wxConvLocal);
187#endif
3f49efdb 188 details->m_address = wxUIntToPtr(base);
defbed48
VZ
189 details->m_length = size;
190
191 // to get the version, we first need the full path
252e3b2c
VZ
192 const HMODULE hmod = wxDynamicLibrary::MSWGetModuleHandle
193 (
194 details->m_name,
195 details->m_address
196 );
defbed48
VZ
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
6d94009f
VZ
217// ----------------------------------------------------------------------------
218// misc functions
219// ----------------------------------------------------------------------------
220
221wxDllType wxDynamicLibrary::GetProgramHandle()
222{
223 return (wxDllType)::GetModuleHandle(NULL);
224}
225
defbed48
VZ
226// ----------------------------------------------------------------------------
227// loading/unloading DLLs
228// ----------------------------------------------------------------------------
229
932d0768
RD
230#ifndef MAX_PATH
231 #define MAX_PATH 260 // from VC++ headers
232#endif
233
defbed48 234/* static */
da55d064 235wxDllType
e2d4ce7d 236wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
defbed48 237{
932d0768
RD
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);
685a9fa6
DS
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
932d0768
RD
277 wxDllType handle = ::LoadLibrary(libname.t_str());
278
279 // reset the search path
685a9fa6
DS
280 if (s_pfnSetDllDirectory)
281 {
282 s_pfnSetDllDirectory(NULL);
283 }
284
932d0768 285 return handle;
defbed48
VZ
286}
287
288/* static */
289void wxDynamicLibrary::Unload(wxDllType handle)
290{
291 ::FreeLibrary(handle);
292}
293
294/* static */
295void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
296{
23ee4b48
WS
297 return (void *)::GetProcAddress(handle,
298#ifdef __WXWINCE__
299 name.c_str()
300#else
301 name.ToAscii()
302#endif // __WXWINCE__
303 );
defbed48
VZ
304}
305
306// ----------------------------------------------------------------------------
307// enumerating loaded DLLs
308// ----------------------------------------------------------------------------
309
310/* static */
311wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
312{
313 wxDynamicLibraryDetailsArray dlls;
314
94534bf8 315#if wxUSE_DBGHELP
defbed48
VZ
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
bf9ce7b5 325 if ( !wxDbgHelpDLL::EnumerateLoadedModulesT
defbed48
VZ
326 (
327 ::GetCurrentProcess(),
328 wxDynamicLibraryDetailsCreator::EnumModulesProc,
329 &params
330 ) )
331 {
bf9ce7b5 332 wxLogLastError(wxT("EnumerateLoadedModulesT"));
defbed48
VZ
333 }
334 }
94534bf8 335#endif // wxUSE_DBGHELP
defbed48
VZ
336
337 return dlls;
338}
339
c118a476 340/* static */
252e3b2c 341WXHMODULE wxDynamicLibrary::MSWGetModuleHandle(const wxString& name, void *addr)
c118a476
VZ
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
252e3b2c 349 typedef BOOL (WINAPI *GetModuleHandleEx_t)(DWORD, LPCTSTR, HMODULE *);
c118a476
VZ
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 =
252e3b2c 358 (GetModuleHandleEx_t)dll.GetSymbolAorW(wxT("GetModuleHandleEx"));
c118a476
VZ
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;
252e3b2c 369 if ( s_pfnGetModuleHandleEx(6, (LPCTSTR)addr, &hmod) && hmod )
c118a476
VZ
370 return hmod;
371 }
372
5ae1a669 373 return ::GetModuleHandle(name.t_str());
c118a476
VZ
374}
375
defbed48
VZ
376#endif // wxUSE_DYNLIB_CLASS
377