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 // ---------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------
39 // ---------------------------------------------------------------------------
41 wxLibraries wxTheLibraries
;
43 // ---------------------------------------------------------------------------
44 // wxLibrary (one instance per dynamic library
45 // ---------------------------------------------------------------------------
47 wxLibrary::wxLibrary(void *handle
)
49 typedef wxClassInfo
**(*t_get_first
)(void);
50 t_get_first get_first
;
55 // Some system may use a local heap for library.
56 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
57 // It is a wxWindows DLL.
59 PrepareClasses(get_first());
62 wxLibrary::~wxLibrary()
64 if (m_handle
&& m_destroy
) {
69 FreeLibrary((HMODULE
)m_handle
);
74 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
76 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
81 return info
->CreateObject();
84 void wxLibrary::PrepareClasses(wxClassInfo
**first
)
86 // Index all class infos by their class name
87 wxClassInfo
*info
= *first
;
91 classTable
.Put(info
->className
, (wxObject
*)info
);
95 // Set base pointers for each wxClassInfo
99 if (info
->GetBaseClassName1())
100 info
->baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
101 if (info
->GetBaseClassName2())
102 info
->baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
108 void *wxLibrary::GetSymbol(const wxString
& symbname
)
111 return dlsym(m_handle
, WXSTRINGCAST symbname
);
114 return GetProcAddress(m_handle
, WXSTRINGCAST symbname
);
119 // ---------------------------------------------------------------------------
120 // wxLibraries (only one instance should normally exist)
121 // ---------------------------------------------------------------------------
123 wxLibraries::wxLibraries()
127 wxLibraries::~wxLibraries()
129 wxNode
*node
= m_loaded
.First();
132 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
139 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
141 wxString lib_name
= name
;
145 if ( (node
= m_loaded
.Find(name
.GetData())) )
146 return ((wxLibrary
*)node
->Data());
148 #if defined(__UNIX__)
149 lib_name
.Prepend("./lib");
152 printf("lib_name = %s\n", WXSTRINGCAST lib_name
);
154 void *handle
= dlopen(WXSTRINGCAST lib_name
, RTLD_LAZY
);
158 #elif defined(__WINDOWS__)
162 HMODULE handle
= LoadLibraryW(lib_name
);
164 HMODULE handle
= LoadLibraryA(lib_name
);
172 lib
= new wxLibrary((void *)handle
);
174 m_loaded
.Append(name
.GetData(), lib
);
178 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
180 wxNode
*node
= m_loaded
.First();
184 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);