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
);
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__)
119 return dlsym(m_handle
, WXSTRINGCAST symbname
);
120 #elif defined( __WINDOWS__ )
121 return GetProcAddress((HINSTANCE
) m_handle
, WXSTRINGCAST symbname
);
122 #elif defined( __WXMAC__ )
124 CFragSymbolClass symClass
;
127 strcpy( (char*) symName
, symbname
) ;
128 c2pstr( (char*) symName
) ;
130 if ( FindSymbol( (CFragConnectionID
) m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
138 // ---------------------------------------------------------------------------
139 // wxLibraries (only one instance should normally exist)
140 // ---------------------------------------------------------------------------
142 wxLibraries::wxLibraries()
146 wxLibraries::~wxLibraries()
148 wxNode
*node
= m_loaded
.First();
151 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
158 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
160 wxString lib_name
= name
;
163 wxClassInfo
*old_sm_first
;
165 if ( (node
= m_loaded
.Find(name
.GetData())) )
166 return ((wxLibrary
*)node
->Data());
168 // If DLL shares data, this is necessary.
169 old_sm_first
= wxClassInfo::sm_first
;
170 wxClassInfo::sm_first
= NULL
;
172 #if defined(__UNIX__)
173 lib_name
.Prepend("./lib");
176 printf("lib_name = %s\n", WXSTRINGCAST lib_name
);
178 void *handle
= dlopen(WXSTRINGCAST lib_name
, RTLD_LAZY
);
180 printf("error = %s\n", dlerror());
184 #elif defined(__WINDOWS__)
188 HMODULE handle
= LoadLibraryW(lib_name
);
191 HMODULE handle
= ::LoadLibrary(lib_name
);
193 HMODULE handle
= LoadLibraryA(lib_name
);
198 #elif defined(__WXMAC__)
200 CFragConnectionID handle
;
204 wxMacPathToFSSpec( lib_name
, &myFSSpec
) ;
205 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
206 myErrName
) != noErr
)
208 p2cstr( myErrName
) ;
209 wxASSERT_MSG( 1 , (char*)myErrName
) ;
216 lib
= new wxLibrary((void *)handle
);
218 wxClassInfo::sm_first
= old_sm_first
;
220 m_loaded
.Append(name
.GetData(), lib
);
224 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
226 wxNode
*node
= m_loaded
.First();
230 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);