1999-02-22 19:42:45 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2001-06-15 22:55:11 +00:00
|
|
|
// Name: wx/dynlib.h
|
|
|
|
// Purpose: Dynamic library loading classes
|
|
|
|
// Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
|
1999-02-22 19:42:45 +00:00
|
|
|
// Modified by:
|
|
|
|
// Created: 20/07/98
|
|
|
|
// RCS-ID: $Id$
|
2001-06-15 22:55:11 +00:00
|
|
|
// Copyright: (c) 1998 Guilhem Lavaux
|
1999-02-22 19:42:45 +00:00
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-15 00:23:28 +00:00
|
|
|
#ifndef _WX_DYNLIB_H__
|
|
|
|
#define _WX_DYNLIB_H__
|
1998-08-04 17:49:26 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
1999-06-07 20:49:35 +00:00
|
|
|
# pragma interface
|
1998-08-04 17:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
1999-08-20 22:52:21 +00:00
|
|
|
#include "wx/setup.h"
|
1999-02-23 17:51:46 +00:00
|
|
|
|
2001-12-19 07:09:58 +00:00
|
|
|
#if wxUSE_DYNAMIC_LOADER
|
|
|
|
|
|
|
|
#include "wx/dynload.h" // Use the new (version of) wxDynamicLibrary instead
|
|
|
|
|
|
|
|
#elif wxUSE_DYNLIB_CLASS
|
1999-02-23 17:51:46 +00:00
|
|
|
|
1999-08-20 22:52:21 +00:00
|
|
|
#include "wx/string.h"
|
|
|
|
#include "wx/list.h"
|
|
|
|
#include "wx/hash.h"
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-23 17:51:46 +00: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 20:49:35 +00: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 20:34:06 +00:00
|
|
|
#endif // !Unix or already have some HAVE_xxx defined
|
|
|
|
|
2000-01-26 22:35:48 +00: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 20:49:35 +00:00
|
|
|
# include <dlfcn.h>
|
1999-02-22 19:42:45 +00:00
|
|
|
typedef void *wxDllType;
|
1999-02-23 17:51:46 +00:00
|
|
|
#elif defined(HAVE_SHL_LOAD)
|
1999-06-07 20:49:35 +00:00
|
|
|
# include <dl.h>
|
1999-02-23 17:51:46 +00:00
|
|
|
typedef shl_t wxDllType;
|
1999-02-22 19:42:45 +00:00
|
|
|
#elif defined(__WINDOWS__)
|
2000-01-24 20:19:39 +00:00
|
|
|
# include <windows.h> // needed to get HMODULE
|
1999-02-22 19:42:45 +00:00
|
|
|
typedef HMODULE wxDllType;
|
2001-07-15 18:47:13 +00:00
|
|
|
#elif defined(__DARWIN__)
|
2001-02-15 21:51:14 +00:00
|
|
|
typedef void *wxDllType;
|
1999-02-22 19:42:45 +00:00
|
|
|
#elif defined(__WXMAC__)
|
|
|
|
typedef CFragConnectionID wxDllType;
|
|
|
|
#else
|
1999-06-07 20:49:35 +00:00
|
|
|
# error "wxLibrary can't be compiled on this platform, sorry."
|
1999-02-22 19:42:45 +00:00
|
|
|
#endif // OS
|
|
|
|
|
1999-07-10 00:50:16 +00: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 08:41:07 +00:00
|
|
|
#endif
|
1998-09-10 11:41:14 +00:00
|
|
|
|
1999-06-07 20:49:35 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2001-06-15 22:55:11 +00:00
|
|
|
// wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
|
1999-06-07 20:49:35 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2000-01-24 20:19:39 +00:00
|
|
|
|
2001-06-15 22:55:11 +00: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 20:49:35 +00:00
|
|
|
*/
|
2000-05-17 16:45:01 +00:00
|
|
|
class WXDLLEXPORT wxDllLoader
|
1999-06-07 20:49:35 +00:00
|
|
|
{
|
2000-01-24 20:19:39 +00:00
|
|
|
public:
|
2001-06-15 22:55:11 +00: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
|
|
|
|
*/
|
2001-12-19 07:09:58 +00:00
|
|
|
static wxDllType LoadLibrary(const wxString& libname, bool *success = 0);
|
2001-06-15 22:55:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This function unloads the shared library previously loaded with
|
|
|
|
LoadLibrary
|
2000-01-24 20:19:39 +00:00
|
|
|
*/
|
|
|
|
static void UnloadLibrary(wxDllType dll);
|
2001-06-15 22:55:11 +00: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 20:19:39 +00:00
|
|
|
*/
|
2001-12-19 13:10:10 +00:00
|
|
|
static void *GetSymbol(wxDllType dllHandle,
|
|
|
|
const wxString &name,
|
|
|
|
bool *success = NULL);
|
2000-01-24 20:19:39 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
// return the standard DLL extension (with leading dot) for this platform
|
2001-12-19 07:09:58 +00:00
|
|
|
static const wxString &GetDllExt() { return ms_dllext; }
|
2000-07-15 19:51:35 +00:00
|
|
|
|
2000-01-24 20:19:39 +00:00
|
|
|
private:
|
2001-06-15 22:55:11 +00:00
|
|
|
// forbid construction of objects
|
2000-01-24 20:19:39 +00:00
|
|
|
wxDllLoader();
|
2001-12-19 07:09:58 +00:00
|
|
|
static const wxString ms_dllext;
|
1999-06-07 20:49:35 +00:00
|
|
|
};
|
|
|
|
|
2001-06-15 22:05:31 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDynamicLibrary - friendly interface to wxDllLoader
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
class WXDLLEXPORT wxDynamicLibrary
|
2001-06-15 22:05:31 +00:00
|
|
|
{
|
|
|
|
public:
|
2001-06-15 22:55:11 +00:00
|
|
|
// ctors
|
2001-06-15 22:05:31 +00:00
|
|
|
wxDynamicLibrary() { m_library = 0; }
|
|
|
|
wxDynamicLibrary(const wxString& name) { Load(name); }
|
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
// return TRUE if the library was loaded successfully
|
2001-06-15 22:05:31 +00:00
|
|
|
bool IsLoaded() const { return m_library != 0; }
|
2001-06-15 22:55:11 +00:00
|
|
|
operator bool() const { return IsLoaded(); }
|
2001-06-15 22:05:31 +00:00
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
// load the library with the given name (full or not), return TRUE on
|
|
|
|
// success
|
2001-06-15 22:05:31 +00:00
|
|
|
bool Load(const wxString& name)
|
|
|
|
{
|
|
|
|
m_library = wxDllLoader::LoadLibrary(name);
|
|
|
|
|
|
|
|
return IsLoaded();
|
|
|
|
}
|
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
// unload the library, also done automatically in dtor
|
2001-06-15 22:05:31 +00:00
|
|
|
void Unload()
|
|
|
|
{
|
|
|
|
if ( IsLoaded() )
|
2001-06-15 22:55:11 +00:00
|
|
|
wxDllLoader::UnloadLibrary(m_library);
|
2001-06-15 22:05:31 +00:00
|
|
|
}
|
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
// load a symbol from the library, return NULL if an error occured or
|
|
|
|
// symbol wasn't found
|
2001-06-15 22:05:31 +00: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 22:55:11 +00:00
|
|
|
// unload the library
|
|
|
|
//
|
|
|
|
// NB: dtor is not virtual, don't derive from this class
|
2001-06-15 22:05:31 +00:00
|
|
|
~wxDynamicLibrary() { Unload(); }
|
|
|
|
|
|
|
|
private:
|
2001-06-15 22:55:11 +00:00
|
|
|
// the handle to DLL or NULL
|
2001-06-15 22:05:31 +00:00
|
|
|
wxDllType m_library;
|
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
// no copy ctor/assignment operators (or we'd try to unload the library
|
|
|
|
// twice)
|
2001-07-04 12:23:32 +00:00
|
|
|
DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
|
2001-06-15 22:55:11 +00:00
|
|
|
};
|
2001-06-15 22:05:31 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
// wxLibrary
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
|
2000-05-17 16:45:01 +00:00
|
|
|
class WXDLLEXPORT wxLibrary : public wxObject
|
1999-02-22 19:42:45 +00:00
|
|
|
{
|
|
|
|
public:
|
1999-02-23 17:51:46 +00:00
|
|
|
wxLibrary(wxDllType handle);
|
2001-06-15 22:55:11 +00:00
|
|
|
virtual ~wxLibrary();
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// Get a symbol from the dynamic library
|
|
|
|
void *GetSymbol(const wxString& symbname);
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// Create the object whose classname is "name"
|
|
|
|
wxObject *CreateObject(const wxString& name);
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
protected:
|
|
|
|
void PrepareClasses(wxClassInfo *first);
|
1998-09-01 17:17:05 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
wxDllType m_handle;
|
1999-06-07 20:49:35 +00:00
|
|
|
|
2001-06-15 22:55:11 +00:00
|
|
|
public:
|
|
|
|
wxHashTable classTable;
|
|
|
|
};
|
1999-06-07 20:49:35 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
// wxLibraries
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2000-05-17 16:45:01 +00:00
|
|
|
class WXDLLEXPORT wxLibraries
|
1999-02-22 19:42:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxLibraries();
|
|
|
|
~wxLibraries();
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// caller is responsible for deleting the returned pointer if !NULL
|
|
|
|
wxLibrary *LoadLibrary(const wxString& basename);
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
wxObject *CreateObject(const wxString& name);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
wxList m_loaded;
|
1998-08-04 17:49:26 +00:00
|
|
|
};
|
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
// Global variables
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
|
2001-11-06 05:10:16 +00:00
|
|
|
extern WXDLLEXPORT_DATA(wxLibraries) wxTheLibraries;
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
// Interesting defines
|
1999-02-22 19:42:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1998-09-01 17:17:05 +00:00
|
|
|
#define WXDLL_ENTRY_FUNCTION() \
|
2001-10-31 12:38:42 +00:00
|
|
|
extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
|
2001-10-29 14:35:04 +00:00
|
|
|
const wxClassInfo *wxGetClassFirst() { \
|
1998-09-17 17:30:13 +00:00
|
|
|
return wxClassInfo::GetFirst(); \
|
1998-09-01 17:17:05 +00:00
|
|
|
}
|
1998-08-04 17:49:26 +00:00
|
|
|
|
1999-02-23 17:51:46 +00:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
|
|
|
|
1999-02-22 19:42:45 +00:00
|
|
|
#endif // _WX_DYNLIB_H__
|