]>
Commit | Line | Data |
---|---|---|
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 | ||
4115960d | 47 | WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray) |
7b0bfbb2 | 48 | |
1948bb32 VS |
49 | // ============================================================================ |
50 | // implementation | |
51 | // ============================================================================ | |
846e1424 | 52 | |
a585ca5c | 53 | // --------------------------------------------------------------------------- |
1948bb32 | 54 | // wxDynamicLibrary |
a585ca5c | 55 | // --------------------------------------------------------------------------- |
7b0bfbb2 | 56 | |
defbed48 | 57 | #if defined(__WXPM__) || defined(__EMX__) |
9a83f860 | 58 | const wxString wxDynamicLibrary::ms_dllext(wxT(".dll")); |
f6bcfd97 | 59 | #endif |
f6bcfd97 | 60 | |
6d94009f | 61 | // for MSW/Unix it is defined in platform-specific file |
d98a58c5 | 62 | #if !(defined(__WINDOWS__) || defined(__UNIX__)) || defined(__EMX__) |
da55d064 | 63 | |
1948bb32 | 64 | wxDllType wxDynamicLibrary::GetProgramHandle() |
0868079c | 65 | { |
da55d064 | 66 | wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform")); |
7742efff | 67 | return 0; |
0868079c KB |
68 | } |
69 | ||
d98a58c5 | 70 | #endif // __WINDOWS__ || __UNIX__ |
6d94009f | 71 | |
da55d064 | 72 | |
defbed48 | 73 | bool wxDynamicLibrary::Load(const wxString& libnameOrig, int flags) |
a585ca5c | 74 | { |
9a83f860 | 75 | wxASSERT_MSG(m_handle == 0, wxT("Library already loaded.")); |
1948bb32 VS |
76 | |
77 | // add the proper extension for the DLL ourselves unless told not to | |
defbed48 | 78 | wxString libname = libnameOrig; |
1948bb32 VS |
79 | if ( !(flags & wxDL_VERBATIM) ) |
80 | { | |
81 | // and also check that the libname doesn't already have it | |
82 | wxString ext; | |
83 | wxFileName::SplitPath(libname, NULL, NULL, &ext); | |
84 | if ( ext.empty() ) | |
85 | { | |
86 | libname += GetDllExt(); | |
87 | } | |
88 | } | |
7b0bfbb2 | 89 | |
1948bb32 VS |
90 | // different ways to load a shared library |
91 | // | |
92 | // FIXME: should go to the platform-specific files! | |
3a39a9da | 93 | #if defined(__WXPM__) || defined(__EMX__) |
670f9935 | 94 | char err[256] = ""; |
1f3d9911 | 95 | DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle); |
6c286397 | 96 | #else // this should be the only remaining branch eventually |
da55d064 | 97 | m_handle = RawLoad(libname, flags); |
1948bb32 VS |
98 | #endif |
99 | ||
e2fc2bd5 | 100 | if ( m_handle == 0 && !(flags & wxDL_QUIET) ) |
7cc98b3e | 101 | { |
da55d064 VZ |
102 | #ifdef wxHAVE_DYNLIB_ERROR |
103 | Error(); | |
1948bb32 | 104 | #else |
da55d064 | 105 | wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str()); |
0b9ab0bd | 106 | #endif |
7cc98b3e VZ |
107 | } |
108 | ||
1948bb32 | 109 | return IsLoaded(); |
a585ca5c KB |
110 | } |
111 | ||
da55d064 VZ |
112 | // for MSW and Unix this is implemented in the platform-specific file |
113 | // | |
114 | // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp! | |
d98a58c5 | 115 | #if (!defined(__WINDOWS__) && !defined(__UNIX__)) || defined(__EMX__) |
defbed48 | 116 | |
9d033af9 VZ |
117 | /* static */ |
118 | void wxDynamicLibrary::Unload(wxDllType handle) | |
752c7d6b | 119 | { |
55034339 | 120 | #if defined(__OS2__) || defined(__EMX__) |
ff793cab | 121 | DosFreeModule( handle ); |
1948bb32 | 122 | #else |
9d033af9 | 123 | #error "runtime shared lib support not implemented" |
1948bb32 | 124 | #endif |
752c7d6b KB |
125 | } |
126 | ||
d98a58c5 | 127 | #endif // !(__WINDOWS__ || __UNIX__) |
defbed48 | 128 | |
a018a119 | 129 | void *wxDynamicLibrary::DoGetSymbol(const wxString &name, bool *success) const |
a585ca5c | 130 | { |
1948bb32 | 131 | wxCHECK_MSG( IsLoaded(), NULL, |
9a83f860 | 132 | wxT("Can't load symbol from unloaded library") ); |
1948bb32 | 133 | |
0b9ab0bd | 134 | void *symbol = 0; |
a585ca5c | 135 | |
999836aa | 136 | wxUnusedVar(symbol); |
3a39a9da | 137 | #if defined(__WXPM__) || defined(__EMX__) |
1f3d9911 | 138 | DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol ); |
1c193821 | 139 | #else |
da55d064 | 140 | symbol = RawGetSymbol(m_handle, name); |
1c193821 | 141 | #endif |
0b9ab0bd | 142 | |
a018a119 VZ |
143 | if ( success ) |
144 | *success = symbol != NULL; | |
145 | ||
146 | return symbol; | |
147 | } | |
148 | ||
149 | void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const | |
150 | { | |
151 | void *symbol = DoGetSymbol(name, success); | |
7b0bfbb2 VZ |
152 | if ( !symbol ) |
153 | { | |
da55d064 VZ |
154 | #ifdef wxHAVE_DYNLIB_ERROR |
155 | Error(); | |
0b9ab0bd | 156 | #else |
7cc98b3e VZ |
157 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), |
158 | name.c_str()); | |
0b9ab0bd | 159 | #endif |
7b0bfbb2 | 160 | } |
0b9ab0bd | 161 | |
7b0bfbb2 | 162 | return symbol; |
7a4b9130 | 163 | } |
5fccb5b4 | 164 | |
defbed48 VZ |
165 | // ---------------------------------------------------------------------------- |
166 | // informational methods | |
167 | // ---------------------------------------------------------------------------- | |
168 | ||
1948bb32 | 169 | /*static*/ |
350fffae VZ |
170 | wxString |
171 | wxDynamicLibrary::CanonicalizeName(const wxString& name, | |
da55d064 | 172 | wxDynamicLibraryCategory cat) |
7a4b9130 | 173 | { |
350fffae VZ |
174 | wxString nameCanonic; |
175 | ||
87ec9b8f | 176 | // under Unix the library names usually start with "lib" prefix, add it |
e9737ede | 177 | #if defined(__UNIX__) && !defined(__EMX__) |
350fffae VZ |
178 | switch ( cat ) |
179 | { | |
180 | default: | |
9a83f860 | 181 | wxFAIL_MSG( wxT("unknown wxDynamicLibraryCategory value") ); |
350fffae VZ |
182 | // fall through |
183 | ||
184 | case wxDL_MODULE: | |
185 | // don't do anything for modules, their names are arbitrary | |
186 | break; | |
187 | ||
188 | case wxDL_LIBRARY: | |
189 | // library names should start with "lib" under Unix | |
9a83f860 | 190 | nameCanonic = wxT("lib"); |
350fffae VZ |
191 | break; |
192 | } | |
da55d064 VZ |
193 | #else // !__UNIX__ |
194 | wxUnusedVar(cat); | |
195 | #endif // __UNIX__/!__UNIX__ | |
350fffae VZ |
196 | |
197 | nameCanonic << name << GetDllExt(); | |
198 | return nameCanonic; | |
7a4b9130 | 199 | } |
8a0d4cf6 | 200 | |
1948bb32 VS |
201 | /*static*/ |
202 | wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name, | |
203 | wxPluginCategory cat) | |
f11bdd03 | 204 | { |
1948bb32 VS |
205 | wxString suffix; |
206 | if ( cat == wxDL_PLUGIN_GUI ) | |
f11bdd03 | 207 | { |
449090b5 | 208 | suffix = wxPlatformInfo::Get().GetPortIdShortName(); |
f11bdd03 | 209 | } |
1948bb32 | 210 | #if wxUSE_UNICODE |
9a83f860 | 211 | suffix << wxT('u'); |
1948bb32 VS |
212 | #endif |
213 | #ifdef __WXDEBUG__ | |
9a83f860 | 214 | suffix << wxT('d'); |
1948bb32 | 215 | #endif |
f11bdd03 | 216 | |
1948bb32 | 217 | if ( !suffix.empty() ) |
9a83f860 | 218 | suffix = wxString(wxT("_")) + suffix; |
f11bdd03 | 219 | |
3546ffae | 220 | #define WXSTRINGIZE(x) #x |
e9737ede | 221 | #if defined(__UNIX__) && !defined(__EMX__) |
1948bb32 | 222 | #if (wxMINOR_VERSION % 2) == 0 |
3546ffae | 223 | #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) |
1948bb32 | 224 | #else |
3546ffae | 225 | #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z) |
1948bb32 VS |
226 | #endif |
227 | #else | |
228 | #if (wxMINOR_VERSION % 2) == 0 | |
3546ffae | 229 | #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) |
1948bb32 | 230 | #else |
3546ffae | 231 | #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z) |
1948bb32 VS |
232 | #endif |
233 | #endif | |
350fffae | 234 | |
1948bb32 VS |
235 | suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION, |
236 | wxRELEASE_NUMBER)); | |
237 | #undef wxDLLVER | |
3546ffae | 238 | #undef WXSTRINGIZE |
f11bdd03 | 239 | |
2c02ec05 VS |
240 | #ifdef __WINDOWS__ |
241 | // Add compiler identification: | |
242 | #if defined(__GNUG__) | |
9a83f860 | 243 | suffix << wxT("_gcc"); |
2c02ec05 | 244 | #elif defined(__VISUALC__) |
9a83f860 | 245 | suffix << wxT("_vc"); |
2c02ec05 | 246 | #elif defined(__WATCOMC__) |
9a83f860 | 247 | suffix << wxT("_wat"); |
2c02ec05 | 248 | #elif defined(__BORLANDC__) |
9a83f860 | 249 | suffix << wxT("_bcc"); |
2c02ec05 VS |
250 | #endif |
251 | #endif | |
252 | ||
1948bb32 | 253 | return CanonicalizeName(name + suffix, wxDL_MODULE); |
f11bdd03 | 254 | } |
5fccb5b4 | 255 | |
1948bb32 VS |
256 | /*static*/ |
257 | wxString wxDynamicLibrary::GetPluginsDirectory() | |
f11bdd03 | 258 | { |
1948bb32 VS |
259 | #ifdef __UNIX__ |
260 | wxString format = wxGetInstallPrefix(); | |
cb979fac | 261 | wxString dir; |
1948bb32 VS |
262 | format << wxFILE_SEP_PATH |
263 | << wxT("lib") << wxFILE_SEP_PATH | |
264 | << wxT("wx") << wxFILE_SEP_PATH | |
cb979fac | 265 | #if (wxMINOR_VERSION % 2) == 0 |
1948bb32 | 266 | << wxT("%i.%i"); |
1948bb32 | 267 | dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION); |
1948bb32 | 268 | #else |
cb979fac VS |
269 | << wxT("%i.%i.%i"); |
270 | dir.Printf(format.c_str(), | |
271 | wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER); | |
272 | #endif | |
273 | return dir; | |
274 | ||
275 | #else // ! __UNIX__ | |
1948bb32 VS |
276 | return wxEmptyString; |
277 | #endif | |
f11bdd03 GD |
278 | } |
279 | ||
f11bdd03 | 280 | |
1948bb32 | 281 | #endif // wxUSE_DYNLIB_CLASS |