1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dynlib.h"
16 #include <wx/dynlib.h>
17 #include <wx/filefn.h>
19 #include <wx/string.h>
21 // ---------------------------------------------------------------------------
22 // System dependent include
23 // ---------------------------------------------------------------------------
25 #if defined(__LINUX__) || defined(__SGI__)
33 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
37 wxLibraries wxTheLibraries
;
39 // ---------------------------------------------------------------------------
40 // wxLibrary (one instance per dynamic library
41 // ---------------------------------------------------------------------------
43 wxLibrary::wxLibrary(void *handle
)
45 typedef wxClassLibrary
*(*t_get_list
)(void);
50 get_list
= (t_get_list
)GetSymbol("GetClassList");
51 m_liblist
= (*get_list
)();
54 wxLibrary::~wxLibrary()
57 typedef void (*t_free_list
)(wxClassLibrary
*);
58 t_free_list free_list
;
60 free_list
= (t_free_list
) GetSymbol("FreeClassList");
61 if (free_list
!= NULL
)
66 #if defined(__LINUX__) || defined(__SGI__)
70 FreeLibrary((HMODULE
)m_handle
);
75 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
77 return m_liblist
->CreateObject(name
);
80 void *wxLibrary::GetSymbol(const wxString
& symbname
)
82 #if defined(__LINUX__) || defined(__SGI__)
83 return dlsym(m_handle
, WXSTRINGCAST symbname
);
86 return GetProcAddress(m_handle
, WXSTRINGCAST symbname
);
91 // ---------------------------------------------------------------------------
92 // wxLibraries (only one instance should normally exist)
93 // ---------------------------------------------------------------------------
95 wxLibraries::wxLibraries()
99 wxLibraries::~wxLibraries()
101 wxNode
*node
= m_loaded
.First();
104 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
111 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
113 wxString lib_name
= name
;
117 if ( (node
= m_loaded
.Find(name
.GetData())) )
118 return ((wxLibrary
*)node
->Data());
120 #if defined(__LINUX__) || defined(__SGI__)
121 lib_name
.Prepend("lib");
124 printf("lib_name = %s\n", WXSTRINGCAST lib_name
);
126 void *handle
= dlopen(WXSTRINGCAST lib_name
, RTLD_LAZY
);
134 HMODULE handle
= LoadLibrary(lib_name
);
138 lib
= new wxLibrary((void *)handle
);
140 m_loaded
.Append(name
.GetData(), lib
);
144 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
146 wxNode
*node
= m_loaded
.First();
150 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
159 // ---------------------------------------------------------------------------
160 // wxClassLibrary (this class is used to access the internal class)
161 // ---------------------------------------------------------------------------
163 wxClassLibrary::wxClassLibrary(void)
167 wxClassLibrary::~wxClassLibrary(void)
171 for (i
=0;i
<m_list
.Count();i
++)
175 void wxClassLibrary::RegisterClass(wxClassInfo
*class_info
,
176 const wxString
& path
)
178 wxClassLibInfo
*info
= new wxClassLibInfo
;
180 info
->class_info
= class_info
;
185 void wxClassLibrary::UnregisterClass(wxClassInfo
*class_info
)
189 while (i
< m_list
.Count()) {
190 if (m_list
[i
]->class_info
== class_info
) {
199 bool wxClassLibrary::CreateObjects(const wxString
& path
,
200 wxArrayClassInfo
& objs
)
202 wxClassLibInfo
*info
;
205 while (i
< m_list
.Count()) {
207 if (wxMatchWild(path
, info
->path
))
208 objs
.Add(info
->class_info
);
214 bool wxClassLibrary::FetchInfos(const wxString
& path
,
215 wxArrayClassLibInfo
& infos
)
217 wxClassLibInfo
*info
;
220 while (i
< m_list
.Count()) {
222 if (wxMatchWild(path
, info
->path
)) {
223 wxClassLibInfo
*inf
= new wxClassLibInfo
;
232 wxObject
*wxClassLibrary::CreateObject(const wxString
& path
)
234 wxClassLibInfo
*info
;
237 while (i
< m_list
.Count()) {
239 if (wxMatchWild(path
, info
->path
))
240 return info
->class_info
->CreateObject();