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 // ---------------------------------------------------------------------------
34 #if defined(__UNIX__) || defined(__unix__)
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
) {
74 #if defined(__UNIX__) || defined(__unix__)
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
);
101 info
= info
->GetNext();
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());
116 void *wxLibrary::GetSymbol(const wxString
& symbname
)
118 #if defined(__UNIX__) || defined(__unix__)
119 return dlsym(m_handle
, WXSTRINGCAST symbname
);
122 return GetProcAddress((HINSTANCE
) m_handle
, WXSTRINGCAST symbname
);
127 // ---------------------------------------------------------------------------
128 // wxLibraries (only one instance should normally exist)
129 // ---------------------------------------------------------------------------
131 wxLibraries::wxLibraries()
135 wxLibraries::~wxLibraries()
137 wxNode
*node
= m_loaded
.First();
140 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
147 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
149 wxString lib_name
= name
;
152 wxClassInfo
*old_sm_first
;
154 if ( (node
= m_loaded
.Find(name
.GetData())) )
155 return ((wxLibrary
*)node
->Data());
157 // If DLL shares data, this is necessary.
158 old_sm_first
= wxClassInfo::sm_first
;
159 wxClassInfo::sm_first
= NULL
;
161 #if defined(__UNIX__) || defined(__unix__)
162 lib_name
.Prepend("./lib");
165 printf("lib_name = %s\n", WXSTRINGCAST lib_name
);
167 void *handle
= dlopen(WXSTRINGCAST lib_name
, RTLD_LAZY
);
169 printf("error = %s\n", dlerror());
173 #elif defined(__WINDOWS__)
177 HMODULE handle
= LoadLibraryW(lib_name
);
179 HMODULE handle
= LoadLibraryA(lib_name
);
187 lib
= new wxLibrary((void *)handle
);
189 wxClassInfo::sm_first
= old_sm_first
;
191 m_loaded
.Append(name
.GetData(), lib
);
195 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
197 wxNode
*node
= m_loaded
.First();
201 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);