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 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
37 wxLibraries wxTheLibraries
;
39 // ---------------------------------------------------------------------------
40 // wxLibrary (one instance per dynamic library
41 // ---------------------------------------------------------------------------
43 wxLibrary::wxLibrary(void *handle
)
45 typedef wxClassInfo
**(*t_get_first
)(void);
46 t_get_first get_first
;
51 // Some system may use a local heap for library.
52 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
53 // It is a wxWindows DLL.
55 PrepareClasses(get_first());
58 wxLibrary::~wxLibrary()
60 if (m_handle
&& m_destroy
) {
65 FreeLibrary((HMODULE
)m_handle
);
70 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
72 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
77 return info
->CreateObject();
80 void wxLibrary::PrepareClasses(wxClassInfo
**first
)
82 // Index all class infos by their class name
83 wxClassInfo
*info
= *first
;
87 classTable
.Put(info
->className
, (wxObject
*)info
);
91 // Set base pointers for each wxClassInfo
95 if (info
->GetBaseClassName1())
96 info
->baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
97 if (info
->GetBaseClassName2())
98 info
->baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
104 void *wxLibrary::GetSymbol(const wxString
& symbname
)
107 return dlsym(m_handle
, WXSTRINGCAST symbname
);
110 return GetProcAddress(m_handle
, WXSTRINGCAST symbname
);
115 // ---------------------------------------------------------------------------
116 // wxLibraries (only one instance should normally exist)
117 // ---------------------------------------------------------------------------
119 wxLibraries::wxLibraries()
123 wxLibraries::~wxLibraries()
125 wxNode
*node
= m_loaded
.First();
128 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
135 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
137 wxString lib_name
= name
;
141 if ( (node
= m_loaded
.Find(name
.GetData())) )
142 return ((wxLibrary
*)node
->Data());
144 #if defined(__UNIX__)
145 lib_name
.Prepend("./lib");
148 printf("lib_name = %s\n", WXSTRINGCAST lib_name
);
150 void *handle
= dlopen(WXSTRINGCAST lib_name
, RTLD_LAZY
);
154 #elif defined(__WINDOWS__)
157 HMODULE handle
= LoadLibrary(lib_name
);
164 lib
= new wxLibrary((void *)handle
);
166 m_loaded
.Append(name
.GetData(), lib
);
170 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
172 wxNode
*node
= m_loaded
.First();
176 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);