]>
Commit | Line | Data |
---|---|---|
7a4b9130 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynlib.cpp | |
3 | // Purpose: Dynamic library management | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 20/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
7b0bfbb2 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7a4b9130 | 20 | #ifdef __GNUG__ |
7b0bfbb2 | 21 | #pragma implementation "dynlib.h" |
7a4b9130 GL |
22 | #endif |
23 | ||
0c32066b JS |
24 | #include "wx/wxprec.h" |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif //__BORLANDC__ | |
29 | ||
7b0bfbb2 VZ |
30 | #include "wx/dynlib.h" |
31 | #include "wx/filefn.h" | |
32 | #include "wx/intl.h" | |
33 | #include "wx/log.h" | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // conditional compilation | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | #if defined(HAVE_DLOPEN) | |
40 | #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY) | |
41 | #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name) | |
42 | #define wxDllClose dlclose | |
43 | #elif defined(HAVE_SHLLOAD) | |
44 | #define wxDllOpen(lib) shl_open(lib, BIND_DEFERRED, 0) | |
45 | #define wxDllClose shl_unload | |
46 | ||
47 | static inline void *wxDllGetSymbol(shl_t *handle, const char *name) | |
48 | { | |
49 | void *sym; | |
50 | if ( shl_findsym(handle, name, TYPE_UNDEFINED, &sym) == 0 ) | |
51 | return sym; | |
52 | else | |
53 | return (void *)0; | |
54 | } | |
55 | #elif defined(__WINDOWS__) | |
56 | #define wxDllOpen(lib) ::LoadLibrary(lib) | |
57 | #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name) | |
58 | #define wxDllClose ::FreeLibrary | |
59 | #else | |
60 | #error "Don't know how to load shared libraries on this platform." | |
61 | #endif // OS | |
7a4b9130 GL |
62 | |
63 | // --------------------------------------------------------------------------- | |
7b0bfbb2 | 64 | // Global variables |
7a4b9130 GL |
65 | // --------------------------------------------------------------------------- |
66 | ||
7b0bfbb2 | 67 | wxLibraries wxTheLibraries; |
7a4b9130 | 68 | |
7b0bfbb2 VZ |
69 | // ---------------------------------------------------------------------------- |
70 | // private functions | |
71 | // ---------------------------------------------------------------------------- | |
123a7fdd | 72 | |
7b0bfbb2 VZ |
73 | // construct the full name from the base shared object name: adds a .dll |
74 | // suffix under Windows or .so under Unix | |
75 | static wxString ConstructLibraryName(const wxString& basename) | |
76 | { | |
77 | wxString fullname(basename); | |
78 | ||
79 | #if defined(__UNIX__) | |
80 | fullname << ".so"; | |
81 | #elif defined(__WINDOWS__) | |
82 | fullname << ".dll"; | |
27529614 JS |
83 | #endif |
84 | ||
7b0bfbb2 VZ |
85 | return fullname; |
86 | } | |
7a4b9130 | 87 | |
7b0bfbb2 VZ |
88 | // ============================================================================ |
89 | // implementation | |
90 | // ============================================================================ | |
7a4b9130 GL |
91 | |
92 | // --------------------------------------------------------------------------- | |
7b0bfbb2 | 93 | // wxLibrary (one instance per dynamic library) |
7a4b9130 GL |
94 | // --------------------------------------------------------------------------- |
95 | ||
7b0bfbb2 | 96 | wxLibrary::wxLibrary(wxDllType handle) |
7a4b9130 | 97 | { |
7b0bfbb2 VZ |
98 | typedef wxClassInfo *(*t_get_first)(void); |
99 | t_get_first get_first; | |
7a4b9130 | 100 | |
7b0bfbb2 | 101 | m_handle = handle; |
7a4b9130 | 102 | |
7b0bfbb2 VZ |
103 | // Some system may use a local heap for library. |
104 | get_first = (t_get_first)GetSymbol("wxGetClassFirst"); | |
105 | // It is a wxWindows DLL. | |
106 | if (get_first) | |
107 | PrepareClasses(get_first()); | |
7a4b9130 GL |
108 | } |
109 | ||
110 | wxLibrary::~wxLibrary() | |
111 | { | |
7b0bfbb2 VZ |
112 | if ( m_handle ) |
113 | { | |
114 | wxDllClose(m_handle); | |
115 | } | |
7a4b9130 GL |
116 | } |
117 | ||
118 | wxObject *wxLibrary::CreateObject(const wxString& name) | |
119 | { | |
7b0bfbb2 | 120 | wxClassInfo *info = (wxClassInfo *)classTable.Get(name); |
f4a8c29f | 121 | |
7b0bfbb2 VZ |
122 | if (!info) |
123 | return NULL; | |
f4a8c29f | 124 | |
7b0bfbb2 | 125 | return info->CreateObject(); |
f4a8c29f GL |
126 | } |
127 | ||
856d2e52 | 128 | void wxLibrary::PrepareClasses(wxClassInfo *first) |
f4a8c29f | 129 | { |
7b0bfbb2 VZ |
130 | // Index all class infos by their class name |
131 | wxClassInfo *info = first; | |
132 | while (info) | |
133 | { | |
134 | if (info->m_className) | |
135 | classTable.Put(info->m_className, (wxObject *)info); | |
136 | info = info->GetNext(); | |
137 | } | |
138 | ||
139 | // Set base pointers for each wxClassInfo | |
140 | info = first; | |
141 | while (info) | |
142 | { | |
143 | if (info->GetBaseClassName1()) | |
144 | info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1()); | |
145 | if (info->GetBaseClassName2()) | |
146 | info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2()); | |
147 | info = info->m_next; | |
148 | } | |
7a4b9130 GL |
149 | } |
150 | ||
151 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
152 | { | |
7b0bfbb2 VZ |
153 | void *symbol = NULL; // return value |
154 | ||
155 | #if defined( __WXMAC__ ) | |
156 | Ptr symAddress ; | |
157 | CFragSymbolClass symClass ; | |
158 | Str255 symName ; | |
159 | ||
160 | strcpy( (char*) symName , symbname ) ; | |
161 | c2pstr( (char*) symName ) ; | |
162 | ||
163 | if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr ) | |
164 | { | |
165 | symbol = (void *)symAddress ; | |
166 | } | |
167 | #else | |
168 | // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we | |
169 | // should make a copy of it | |
170 | symbol = wxDllGetSymbol(m_handle, WXSTRINGCAST symbname); | |
123a7fdd | 171 | #endif |
7b0bfbb2 VZ |
172 | |
173 | if ( !symbol ) | |
174 | { | |
175 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), | |
176 | symbname.c_str()); | |
177 | } | |
178 | ||
179 | return symbol; | |
7a4b9130 GL |
180 | } |
181 | ||
182 | // --------------------------------------------------------------------------- | |
183 | // wxLibraries (only one instance should normally exist) | |
184 | // --------------------------------------------------------------------------- | |
185 | ||
186 | wxLibraries::wxLibraries() | |
187 | { | |
188 | } | |
189 | ||
190 | wxLibraries::~wxLibraries() | |
191 | { | |
7b0bfbb2 | 192 | wxNode *node = m_loaded.First(); |
7a4b9130 | 193 | |
7b0bfbb2 VZ |
194 | while (node) { |
195 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
196 | delete lib; | |
7a4b9130 | 197 | |
7b0bfbb2 VZ |
198 | node = node->Next(); |
199 | } | |
7a4b9130 GL |
200 | } |
201 | ||
202 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
203 | { | |
7b0bfbb2 VZ |
204 | wxNode *node; |
205 | wxLibrary *lib; | |
206 | wxClassInfo *old_sm_first; | |
7a4b9130 | 207 | |
7b0bfbb2 VZ |
208 | if ( (node = m_loaded.Find(name.GetData())) ) |
209 | return ((wxLibrary *)node->Data()); | |
7a4b9130 | 210 | |
7b0bfbb2 VZ |
211 | // If DLL shares data, this is necessary. |
212 | old_sm_first = wxClassInfo::sm_first; | |
213 | wxClassInfo::sm_first = NULL; | |
214 | ||
215 | wxString lib_name = ConstructLibraryName(name); | |
856d2e52 | 216 | |
9e4b2f1c | 217 | #if defined(__UNIX__) |
7b0bfbb2 VZ |
218 | // TODO use LD_LIBRARY_PATH! |
219 | lib_name.Prepend("/lib"); | |
220 | #endif // __UNIX__ | |
7a4b9130 | 221 | |
7b0bfbb2 | 222 | wxDllType handle ; |
7a4b9130 | 223 | |
7b0bfbb2 VZ |
224 | #if defined(__WXMAC__) |
225 | FSSpec myFSSpec ; | |
226 | Ptr myMainAddr ; | |
227 | Str255 myErrName ; | |
7a4b9130 | 228 | |
7b0bfbb2 VZ |
229 | wxMacPathToFSSpec( lib_name , &myFSSpec ) ; |
230 | if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr , | |
231 | myErrName ) != noErr ) | |
232 | { | |
233 | p2cstr( myErrName ) ; | |
234 | wxASSERT_MSG( 1 , (char*)myErrName ) ; | |
235 | return NULL ; | |
236 | } | |
237 | #else // !Mac | |
238 | handle = wxDllOpen(lib_name); | |
239 | #endif // OS | |
856d2e52 | 240 | |
7b0bfbb2 VZ |
241 | if ( !handle ) |
242 | { | |
243 | wxLogSysError(_("Failed to load shared library '%s'"), | |
244 | lib_name.c_str()); | |
7a4b9130 | 245 | |
7b0bfbb2 VZ |
246 | return NULL; |
247 | } | |
f4a8c29f | 248 | |
7b0bfbb2 | 249 | lib = new wxLibrary(handle); |
7a4b9130 | 250 | |
7b0bfbb2 | 251 | wxClassInfo::sm_first = old_sm_first; |
856d2e52 | 252 | |
7b0bfbb2 VZ |
253 | m_loaded.Append(name.GetData(), lib); |
254 | ||
255 | return lib; | |
7a4b9130 GL |
256 | } |
257 | ||
258 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
259 | { | |
7b0bfbb2 VZ |
260 | wxNode *node = m_loaded.First(); |
261 | wxObject *obj; | |
7a4b9130 | 262 | |
7b0bfbb2 VZ |
263 | while (node) { |
264 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
265 | if (obj) | |
266 | return obj; | |
7a4b9130 | 267 | |
7b0bfbb2 VZ |
268 | node = node->Next(); |
269 | } | |
270 | return NULL; | |
7a4b9130 | 271 | } |