]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
fixed unused parameter warning
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130 1/////////////////////////////////////////////////////////////////////////////
55034339 2// Name: src/common/dynlib.cpp
7a4b9130
GL
3// Purpose: Dynamic library management
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 20/07/98
7// RCS-ID: $Id$
da55d064
VZ
8// Copyright: (c) 1998 Guilhem Lavaux
9// 2000-2005 Vadim Zeitlin
65571936 10// Licence: wxWindows licence
7a4b9130
GL
11/////////////////////////////////////////////////////////////////////////////
12
da55d064
VZ
13//FIXME: This class isn't really common at all, it should be moved into
14// platform dependent files (already done for Windows and Unix)
15
7b0bfbb2
VZ
16// ============================================================================
17// declarations
18// ============================================================================
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
0c32066b
JS
24#include "wx/wxprec.h"
25
2df7be7f 26#ifdef __BORLANDC__
e4db172a 27 #pragma hdrstop
2df7be7f 28#endif
f6bcfd97 29
1948bb32 30#if wxUSE_DYNLIB_CLASS
8a0d4cf6 31
7b0bfbb2 32#include "wx/dynlib.h"
e4db172a
WS
33
34#ifndef WX_PRECOMP
35 #include "wx/intl.h"
36 #include "wx/log.h"
670f9935 37 #include "wx/app.h"
de6185e2 38 #include "wx/utils.h"
e4db172a
WS
39#endif //WX_PRECOMP
40
7b0bfbb2 41#include "wx/filefn.h"
1948bb32 42#include "wx/filename.h" // for SplitPath()
8bb6b2c0 43#include "wx/platinfo.h"
7b0bfbb2 44
defbed48
VZ
45#include "wx/arrimpl.cpp"
46
76a5e5d2
SC
47#if defined(__WXMAC__)
48 #include "wx/mac/private.h"
49#endif
50
4115960d 51WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray)
7b0bfbb2 52
1948bb32
VS
53// ============================================================================
54// implementation
55// ============================================================================
846e1424 56
a585ca5c 57// ---------------------------------------------------------------------------
1948bb32 58// wxDynamicLibrary
a585ca5c 59// ---------------------------------------------------------------------------
7b0bfbb2 60
defbed48 61#if defined(__WXPM__) || defined(__EMX__)
00711afd 62 const wxString wxDynamicLibrary::ms_dllext(_T(".dll"));
f6bcfd97 63#endif
f6bcfd97 64
6d94009f 65// for MSW/Unix it is defined in platform-specific file
86a6ecff 66#if !(defined(__WXMSW__) || defined(__UNIX__)) || defined(__EMX__)
da55d064 67
1948bb32 68wxDllType wxDynamicLibrary::GetProgramHandle()
0868079c 69{
da55d064 70 wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform"));
7742efff 71 return 0;
0868079c
KB
72}
73
6d94009f
VZ
74#endif // __WXMSW__ || __UNIX__
75
da55d064 76
defbed48 77bool wxDynamicLibrary::Load(const wxString& libnameOrig, int flags)
a585ca5c 78{
1948bb32
VS
79 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
80
81 // add the proper extension for the DLL ourselves unless told not to
defbed48 82 wxString libname = libnameOrig;
1948bb32
VS
83 if ( !(flags & wxDL_VERBATIM) )
84 {
85 // and also check that the libname doesn't already have it
86 wxString ext;
87 wxFileName::SplitPath(libname, NULL, NULL, &ext);
88 if ( ext.empty() )
89 {
90 libname += GetDllExt();
91 }
92 }
7b0bfbb2 93
1948bb32
VS
94 // different ways to load a shared library
95 //
96 // FIXME: should go to the platform-specific files!
3a39a9da 97#if defined(__WXPM__) || defined(__EMX__)
670f9935 98 char err[256] = "";
1f3d9911 99 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
6c286397 100#else // this should be the only remaining branch eventually
da55d064 101 m_handle = RawLoad(libname, flags);
1948bb32
VS
102#endif
103
104 if ( m_handle == 0 )
7cc98b3e 105 {
da55d064
VZ
106#ifdef wxHAVE_DYNLIB_ERROR
107 Error();
1948bb32 108#else
da55d064 109 wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str());
0b9ab0bd 110#endif
7cc98b3e
VZ
111 }
112
1948bb32 113 return IsLoaded();
a585ca5c
KB
114}
115
da55d064
VZ
116// for MSW and Unix this is implemented in the platform-specific file
117//
118// TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp!
e9737ede 119#if (!defined(__WXMSW__) && !defined(__UNIX__)) || defined(__EMX__)
defbed48 120
9d033af9
VZ
121/* static */
122void wxDynamicLibrary::Unload(wxDllType handle)
752c7d6b 123{
55034339 124#if defined(__OS2__) || defined(__EMX__)
ff793cab 125 DosFreeModule( handle );
1948bb32 126#else
9d033af9 127 #error "runtime shared lib support not implemented"
1948bb32 128#endif
752c7d6b
KB
129}
130
da55d064 131#endif // !(__WXMSW__ || __UNIX__)
defbed48 132
a018a119 133void *wxDynamicLibrary::DoGetSymbol(const wxString &name, bool *success) const
a585ca5c 134{
1948bb32
VS
135 wxCHECK_MSG( IsLoaded(), NULL,
136 _T("Can't load symbol from unloaded library") );
137
0b9ab0bd 138 void *symbol = 0;
a585ca5c 139
999836aa 140 wxUnusedVar(symbol);
3a39a9da 141#if defined(__WXPM__) || defined(__EMX__)
1f3d9911 142 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
1c193821 143#else
da55d064 144 symbol = RawGetSymbol(m_handle, name);
1c193821 145#endif
0b9ab0bd 146
a018a119
VZ
147 if ( success )
148 *success = symbol != NULL;
149
150 return symbol;
151}
152
153void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const
154{
155 void *symbol = DoGetSymbol(name, success);
7b0bfbb2
VZ
156 if ( !symbol )
157 {
da55d064
VZ
158#ifdef wxHAVE_DYNLIB_ERROR
159 Error();
0b9ab0bd 160#else
7cc98b3e
VZ
161 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
162 name.c_str());
0b9ab0bd 163#endif
7b0bfbb2 164 }
0b9ab0bd 165
7b0bfbb2 166 return symbol;
7a4b9130 167}
5fccb5b4 168
defbed48
VZ
169// ----------------------------------------------------------------------------
170// informational methods
171// ----------------------------------------------------------------------------
172
1948bb32 173/*static*/
350fffae
VZ
174wxString
175wxDynamicLibrary::CanonicalizeName(const wxString& name,
da55d064 176 wxDynamicLibraryCategory cat)
7a4b9130 177{
350fffae
VZ
178 wxString nameCanonic;
179
87ec9b8f 180 // under Unix the library names usually start with "lib" prefix, add it
e9737ede 181#if defined(__UNIX__) && !defined(__EMX__)
350fffae
VZ
182 switch ( cat )
183 {
184 default:
185 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
186 // fall through
187
188 case wxDL_MODULE:
189 // don't do anything for modules, their names are arbitrary
190 break;
191
192 case wxDL_LIBRARY:
193 // library names should start with "lib" under Unix
194 nameCanonic = _T("lib");
195 break;
196 }
da55d064
VZ
197#else // !__UNIX__
198 wxUnusedVar(cat);
199#endif // __UNIX__/!__UNIX__
350fffae
VZ
200
201 nameCanonic << name << GetDllExt();
202 return nameCanonic;
7a4b9130 203}
8a0d4cf6 204
1948bb32
VS
205/*static*/
206wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
207 wxPluginCategory cat)
f11bdd03 208{
1948bb32
VS
209 wxString suffix;
210 if ( cat == wxDL_PLUGIN_GUI )
f11bdd03 211 {
449090b5 212 suffix = wxPlatformInfo::Get().GetPortIdShortName();
f11bdd03 213 }
1948bb32
VS
214#if wxUSE_UNICODE
215 suffix << _T('u');
216#endif
217#ifdef __WXDEBUG__
218 suffix << _T('d');
219#endif
f11bdd03 220
1948bb32
VS
221 if ( !suffix.empty() )
222 suffix = wxString(_T("_")) + suffix;
f11bdd03 223
3546ffae 224#define WXSTRINGIZE(x) #x
e9737ede 225#if defined(__UNIX__) && !defined(__EMX__)
1948bb32 226 #if (wxMINOR_VERSION % 2) == 0
3546ffae 227 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
1948bb32 228 #else
3546ffae 229 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
1948bb32
VS
230 #endif
231#else
232 #if (wxMINOR_VERSION % 2) == 0
3546ffae 233 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
1948bb32 234 #else
3546ffae 235 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
1948bb32
VS
236 #endif
237#endif
350fffae 238
1948bb32
VS
239 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
240 wxRELEASE_NUMBER));
241#undef wxDLLVER
3546ffae 242#undef WXSTRINGIZE
f11bdd03 243
2c02ec05
VS
244#ifdef __WINDOWS__
245 // Add compiler identification:
246 #if defined(__GNUG__)
247 suffix << _T("_gcc");
248 #elif defined(__VISUALC__)
249 suffix << _T("_vc");
250 #elif defined(__WATCOMC__)
251 suffix << _T("_wat");
252 #elif defined(__BORLANDC__)
253 suffix << _T("_bcc");
254 #endif
255#endif
256
1948bb32 257 return CanonicalizeName(name + suffix, wxDL_MODULE);
f11bdd03 258}
5fccb5b4 259
1948bb32
VS
260/*static*/
261wxString wxDynamicLibrary::GetPluginsDirectory()
f11bdd03 262{
1948bb32
VS
263#ifdef __UNIX__
264 wxString format = wxGetInstallPrefix();
cb979fac 265 wxString dir;
1948bb32
VS
266 format << wxFILE_SEP_PATH
267 << wxT("lib") << wxFILE_SEP_PATH
268 << wxT("wx") << wxFILE_SEP_PATH
cb979fac 269#if (wxMINOR_VERSION % 2) == 0
1948bb32 270 << wxT("%i.%i");
1948bb32 271 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
1948bb32 272#else
cb979fac
VS
273 << wxT("%i.%i.%i");
274 dir.Printf(format.c_str(),
275 wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER);
276#endif
277 return dir;
278
279#else // ! __UNIX__
1948bb32
VS
280 return wxEmptyString;
281#endif
f11bdd03
GD
282}
283
f11bdd03 284
1948bb32 285#endif // wxUSE_DYNLIB_CLASS