]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
fixed some ifdef's to also check for __unix__
[wxWidgets.git] / src / common / dynlib.cpp
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
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
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
34 #if defined(__UNIX__) || defined(__unix__)
35 #include <dlfcn.h>
36 #endif
37
38 #ifdef __WINDOWS__
39 #include <windows.h>
40 #endif
41
42 #ifdef LoadLibrary
43 #undef LoadLibrary
44 #endif
45
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 {
58 typedef wxClassInfo *(*t_get_first)(void);
59 t_get_first get_first;
60
61 m_handle = handle;
62 m_destroy = TRUE;
63
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());
69 }
70
71 wxLibrary::~wxLibrary()
72 {
73 if (m_handle && m_destroy) {
74 #if defined(__UNIX__) || defined(__unix__)
75 dlclose(m_handle);
76 #endif
77 #ifdef __WINDOWS__
78 FreeLibrary((HMODULE)m_handle);
79 #endif
80 }
81 }
82
83 wxObject *wxLibrary::CreateObject(const wxString& name)
84 {
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 {
99 if (info->m_className)
100 classTable.Put(info->m_className, (wxObject *)info);
101 info = info->GetNext();
102 }
103
104 // Set base pointers for each wxClassInfo
105 info = first;
106 while (info)
107 {
108 if (info->GetBaseClassName1())
109 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
110 if (info->GetBaseClassName2())
111 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
112 info = info->m_next;
113 }
114 }
115
116 void *wxLibrary::GetSymbol(const wxString& symbname)
117 {
118 #if defined(__UNIX__) || defined(__unix__)
119 return dlsym(m_handle, WXSTRINGCAST symbname);
120 #endif
121 #ifdef __WINDOWS__
122 return GetProcAddress((HINSTANCE) m_handle, WXSTRINGCAST symbname);
123 #endif
124 return NULL;
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;
152 wxClassInfo *old_sm_first;
153
154 if ( (node = m_loaded.Find(name.GetData())) )
155 return ((wxLibrary *)node->Data());
156
157 // If DLL shares data, this is necessary.
158 old_sm_first = wxClassInfo::sm_first;
159 wxClassInfo::sm_first = NULL;
160
161 #if defined(__UNIX__) || defined(__unix__)
162 lib_name.Prepend("./lib");
163 lib_name += ".so";
164
165 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
166
167 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
168
169 printf("error = %s\n", dlerror());
170
171 if (!handle)
172 return NULL;
173 #elif defined(__WINDOWS__)
174 lib_name += ".dll";
175
176 #ifdef UNICODE
177 HMODULE handle = LoadLibraryW(lib_name);
178 #else
179 HMODULE handle = LoadLibraryA(lib_name);
180 #endif
181 if (!handle)
182 return NULL;
183 #else
184 return NULL;
185 #endif
186
187 lib = new wxLibrary((void *)handle);
188
189 wxClassInfo::sm_first = old_sm_first;
190
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);
202 if (obj)
203 return obj;
204
205 node = node->Next();
206 }
207 return NULL;
208 }