]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynlib.h
Project file update
[wxWidgets.git] / include / wx / dynlib.h
CommitLineData
7b0bfbb2 1/////////////////////////////////////////////////////////////////////////////
33a8563e
VZ
2// Name: wx/dynlib.h
3// Purpose: Dynamic library loading classes
4// Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
7b0bfbb2
VZ
5// Modified by:
6// Created: 20/07/98
7// RCS-ID: $Id$
33a8563e 8// Copyright: (c) 1998 Guilhem Lavaux
7b0bfbb2
VZ
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_DYNLIB_H__
13#define _WX_DYNLIB_H__
7a4b9130
GL
14
15#ifdef __GNUG__
a585ca5c 16# pragma interface
7a4b9130
GL
17#endif
18
ed58dbea 19#include "wx/setup.h"
8a0d4cf6 20
0b9ab0bd
RL
21#if wxUSE_DYNAMIC_LOADER
22
23#include "wx/dynload.h" // Use the new (version of) wxDynamicLibrary instead
24
25#elif wxUSE_DYNLIB_CLASS
8a0d4cf6 26
ed58dbea
RR
27#include "wx/string.h"
28#include "wx/list.h"
29#include "wx/hash.h"
7a4b9130 30
8a0d4cf6
VZ
31// this is normally done by configure, but I leave it here for now...
32#if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD))
a585ca5c
KB
33# if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__) || defined(__FREEBSD__)
34# define HAVE_DLOPEN
35# elif defined(__HPUX__)
36# define HAVE_SHL_LOAD
37# endif // Unix flavour
3e4efd7c
VZ
38#endif // !Unix or already have some HAVE_xxx defined
39
1a787c5d
SN
40// Note: WXPM/EMX has to be tested first, since we want to use
41// native version, even if configure detected presence of DLOPEN.
42#if defined(__WXPM__) || defined(__EMX__)
43# define INCL_DOS
44# include <os2.h>
45 typedef HMODULE wxDllType;
46#elif defined(HAVE_DLOPEN)
a585ca5c 47# include <dlfcn.h>
7b0bfbb2 48 typedef void *wxDllType;
8a0d4cf6 49#elif defined(HAVE_SHL_LOAD)
a585ca5c 50# include <dl.h>
8a0d4cf6 51 typedef shl_t wxDllType;
7b0bfbb2 52#elif defined(__WINDOWS__)
3c1a88d8 53# include <windows.h> // needed to get HMODULE
7b0bfbb2 54 typedef HMODULE wxDllType;
f11bdd03 55#elif defined(__DARWIN__)
03e11df5 56 typedef void *wxDllType;
7b0bfbb2 57#elif defined(__WXMAC__)
fd1bc7fc 58 typedef void *wxDllType;
7b0bfbb2 59#else
a585ca5c 60# error "wxLibrary can't be compiled on this platform, sorry."
7b0bfbb2
VZ
61#endif // OS
62
7cc98b3e
VZ
63// LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader method
64// should be called LoadLibrary, not LoadLibraryA or LoadLibraryW!
65#if defined(__WIN32__) && defined(LoadLibrary)
66# include "wx/msw/winundef.h"
7c0f3a1e 67#endif
0c32066b 68
a585ca5c 69// ----------------------------------------------------------------------------
33a8563e 70// wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
a585ca5c 71// ----------------------------------------------------------------------------
3c1a88d8 72
33a8563e
VZ
73/*
74 wxDllLoader is a class providing an interface similar to unix's dlopen().
75 It is used by wxDynamicLibrary wxLibrary and manages the actual loading of
76 DLLs and the resolving of symbols in them. There are no instances of this
77 class, it simply serves as a namespace for its static member functions.
a585ca5c 78*/
b97a2c53 79class WXDLLEXPORT wxDllLoader
a585ca5c 80{
3c1a88d8 81public:
33a8563e
VZ
82 /*
83 This function loads the shared library libname into memory.
84
85 libname may be either the full path to the file or just the filename in
86 which case the library is searched for in all standard locations
87 (use GetDllExt() to construct the filename)
88
89 if success pointer is not NULL, it will be filled with TRUE if everything
90 went ok and FALSE otherwise
91 */
0b9ab0bd 92 static wxDllType LoadLibrary(const wxString& libname, bool *success = 0);
33a8563e
VZ
93
94 /*
95 This function unloads the shared library previously loaded with
96 LoadLibrary
3c1a88d8 97 */
3c1a88d8 98 static void UnloadLibrary(wxDllType dll);
33a8563e
VZ
99
100 /*
101 This function returns a valid handle for the main program
102 itself or NULL if back linking is not supported by the current platform
103 (e.g. Win32).
104 */
105 static wxDllType GetProgramHandle();
106
107 /*
108 This function resolves a symbol in a loaded DLL, such as a
109 variable or function name.
110
111 dllHandle Handle of the DLL, as returned by LoadDll().
112 name Name of the symbol.
113
114 Returns the pointer to the symbol or NULL on error.
3c1a88d8 115 */
c74d8df0
VZ
116 static void *GetSymbol(wxDllType dllHandle,
117 const wxString &name,
118 bool *success = NULL);
3c1a88d8 119
f6bcfd97 120 // return the standard DLL extension (with leading dot) for this platform
0b9ab0bd 121 static const wxString &GetDllExt() { return ms_dllext; }
f6bcfd97 122
3c1a88d8 123private:
33a8563e 124 // forbid construction of objects
3c1a88d8 125 wxDllLoader();
0b9ab0bd 126 static const wxString ms_dllext;
a585ca5c
KB
127};
128
181b4068
VS
129// ----------------------------------------------------------------------------
130// wxDynamicLibrary - friendly interface to wxDllLoader
131// ----------------------------------------------------------------------------
132
33a8563e 133class WXDLLEXPORT wxDynamicLibrary
181b4068
VS
134{
135public:
33a8563e 136 // ctors
181b4068
VS
137 wxDynamicLibrary() { m_library = 0; }
138 wxDynamicLibrary(const wxString& name) { Load(name); }
139
33a8563e 140 // return TRUE if the library was loaded successfully
181b4068 141 bool IsLoaded() const { return m_library != 0; }
33a8563e 142 operator bool() const { return IsLoaded(); }
181b4068 143
33a8563e
VZ
144 // load the library with the given name (full or not), return TRUE on
145 // success
181b4068
VS
146 bool Load(const wxString& name)
147 {
148 m_library = wxDllLoader::LoadLibrary(name);
149
150 return IsLoaded();
151 }
152
33a8563e 153 // unload the library, also done automatically in dtor
181b4068
VS
154 void Unload()
155 {
156 if ( IsLoaded() )
33a8563e 157 wxDllLoader::UnloadLibrary(m_library);
181b4068
VS
158 }
159
33a8563e
VZ
160 // load a symbol from the library, return NULL if an error occured or
161 // symbol wasn't found
181b4068
VS
162 void *GetSymbol(const wxString& name) const
163 {
164 wxCHECK_MSG( IsLoaded(), NULL,
165 _T("can't load symbol from unloaded library") );
166
167 return wxDllLoader::GetSymbol(m_library, name);
168 }
169
33a8563e
VZ
170 // unload the library
171 //
172 // NB: dtor is not virtual, don't derive from this class
181b4068
VS
173 ~wxDynamicLibrary() { Unload(); }
174
175private:
33a8563e 176 // the handle to DLL or NULL
181b4068 177 wxDllType m_library;
181b4068 178
33a8563e
VZ
179 // no copy ctor/assignment operators (or we'd try to unload the library
180 // twice)
1a0d517e 181 DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
33a8563e 182};
181b4068 183
7b0bfbb2 184// ----------------------------------------------------------------------------
7a4b9130 185// wxLibrary
7b0bfbb2 186// ----------------------------------------------------------------------------
7a4b9130 187
b97a2c53 188class WXDLLEXPORT wxLibrary : public wxObject
7b0bfbb2 189{
7b0bfbb2 190public:
8a0d4cf6 191 wxLibrary(wxDllType handle);
33a8563e 192 virtual ~wxLibrary();
7a4b9130 193
7b0bfbb2
VZ
194 // Get a symbol from the dynamic library
195 void *GetSymbol(const wxString& symbname);
7a4b9130 196
7b0bfbb2
VZ
197 // Create the object whose classname is "name"
198 wxObject *CreateObject(const wxString& name);
7a4b9130 199
7b0bfbb2
VZ
200protected:
201 void PrepareClasses(wxClassInfo *first);
f4a8c29f 202
7b0bfbb2 203 wxDllType m_handle;
a585ca5c 204
33a8563e
VZ
205public:
206 wxHashTable classTable;
207};
a585ca5c 208
7b0bfbb2 209// ----------------------------------------------------------------------------
7a4b9130 210// wxLibraries
7b0bfbb2
VZ
211// ----------------------------------------------------------------------------
212
b97a2c53 213class WXDLLEXPORT wxLibraries
7b0bfbb2
VZ
214{
215public:
216 wxLibraries();
217 ~wxLibraries();
7a4b9130 218
7b0bfbb2
VZ
219 // caller is responsible for deleting the returned pointer if !NULL
220 wxLibrary *LoadLibrary(const wxString& basename);
7a4b9130 221
7b0bfbb2
VZ
222 wxObject *CreateObject(const wxString& name);
223
224protected:
225 wxList m_loaded;
7a4b9130
GL
226};
227
7b0bfbb2 228// ----------------------------------------------------------------------------
7a4b9130 229// Global variables
7b0bfbb2 230// ----------------------------------------------------------------------------
7a4b9130 231
0d6e7df4 232extern WXDLLEXPORT_DATA(wxLibraries) wxTheLibraries;
7a4b9130 233
7b0bfbb2 234// ----------------------------------------------------------------------------
7a4b9130 235// Interesting defines
7b0bfbb2 236// ----------------------------------------------------------------------------
7a4b9130 237
f4a8c29f 238#define WXDLL_ENTRY_FUNCTION() \
ad8b3990 239extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
1b733817 240const wxClassInfo *wxGetClassFirst() { \
856d2e52 241 return wxClassInfo::GetFirst(); \
f4a8c29f 242}
7a4b9130 243
8a0d4cf6
VZ
244#endif // wxUSE_DYNLIB_CLASS
245
7b0bfbb2 246#endif // _WX_DYNLIB_H__