C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-01-2005, 05:51 AM   #1
mustang benny
 
bennyandthejets's Avatar
 
Join Date: Jul 2002
Posts: 1,401
Loading a .dll

I want to implement a simple plugin system for my program. Each plugin will have a couple interface functions, ie for initialization and execution, which can have defined signatures, using primitive types. These functions would have identical signatures in every .dll.

How can I load multiple .dlls, but still be able to call the specific function I need?
__________________
benforbes@optusnet.com.au
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro

Code Tags
Programming FAQ
Tutorials
bennyandthejets is offline   Reply With Quote
Old 06-01-2005, 10:48 PM   #2
Banned
 
nickname_changed's Avatar
 
Join Date: Feb 2003
Location: Australia
Posts: 986
OK, this is how I do it:

I check all files in a directory for .DLL or .EXE's. Then, I try to load each DLL. Then, I look at every Type in each DLL to see what it is. If it implements IPlugin (an interface I created that all my plugins or extensions follow), I load the object.

Code:
// here you could loop through a directory and try every file...
Assembly[] assemblies = new Assembly[1];
try
{
	assemblies[0] = Assembly.LoadFrom(@"C:\firstPlugin.dll");
}
catch { }
try
{
	assemblies[1] = Assembly.LoadFrom(@"C:\secondPlugin.dll");
}
catch { }

// search each loaded assembly for the type
foreach (Assembly assembly in assemblies)
{
	foreach (Type type in assembly.GetTypes())
	{
		if (type.IsClass)
		{
			// try to create an intance of it - maybe not the 
			// safest method but works
			try
			{
			    object o = Activator.CreateInstance(type);

			    // IPlugin is a custom interface we made
				if (o is IPlugin)
				{
				    IPlugin plugin = (IPlugin)o;
				    plugin.Execute();
				}
			}
			catch
			{
				
			}
		}
	}
}
nickname_changed is offline   Reply With Quote
Old 06-01-2005, 11:28 PM   #3
mustang benny
 
bennyandthejets's Avatar
 
Join Date: Jul 2002
Posts: 1,401
Thanks, that looks perfect.
__________________
benforbes@optusnet.com.au
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro

Code Tags
Programming FAQ
Tutorials
bennyandthejets is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cargo loading system kyle rull C Programming 1 04-20-2009 12:16 PM
loading .h, .dll, and .lib sigh Windows Programming 4 02-08-2008 01:32 AM
added start menu crashes game avgprogamerjoe Game Programming 6 08-29-2007 01:30 PM
Problem with a .dll file GaPe Windows Programming 2 10-29-2003 01:20 PM
Loading Screen pinkcheese Windows Programming 2 04-06-2002 11:48 PM


All times are GMT -6. The time now is 12:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22