Click here to Skip to main content
15,868,049 members
Articles / Desktop Programming / MFC

Multi platform plug-in development made easy!

Rate me:
Please Sign up or sign in to vote.
4.04/5 (25 votes)
7 Mar 20065 min read 100.3K   1.9K   63  
How to use and develop plug-ins for multiple platforms.
/*!

\section terms_of_use Terms of Use

libspl - the simple plugin layer library.
Copyright (C) 2004 Andreas Loeffler and Ren� Stuhr (www.unitedbytes.de).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

\section author Author
Andreas Loeffler (www.unitedbytes.de)

*/

#include "spl.h"

int main(int argc, char** argv)
{
	slcPluginArgs   pluginArgs;
	slcPluginInfo   pluginInfo;
	slcPluginServer pluginServer;

	long lRetCode = 0;

	printf( "Loading plugins ...\n" );

#if SPL_PLATFORM == SPL_PLATFORM_WIN32

	lRetCode = pluginServer.LoadAllPlugins( "..//plugins//", "*.dll" );

#elif SPL_PLATFORM == SPL_PLATFORM_LINUX

	lRetCode = pluginServer.LoadAllPlugins( "..//plugins//", "*.so" );

#endif

	if( 0 == lRetCode )
	{
		printf( "ERROR: No plugins found, aborting!\n" );
		return -1;
	}

	pluginServer.GetAllPluginInfos();

	long lPluginNumber = 0;
	if( argc > 1 )
	{
		printf( "\nUsing user defined plugin number to run (index 0)...\n" );
		lPluginNumber = atoi( argv[ 1 ] );
		if( lPluginNumber + 1 > pluginServer.GetLoadedPlugins() )
			lPluginNumber = 0;
	}
	pluginInfo = pluginServer.GetSafedPluginInfo( lPluginNumber );

	printf( "%ld plugins found!\n", lRetCode );
	printf( "Using plugin \"%s\" for demonstration purposes!\n\n", pluginInfo.GetName().c_str() );

	long lInitArgs = pluginInfo.GetArgCount( slcPluginInfo::sleFunction_Init );
	long lRunArgs = pluginInfo.GetArgCount( slcPluginInfo::sleFunction_Run );
	long lShutdownArgs = pluginInfo.GetArgCount( slcPluginInfo::sleFunction_Shutdown );
	
	if( lInitArgs > 0 )
	{
		printf( "This plugin has %ld arguments for the initialisation function!\n", lInitArgs );

		for( long l = 0; l < lInitArgs; l++ )
		{
			printf( "(%ld) Name: %s\n", l + 1, pluginInfo.GetArgName( slcPluginInfo::sleFunction_Init, l ).c_str() );
			printf( "(%ld) Desc: %s\n\n", l + 1, pluginInfo.GetArgDescription( slcPluginInfo::sleFunction_Init, l ).c_str() );
		}
	}

	if( lRunArgs > 0 )
	{
		printf( "This plugin has %ld arguments for the run function!\n", lRunArgs );

		for( long l = 0; l < lRunArgs; l++ )
		{
			printf( "(%ld) Name: %s\n", l + 1, pluginInfo.GetArgName( slcPluginInfo::sleFunction_Run, l ).c_str() );
			printf( "(%ld) Desc: %s\n\n", l + 1, pluginInfo.GetArgDescription( slcPluginInfo::sleFunction_Run, l ).c_str() );
		}
	}

	if( lShutdownArgs > 0 )
	{
		printf( "This plugin has %ld arguments for the shutdown function!\n", lShutdownArgs );

		for( long l = 0; l < lShutdownArgs; l++ )
		{
			printf( "(%ld) Name: %s\n", l + 1, pluginInfo.GetArgName( slcPluginInfo::sleFunction_Shutdown, l ).c_str() );
			printf( "(%ld) Desc: %s\n\n", l + 1, pluginInfo.GetArgDescription( slcPluginInfo::sleFunction_Shutdown, l ).c_str() );
		}
	}
	
	char* szString = "United Bytes";
	char  szResult[ 32 ] = { 0 };

	printf( "String before running plugin %ld (index 0): %s\n", lPluginNumber, szString );

	pluginArgs.SetArg( szString );
	pluginArgs.SetArg( szResult ); 
	pluginServer.RunPlugin( lPluginNumber, &pluginArgs );

	printf( "String AFTER running plugin %ld (index 0): %s\n\n", lPluginNumber, szResult );

	printf( "Now unloading all loaded plugins...\n" );
	pluginServer.UnloadAllPlugins();

	printf( "Test successfully finished.\n" );
	return 0;
}

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
Web Developer
Germany Germany
I was born in 1982 near Stuttgart / Germany and began my first steps in programming computers at the age of only nine years on an old Commodore CBM 7072. In 2002 I finished my education as IT specialist for software engineering and did my civillian service afterwards. Currently I'm working as leader of the software division in a bigger company located in south west Germany, mainly on software development and research projects for multimedia terminals and user recognition/verification systems.

Comments and Discussions