]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dynlib.cpp | |
3 | // Purpose: Dynamic library management | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 20/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_DYNLIB_H__ | |
13 | #define _WX_DYNLIB_H__ | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | # pragma interface | |
17 | #endif | |
18 | ||
19 | #include "wx/setup.h" | |
20 | ||
21 | #if wxUSE_DYNLIB_CLASS | |
22 | ||
23 | #include "wx/string.h" | |
24 | #include "wx/list.h" | |
25 | #include "wx/hash.h" | |
26 | ||
27 | // this is normally done by configure, but I leave it here for now... | |
28 | #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD)) | |
29 | # if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__) || defined(__FREEBSD__) | |
30 | # define HAVE_DLOPEN | |
31 | # elif defined(__HPUX__) | |
32 | # define HAVE_SHL_LOAD | |
33 | # endif // Unix flavour | |
34 | #endif // !Unix or already have some HAVE_xxx defined | |
35 | ||
36 | #if defined(HAVE_DLOPEN) | |
37 | # include <dlfcn.h> | |
38 | typedef void *wxDllType; | |
39 | #elif defined(HAVE_SHL_LOAD) | |
40 | # include <dl.h> | |
41 | typedef shl_t wxDllType; | |
42 | #elif defined(__WINDOWS__) | |
43 | # include <windows.h> // needed to get HMODULE | |
44 | typedef HMODULE wxDllType; | |
45 | #elif defined(__OS2__) | |
46 | # define INCL_DOS | |
47 | # include <os2.h> | |
48 | typedef HMODULE wxDllType; | |
49 | #elif defined(__WXMAC__) | |
50 | typedef CFragConnectionID wxDllType; | |
51 | #else | |
52 | # error "wxLibrary can't be compiled on this platform, sorry." | |
53 | #endif // OS | |
54 | ||
55 | // LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader method | |
56 | // should be called LoadLibrary, not LoadLibraryA or LoadLibraryW! | |
57 | #if defined(__WIN32__) && defined(LoadLibrary) | |
58 | # include "wx/msw/winundef.h" | |
59 | #endif | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // wxDllLoader | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | /** wxDllLoader is a class providing an interface similar to unix's | |
66 | dlopen(). It is used by the wxLibrary framework and manages the | |
67 | actual loading of DLLs and the resolving of symbols in them. | |
68 | There are no instances of this class, it simply serves as a | |
69 | namespace for its static member functions. | |
70 | */ | |
71 | class wxDllLoader | |
72 | { | |
73 | public: | |
74 | /** This function loads a shared library into memory, with libname | |
75 | being the basename of the library, without the filename | |
76 | extension. No initialisation of the library will be done. | |
77 | @param libname Name of the shared object to load. | |
78 | @param success Must point to a bool variable which will be set to TRUE or FALSE. | |
79 | @return A handle to the loaded DLL. Use success parameter to test if it is valid. | |
80 | */ | |
81 | static wxDllType LoadLibrary(const wxString & libname, bool *success = NULL); | |
82 | /** This function unloads the shared library. */ | |
83 | static void UnloadLibrary(wxDllType dll); | |
84 | /** This function returns a valid handle for the main program | |
85 | itself. */ | |
86 | static wxDllType GetProgramHandle(void); | |
87 | /** This function resolves a symbol in a loaded DLL, such as a | |
88 | variable or function name. | |
89 | @param dllHandle Handle of the DLL, as returned by LoadDll(). | |
90 | @param name Name of the symbol. | |
91 | @return A pointer to the symbol. | |
92 | */ | |
93 | static void * GetSymbol(wxDllType dllHandle, const wxString &name); | |
94 | ||
95 | private: | |
96 | /// forbid construction of objects | |
97 | wxDllLoader(); | |
98 | }; | |
99 | ||
100 | // ---------------------------------------------------------------------------- | |
101 | // wxLibrary | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
104 | class wxLibrary : public wxObject | |
105 | { | |
106 | public: | |
107 | wxHashTable classTable; | |
108 | ||
109 | public: | |
110 | wxLibrary(wxDllType handle); | |
111 | ~wxLibrary(); | |
112 | ||
113 | // Get a symbol from the dynamic library | |
114 | void *GetSymbol(const wxString& symbname); | |
115 | ||
116 | // Create the object whose classname is "name" | |
117 | wxObject *CreateObject(const wxString& name); | |
118 | ||
119 | protected: | |
120 | void PrepareClasses(wxClassInfo *first); | |
121 | ||
122 | wxDllType m_handle; | |
123 | }; | |
124 | ||
125 | ||
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // wxLibraries | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | class wxLibraries | |
132 | { | |
133 | public: | |
134 | wxLibraries(); | |
135 | ~wxLibraries(); | |
136 | ||
137 | // caller is responsible for deleting the returned pointer if !NULL | |
138 | wxLibrary *LoadLibrary(const wxString& basename); | |
139 | ||
140 | wxObject *CreateObject(const wxString& name); | |
141 | ||
142 | protected: | |
143 | wxList m_loaded; | |
144 | }; | |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
147 | // Global variables | |
148 | // ---------------------------------------------------------------------------- | |
149 | ||
150 | extern wxLibraries wxTheLibraries; | |
151 | ||
152 | // ---------------------------------------------------------------------------- | |
153 | // Interesting defines | |
154 | // ---------------------------------------------------------------------------- | |
155 | ||
156 | #define WXDLL_ENTRY_FUNCTION() \ | |
157 | extern "C" wxClassInfo *wxGetClassFirst(); \ | |
158 | wxClassInfo *wxGetClassFirst() { \ | |
159 | return wxClassInfo::GetFirst(); \ | |
160 | } | |
161 | ||
162 | #endif // wxUSE_DYNLIB_CLASS | |
163 | ||
164 | #endif // _WX_DYNLIB_H__ |