]>
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "dynlib.h" | |
14 | #endif | |
15 | ||
0c32066b JS |
16 | #include "wx/wxprec.h" |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif //__BORLANDC__ | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #endif //WX_PRECOMP | |
24 | ||
7a4b9130 GL |
25 | #include <wx/dynlib.h> |
26 | #include <wx/filefn.h> | |
27 | #include <wx/list.h> | |
28 | #include <wx/string.h> | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | // System dependent include | |
32 | // --------------------------------------------------------------------------- | |
33 | ||
9e4b2f1c | 34 | #if defined(__UNIX__) |
7a4b9130 GL |
35 | #include <dlfcn.h> |
36 | #endif | |
37 | ||
123a7fdd GL |
38 | #ifdef __WINDOWS__ |
39 | #include <windows.h> | |
40 | #endif | |
41 | ||
27529614 JS |
42 | #ifdef LoadLibrary |
43 | #undef LoadLibrary | |
44 | #endif | |
45 | ||
7a4b9130 GL |
46 | // --------------------------------------------------------------------------- |
47 | // Global variables | |
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | wxLibraries wxTheLibraries; | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | // wxLibrary (one instance per dynamic library | |
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | wxLibrary::wxLibrary(void *handle) | |
57 | { | |
856d2e52 | 58 | typedef wxClassInfo *(*t_get_first)(void); |
f4a8c29f | 59 | t_get_first get_first; |
7a4b9130 GL |
60 | |
61 | m_handle = handle; | |
f4a8c29f | 62 | m_destroy = TRUE; |
7a4b9130 | 63 | |
f4a8c29f GL |
64 | // Some system may use a local heap for library. |
65 | get_first = (t_get_first)GetSymbol("wxGetClassFirst"); | |
66 | // It is a wxWindows DLL. | |
67 | if (get_first) | |
68 | PrepareClasses(get_first()); | |
7a4b9130 GL |
69 | } |
70 | ||
71 | wxLibrary::~wxLibrary() | |
72 | { | |
f4a8c29f | 73 | if (m_handle && m_destroy) { |
9e4b2f1c | 74 | #if defined(__UNIX__) |
7a4b9130 | 75 | dlclose(m_handle); |
123a7fdd GL |
76 | #endif |
77 | #ifdef __WINDOWS__ | |
78 | FreeLibrary((HMODULE)m_handle); | |
79 | #endif | |
7a4b9130 GL |
80 | } |
81 | } | |
82 | ||
83 | wxObject *wxLibrary::CreateObject(const wxString& name) | |
84 | { | |
f4a8c29f GL |
85 | wxClassInfo *info = (wxClassInfo *)classTable.Get(name); |
86 | ||
87 | if (!info) | |
88 | return NULL; | |
89 | ||
90 | return info->CreateObject(); | |
91 | } | |
92 | ||
856d2e52 | 93 | void wxLibrary::PrepareClasses(wxClassInfo *first) |
f4a8c29f GL |
94 | { |
95 | // Index all class infos by their class name | |
856d2e52 | 96 | wxClassInfo *info = first; |
f4a8c29f GL |
97 | while (info) |
98 | { | |
0c32066b JS |
99 | if (info->m_className) |
100 | classTable.Put(info->m_className, (wxObject *)info); | |
856d2e52 | 101 | info = info->GetNext(); |
f4a8c29f GL |
102 | } |
103 | ||
104 | // Set base pointers for each wxClassInfo | |
856d2e52 | 105 | info = first; |
f4a8c29f GL |
106 | while (info) |
107 | { | |
108 | if (info->GetBaseClassName1()) | |
0c32066b | 109 | info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1()); |
f4a8c29f | 110 | if (info->GetBaseClassName2()) |
0c32066b JS |
111 | info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2()); |
112 | info = info->m_next; | |
f4a8c29f | 113 | } |
7a4b9130 GL |
114 | } |
115 | ||
116 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
117 | { | |
9e4b2f1c | 118 | #if defined(__UNIX__) |
123a7fdd | 119 | return dlsym(m_handle, WXSTRINGCAST symbname); |
7a4b9130 | 120 | #endif |
123a7fdd | 121 | #ifdef __WINDOWS__ |
0c32066b | 122 | return GetProcAddress((HINSTANCE) m_handle, WXSTRINGCAST symbname); |
123a7fdd GL |
123 | #endif |
124 | return NULL; | |
7a4b9130 GL |
125 | } |
126 | ||
127 | // --------------------------------------------------------------------------- | |
128 | // wxLibraries (only one instance should normally exist) | |
129 | // --------------------------------------------------------------------------- | |
130 | ||
131 | wxLibraries::wxLibraries() | |
132 | { | |
133 | } | |
134 | ||
135 | wxLibraries::~wxLibraries() | |
136 | { | |
137 | wxNode *node = m_loaded.First(); | |
138 | ||
139 | while (node) { | |
140 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
141 | delete lib; | |
142 | ||
143 | node = node->Next(); | |
144 | } | |
145 | } | |
146 | ||
147 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
148 | { | |
149 | wxString lib_name = name; | |
150 | wxNode *node; | |
151 | wxLibrary *lib; | |
856d2e52 | 152 | wxClassInfo *old_sm_first; |
7a4b9130 GL |
153 | |
154 | if ( (node = m_loaded.Find(name.GetData())) ) | |
155 | return ((wxLibrary *)node->Data()); | |
156 | ||
856d2e52 GL |
157 | // If DLL shares data, this is necessary. |
158 | old_sm_first = wxClassInfo::sm_first; | |
159 | wxClassInfo::sm_first = NULL; | |
160 | ||
9e4b2f1c | 161 | #if defined(__UNIX__) |
1d44aaf8 | 162 | lib_name.Prepend("./lib"); |
7a4b9130 GL |
163 | lib_name += ".so"; |
164 | ||
165 | printf("lib_name = %s\n", WXSTRINGCAST lib_name); | |
166 | ||
123a7fdd | 167 | void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY); |
7a4b9130 | 168 | |
856d2e52 GL |
169 | printf("error = %s\n", dlerror()); |
170 | ||
123a7fdd GL |
171 | if (!handle) |
172 | return NULL; | |
f4a8c29f | 173 | #elif defined(__WINDOWS__) |
7a4b9130 GL |
174 | lib_name += ".dll"; |
175 | ||
27529614 JS |
176 | #ifdef UNICODE |
177 | HMODULE handle = LoadLibraryW(lib_name); | |
178 | #else | |
179 | HMODULE handle = LoadLibraryA(lib_name); | |
180 | #endif | |
123a7fdd GL |
181 | if (!handle) |
182 | return NULL; | |
f4a8c29f GL |
183 | #else |
184 | return NULL; | |
7a4b9130 | 185 | #endif |
f4a8c29f | 186 | |
123a7fdd | 187 | lib = new wxLibrary((void *)handle); |
7a4b9130 | 188 | |
856d2e52 GL |
189 | wxClassInfo::sm_first = old_sm_first; |
190 | ||
7a4b9130 GL |
191 | m_loaded.Append(name.GetData(), lib); |
192 | return lib; | |
193 | } | |
194 | ||
195 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
196 | { | |
197 | wxNode *node = m_loaded.First(); | |
198 | wxObject *obj; | |
199 | ||
200 | while (node) { | |
201 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
297a3fec | 202 | if (obj) |
7a4b9130 GL |
203 | return obj; |
204 | ||
205 | node = node->Next(); | |
206 | } | |
207 | return NULL; | |
208 | } |