Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C++

DynObj - C++ Cross Platform Plugin Objects

Rate me:
Please Sign up or sign in to vote.
4.95/5 (34 votes)
27 Sep 200727 min read 141.6K   3.4K   132  
DynObj is an open source library that provides a C++ application with run-time class loading facilities
#ifndef PILIB_H
#define PILIB_H

#include "platform.h"
#if defined(__WINDOWS__)
	#define PI_MODE	0
#elif defined(__UNIX__)
	//#define _GNU_SOURCE
	#include <dlfcn.h>
	//#define PI_MODE     (RTLD_LAZY|RTLD_LOCAL)	// Resolve symbols now, keep them local
	//#define PI_MODE   (RTLD_LAZY|RTLD_GLOBAL)	// Resolve symbols when needed, make global
	//#define PI_MODE   (RTLD_NOW|RTLD_GLOBAL)	// Resolve symbols now, make global
    //#define OI_MODE   (RTLD_NOW|RTLD_GLOBAL|RTLD_DEEPBIND)
    #define PI_MODE   	(RTLD_NOW|RTLD_LOCAL|RTLD_DEEPBIND)
#else
	#error Unknown platform
#endif

// C interface
void *piLoadLib(const char *pcLibName, int iMode=-1);
void *piGetSymbol(void* hLib, const char *name);
bool piUnload(void *hLib);


// C++ wrapper
#ifdef __cplusplus

class DynLib{
public:
	DynLib( const char *pcLibName, int iMode=-1 );
	~DynLib( ){ if( m_hLib ) piUnload(m_hLib); delete [] m_name; }
	
	void *GetSymbol( const char *name ){ return piGetSymbol(m_hLib,name); }
	
	// Return handle to library
	operator void* ();
	const char *GetName(){ return m_name; }
	const char *GetLastErr();
	
	void* GetBase( const char *known_symbol );
	
protected:
	void* m_hLib;	
	const char *m_err;
	char *m_name;
	void* m_base;	// Base address of library in mem
};

#endif // __cplusplus

#endif // PILIB_H


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions