![]() |
| | #1 |
| mustang benny Join Date: Jul 2002
Posts: 1,401
| Loading a .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 | |
| | #2 |
| Banned 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 | |
| | #3 |
| mustang benny 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |