1999-02-22 14:42:45 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2001-06-15 18:55:11 -04:00
|
|
|
// Name: wx/dynlib.h
|
|
|
|
// Purpose: Dynamic library loading classes
|
|
|
|
// Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
|
1999-02-22 14:42:45 -05:00
|
|
|
// Modified by:
|
|
|
|
// Created: 20/07/98
|
|
|
|
// RCS-ID: $Id$
|
2001-06-15 18:55:11 -04:00
|
|
|
// Copyright: (c) 1998 Guilhem Lavaux
|
1999-02-22 14:42:45 -05:00
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-14 20:23:28 -04:00
|
|
|
#ifndef _WX_DYNLIB_H__
|
|
|
|
#define _WX_DYNLIB_H__
|
1998-08-04 13:49:26 -04:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
1999-06-07 16:49:35 -04:00
|
|
|
# pragma interface
|
1998-08-04 13:49:26 -04:00
|
|
|
#endif
|
|
|
|
|
1999-08-20 18:52:21 -04:00
|
|
|
#include "wx/setup.h"
|
1999-02-23 12:51:46 -05:00
|
|
|
|
|
|
|
#if wxUSE_DYNLIB_CLASS
|
|
|
|
|
1999-08-20 18:52:21 -04:00
|
|
|
#include "wx/string.h"
|
|
|
|
#include "wx/list.h"
|
|
|
|
#include "wx/hash.h"
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-23 12:51:46 -05:00
|
|
|
// this is normally done by configure, but I leave it here for now...
|
|
|
|
#if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD))
|
1999-06-07 16:49:35 -04:00
|
|
|
# if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__) || defined(__FREEBSD__)
|
|
|
|
# define HAVE_DLOPEN
|
|
|
|
# elif defined(__HPUX__)
|
|
|
|
# define HAVE_SHL_LOAD
|
|
|
|
# endif // Unix flavour
|
1999-02-22 15:34:06 -05:00
|
|
|
#endif // !Unix or already have some HAVE_xxx defined
|
|
|
|
|
2000-01-26 17:35:48 -05:00
|
|
|
// Note: WXPM/EMX has to be tested first, since we want to use
|
|
|
|
// native version, even if configure detected presence of DLOPEN.
|
|
|
|
#if defined(__WXPM__) || defined(__EMX__)
|
|
|
|
# define INCL_DOS
|
|
|
|
# include <os2.h>
|
|
|
|
typedef HMODULE wxDllType;
|
|
|
|
#elif defined(HAVE_DLOPEN)
|
1999-06-07 16:49:35 -04:00
|
|
|
# include <dlfcn.h>
|
1999-02-22 14:42:45 -05:00
|
|
|
typedef void *wxDllType;
|
1999-02-23 12:51:46 -05:00
|
|
|
#elif defined(HAVE_SHL_LOAD)
|
1999-06-07 16:49:35 -04:00
|
|
|
# include <dl.h>
|
1999-02-23 12:51:46 -05:00
|
|
|
typedef shl_t wxDllType;
|
1999-02-22 14:42:45 -05:00
|
|
|
#elif defined(__WINDOWS__)
|
2000-01-24 15:19:39 -05:00
|
|
|
# include <windows.h> // needed to get HMODULE
|
1999-02-22 14:42:45 -05:00
|
|
|
typedef HMODULE wxDllType;
|
2001-07-15 14:47:13 -04:00
|
|
|
#elif defined(__DARWIN__)
|
2001-02-15 16:51:14 -05:00
|
|
|
typedef void *wxDllType;
|
1999-02-22 14:42:45 -05:00
|
|
|
#elif defined(__WXMAC__)
|
|
|
|
typedef CFragConnectionID wxDllType;
|
|
|
|
#else
|
1999-06-07 16:49:35 -04:00
|
|
|
# error "wxLibrary can't be compiled on this platform, sorry."
|
1999-02-22 14:42:45 -05:00
|
|
|
#endif // OS
|
|
|
|
|
1999-07-09 20:50:16 -04:00
|
|
|
// LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader method
|
|
|
|
// should be called LoadLibrary, not LoadLibraryA or LoadLibraryW!
|
|
|
|
#if defined(__WIN32__) && defined(LoadLibrary)
|
|
|
|
# include "wx/msw/winundef.h"
|
1999-06-29 04:41:07 -04:00
|
|
|
#endif
|
1998-09-10 07:41:14 -04:00
|
|
|
|
1999-06-07 16:49:35 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2001-06-15 18:55:11 -04:00
|
|
|
// wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
|
1999-06-07 16:49:35 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2000-01-24 15:19:39 -05:00
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
/*
|
|
|
|
wxDllLoader is a class providing an interface similar to unix's dlopen().
|
|
|
|
It is used by wxDynamicLibrary wxLibrary and manages the actual loading of
|
|
|
|
DLLs and the resolving of symbols in them. There are no instances of this
|
|
|
|
class, it simply serves as a namespace for its static member functions.
|
1999-06-07 16:49:35 -04:00
|
|
|
*/
|
2000-05-17 12:45:01 -04:00
|
|
|
class WXDLLEXPORT wxDllLoader
|
1999-06-07 16:49:35 -04:00
|
|
|
{
|
2000-01-24 15:19:39 -05:00
|
|
|
public:
|
2001-06-15 18:55:11 -04:00
|
|
|
/*
|
|
|
|
This function loads the shared library libname into memory.
|
|
|
|
|
|
|
|
libname may be either the full path to the file or just the filename in
|
|
|
|
which case the library is searched for in all standard locations
|
|
|
|
(use GetDllExt() to construct the filename)
|
|
|
|
|
|
|
|
if success pointer is not NULL, it will be filled with TRUE if everything
|
|
|
|
went ok and FALSE otherwise
|
|
|
|
*/
|
|
|
|
static wxDllType LoadLibrary(const wxString& libname, bool *success = NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
This function unloads the shared library previously loaded with
|
|
|
|
LoadLibrary
|
2000-01-24 15:19:39 -05:00
|
|
|
*/
|
|
|
|
static void UnloadLibrary(wxDllType dll);
|
2001-06-15 18:55:11 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
This function returns a valid handle for the main program
|
|
|
|
itself or NULL if back linking is not supported by the current platform
|
|
|
|
(e.g. Win32).
|
|
|
|
*/
|
|
|
|
static wxDllType GetProgramHandle();
|
|
|
|
|
|
|
|
/*
|
|
|
|
This function resolves a symbol in a loaded DLL, such as a
|
|
|
|
variable or function name.
|
|
|
|
|
|
|
|
dllHandle Handle of the DLL, as returned by LoadDll().
|
|
|
|
name Name of the symbol.
|
|
|
|
|
|
|
|
Returns the pointer to the symbol or NULL on error.
|
2000-01-24 15:19:39 -05:00
|
|
|
*/
|
|
|
|
static void * GetSymbol(wxDllType dllHandle, const wxString &name);
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
// return the standard DLL extension (with leading dot) for this platform
|
|
|
|
static wxString GetDllExt();
|
|
|
|
|
2000-01-24 15:19:39 -05:00
|
|
|
private:
|
2001-06-15 18:55:11 -04:00
|
|
|
// forbid construction of objects
|
2000-01-24 15:19:39 -05:00
|
|
|
wxDllLoader();
|
1999-06-07 16:49:35 -04:00
|
|
|
};
|
|
|
|
|
2001-06-15 18:05:31 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDynamicLibrary - friendly interface to wxDllLoader
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
class WXDLLEXPORT wxDynamicLibrary
|
2001-06-15 18:05:31 -04:00
|
|
|
{
|
|
|
|
public:
|
2001-06-15 18:55:11 -04:00
|
|
|
// ctors
|
2001-06-15 18:05:31 -04:00
|
|
|
wxDynamicLibrary() { m_library = 0; }
|
|
|
|
wxDynamicLibrary(const wxString& name) { Load(name); }
|
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
// return TRUE if the library was loaded successfully
|
2001-06-15 18:05:31 -04:00
|
|
|
bool IsLoaded() const { return m_library != 0; }
|
2001-06-15 18:55:11 -04:00
|
|
|
operator bool() const { return IsLoaded(); }
|
2001-06-15 18:05:31 -04:00
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
// load the library with the given name (full or not), return TRUE on
|
|
|
|
// success
|
2001-06-15 18:05:31 -04:00
|
|
|
bool Load(const wxString& name)
|
|
|
|
{
|
|
|
|
m_library = wxDllLoader::LoadLibrary(name);
|
|
|
|
|
|
|
|
return IsLoaded();
|
|
|
|
}
|
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
// unload the library, also done automatically in dtor
|
2001-06-15 18:05:31 -04:00
|
|
|
void Unload()
|
|
|
|
{
|
|
|
|
if ( IsLoaded() )
|
2001-06-15 18:55:11 -04:00
|
|
|
wxDllLoader::UnloadLibrary(m_library);
|
2001-06-15 18:05:31 -04:00
|
|
|
}
|
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
// load a symbol from the library, return NULL if an error occured or
|
|
|
|
// symbol wasn't found
|
2001-06-15 18:05:31 -04:00
|
|
|
void *GetSymbol(const wxString& name) const
|
|
|
|
{
|
|
|
|
wxCHECK_MSG( IsLoaded(), NULL,
|
|
|
|
_T("can't load symbol from unloaded library") );
|
|
|
|
|
|
|
|
return wxDllLoader::GetSymbol(m_library, name);
|
|
|
|
}
|
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
// unload the library
|
|
|
|
//
|
|
|
|
// NB: dtor is not virtual, don't derive from this class
|
2001-06-15 18:05:31 -04:00
|
|
|
~wxDynamicLibrary() { Unload(); }
|
|
|
|
|
|
|
|
private:
|
2001-06-15 18:55:11 -04:00
|
|
|
// the handle to DLL or NULL
|
2001-06-15 18:05:31 -04:00
|
|
|
wxDllType m_library;
|
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
// no copy ctor/assignment operators (or we'd try to unload the library
|
|
|
|
// twice)
|
2001-07-04 08:23:32 -04:00
|
|
|
DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
|
2001-06-15 18:55:11 -04:00
|
|
|
};
|
2001-06-15 18:05:31 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
// wxLibrary
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
|
2000-05-17 12:45:01 -04:00
|
|
|
class WXDLLEXPORT wxLibrary : public wxObject
|
1999-02-22 14:42:45 -05:00
|
|
|
{
|
|
|
|
public:
|
1999-02-23 12:51:46 -05:00
|
|
|
wxLibrary(wxDllType handle);
|
2001-06-15 18:55:11 -04:00
|
|
|
virtual ~wxLibrary();
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// Get a symbol from the dynamic library
|
|
|
|
void *GetSymbol(const wxString& symbname);
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// Create the object whose classname is "name"
|
|
|
|
wxObject *CreateObject(const wxString& name);
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
protected:
|
|
|
|
void PrepareClasses(wxClassInfo *first);
|
1998-09-01 13:17:05 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
wxDllType m_handle;
|
1999-06-07 16:49:35 -04:00
|
|
|
|
2001-06-15 18:55:11 -04:00
|
|
|
public:
|
|
|
|
wxHashTable classTable;
|
|
|
|
};
|
1999-06-07 16:49:35 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
// wxLibraries
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2000-05-17 12:45:01 -04:00
|
|
|
class WXDLLEXPORT wxLibraries
|
1999-02-22 14:42:45 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxLibraries();
|
|
|
|
~wxLibraries();
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// caller is responsible for deleting the returned pointer if !NULL
|
|
|
|
wxLibrary *LoadLibrary(const wxString& basename);
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
wxObject *CreateObject(const wxString& name);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
wxList m_loaded;
|
1998-08-04 13:49:26 -04:00
|
|
|
};
|
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
// Global variables
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
|
2001-11-06 00:10:16 -05:00
|
|
|
extern WXDLLEXPORT_DATA(wxLibraries) wxTheLibraries;
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
// Interesting defines
|
1999-02-22 14:42:45 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1998-09-01 13:17:05 -04:00
|
|
|
#define WXDLL_ENTRY_FUNCTION() \
|
2001-10-31 07:38:42 -05:00
|
|
|
extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
|
2001-10-29 09:35:04 -05:00
|
|
|
const wxClassInfo *wxGetClassFirst() { \
|
1998-09-17 13:30:13 -04:00
|
|
|
return wxClassInfo::GetFirst(); \
|
1998-09-01 13:17:05 -04:00
|
|
|
}
|
1998-08-04 13:49:26 -04:00
|
|
|
|
1999-02-23 12:51:46 -05:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
|
|
|
|
1999-02-22 14:42:45 -05:00
|
|
|
#endif // _WX_DYNLIB_H__
|