]>
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 | ||
77ff2d26 | 34 | #ifdef __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 | { | |
f4a8c29f GL |
58 | typedef wxClassInfo **(*t_get_first)(void); |
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) { |
77ff2d26 | 74 | #ifdef __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 | ||
93 | void wxLibrary::PrepareClasses(wxClassInfo **first) | |
94 | { | |
95 | // Index all class infos by their class name | |
96 | wxClassInfo *info = *first; | |
97 | while (info) | |
98 | { | |
0c32066b JS |
99 | if (info->m_className) |
100 | classTable.Put(info->m_className, (wxObject *)info); | |
101 | info = info->m_next; | |
f4a8c29f GL |
102 | } |
103 | ||
104 | // Set base pointers for each wxClassInfo | |
105 | info = *first; | |
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 GL |
113 | } |
114 | *first = NULL; | |
7a4b9130 GL |
115 | } |
116 | ||
117 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
118 | { | |
77ff2d26 | 119 | #ifdef __UNIX__ |
123a7fdd | 120 | return dlsym(m_handle, WXSTRINGCAST symbname); |
7a4b9130 | 121 | #endif |
123a7fdd | 122 | #ifdef __WINDOWS__ |
0c32066b | 123 | return GetProcAddress((HINSTANCE) m_handle, WXSTRINGCAST symbname); |
123a7fdd GL |
124 | #endif |
125 | return NULL; | |
7a4b9130 GL |
126 | } |
127 | ||
128 | // --------------------------------------------------------------------------- | |
129 | // wxLibraries (only one instance should normally exist) | |
130 | // --------------------------------------------------------------------------- | |
131 | ||
132 | wxLibraries::wxLibraries() | |
133 | { | |
134 | } | |
135 | ||
136 | wxLibraries::~wxLibraries() | |
137 | { | |
138 | wxNode *node = m_loaded.First(); | |
139 | ||
140 | while (node) { | |
141 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
142 | delete lib; | |
143 | ||
144 | node = node->Next(); | |
145 | } | |
146 | } | |
147 | ||
148 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
149 | { | |
150 | wxString lib_name = name; | |
151 | wxNode *node; | |
152 | wxLibrary *lib; | |
153 | ||
154 | if ( (node = m_loaded.Find(name.GetData())) ) | |
155 | return ((wxLibrary *)node->Data()); | |
156 | ||
f4a8c29f | 157 | #if defined(__UNIX__) |
1d44aaf8 | 158 | lib_name.Prepend("./lib"); |
7a4b9130 GL |
159 | lib_name += ".so"; |
160 | ||
161 | printf("lib_name = %s\n", WXSTRINGCAST lib_name); | |
162 | ||
123a7fdd | 163 | void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY); |
7a4b9130 | 164 | |
123a7fdd GL |
165 | if (!handle) |
166 | return NULL; | |
f4a8c29f | 167 | #elif defined(__WINDOWS__) |
7a4b9130 GL |
168 | lib_name += ".dll"; |
169 | ||
27529614 JS |
170 | #ifdef UNICODE |
171 | HMODULE handle = LoadLibraryW(lib_name); | |
172 | #else | |
173 | HMODULE handle = LoadLibraryA(lib_name); | |
174 | #endif | |
123a7fdd GL |
175 | if (!handle) |
176 | return NULL; | |
f4a8c29f GL |
177 | #else |
178 | return NULL; | |
7a4b9130 | 179 | #endif |
f4a8c29f | 180 | |
123a7fdd | 181 | lib = new wxLibrary((void *)handle); |
7a4b9130 GL |
182 | |
183 | m_loaded.Append(name.GetData(), lib); | |
184 | return lib; | |
185 | } | |
186 | ||
187 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
188 | { | |
189 | wxNode *node = m_loaded.First(); | |
190 | wxObject *obj; | |
191 | ||
192 | while (node) { | |
193 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
194 | if (obj) | |
195 | return obj; | |
196 | ||
197 | node = node->Next(); | |
198 | } | |
199 | return NULL; | |
200 | } |