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/wxprec.h"
25 #include <wx/dynlib.h>
26 #include <wx/filefn.h>
28 #include <wx/string.h>
30 // ---------------------------------------------------------------------------
31 // System dependent include
32 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
50 wxLibraries wxTheLibraries
;
52 // ---------------------------------------------------------------------------
53 // wxLibrary (one instance per dynamic library
54 // ---------------------------------------------------------------------------
56 wxLibrary::wxLibrary(void *handle
)
58 typedef wxClassInfo
**(*t_get_first
)(void);
59 t_get_first get_first
;
64 // Some system may use a local heap for library.
65 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
66 // It is a wxWindows DLL.
68 PrepareClasses(get_first());
71 wxLibrary::~wxLibrary()
73 if (m_handle
&& m_destroy
) {
78 FreeLibrary((HMODULE
)m_handle
);
83 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
85 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
90 return info
->CreateObject();
93 void wxLibrary::PrepareClasses(wxClassInfo
**first
)
95 // Index all class infos by their class name
96 wxClassInfo
*info
= *first
;
99 if (info
->m_className
)
100 classTable
.Put(info
->m_className
, (wxObject
*)info
);
104 // Set base pointers for each wxClassInfo
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());
117 void *wxLibrary::GetSymbol(const wxString
& symbname
)
120 return dlsym(m_handle
, WXSTRINGCAST symbname
);
123 return GetProcAddress((HINSTANCE
) m_handle
, WXSTRINGCAST symbname
);
128 // ---------------------------------------------------------------------------
129 // wxLibraries (only one instance should normally exist)
130 // ---------------------------------------------------------------------------
132 wxLibraries::wxLibraries()
136 wxLibraries::~wxLibraries()
138 wxNode
*node
= m_loaded
.First();
141 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
148 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
150 wxString lib_name
= name
;
154 if ( (node
= m_loaded
.Find(name
.GetData())) )
155 return ((wxLibrary
*)node
->Data());
157 #if defined(__UNIX__)
158 lib_name
.Prepend("./lib");
161 printf("lib_name = %s\n", WXSTRINGCAST lib_name
);
163 void *handle
= dlopen(WXSTRINGCAST lib_name
, RTLD_LAZY
);
167 #elif defined(__WINDOWS__)
171 HMODULE handle
= LoadLibraryW(lib_name
);
173 HMODULE handle
= LoadLibraryA(lib_name
);
181 lib
= new wxLibrary((void *)handle
);
183 m_loaded
.Append(name
.GetData(), lib
);
187 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
189 wxNode
*node
= m_loaded
.First();
193 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);