Results 1 to 11 of 11

Thread: [RESOLVED] [2.0] Issues setting up a Plugin system

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved [RESOLVED] [2.0] Issues setting up a Plugin system

    I've gone through many plugin examples as well as the one in the VB.Net codebank and I just cannot seem to get it working correctly.

    1. After I create an interface, let's call it IPlugin, and I create a DLL that uses that interface, what's the easyest and/or best way to test DLLs to see if it contains that interface?

    2. How do I call a function of a DLL that supports the IPlugin interface I created (let's say it only has 1 method, GetMyName() or something like that)? Remember, this is all handled dynamically (no adding references via the IDE).

    If someone can point me to a good tutorial I will happily go through it but if someone could post some sample code to do 1 and 2 that would be extremely helpful!
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Issues setting up a Plugin system

    You could use reflection to get the types that your library contains. You can then create an instance and use reflection again to get the interfaces implemented by that object. If it implements the IPlugin interface then you can cast it as an IPlugin instance and then access its members. There may well be a better way than that but I've never tried to do this so that's off the top of my head.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0] Issues setting up a Plugin system

    Hmm... I'll see what I can dig up on the MSDN and try to work through this myself some more. If anyone has any code examples I'd greatly appreciate it.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [2.0] Issues setting up a Plugin system

    Here's an example from one of my projects, a chat client.

    Code:
    // the IHost interface provides events that the 
    // plugin can subscribe to to interact with the main program,
    // as well as a function which returns the main 'ChatBot' object.
    
    public interface IPlugin
    {
    	bool Initialize(IHost Host);
    	bool Shutdown();
    
    	void ShowAbout();
    
    	string Author();
    	string Name();
    	string Version();
    	string Info();
    }
    And in the main program,
    Code:
    static void LoadPlugins(Settings settings)
    {
    	// settings.Plugins is a string[]
    
    	foreach (string sPlugin in settings.Plugins)
    	{
    		try
    		{
    			Assembly a = Assembly.LoadFile(System.IO.Path.Combine(Program.Path(), sPlugin));
    			Type[] types = a.GetTypes();
    			foreach (Type t in types)
    			{
    				if (t.GetInterface("IPlugin", true) != null)
    				{
    					IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
    					if (plugin.Initialize(host))
    						plugins.Add(plugin);
    
    				}
    			}
    
    		}
    		catch
    		{
    			// host.WriteLine("Failed to load plugin: " + sPlugin)
    		}
    	}
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0] Issues setting up a Plugin system

    Thanks a lot! I am having an issue though with this part:
    VB Code:
    1. IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
    My DLL is going to consist of a Custom UserControl that also inherits the IPlugin interface (so it's declared like the following)
    VB Code:
    1. public class MyCom : UserControl, IPlugin
    2. {
    3.  
    4. }

    So the base of this plugin will be both a UserControl and an IPlugin. How can I cast this correctly so I can access it? Or do you have any other recommendations?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Issues setting up a Plugin system

    IPlugin is not the base for your class. .NET does not allow multiple inheritance. UserControl is the immediate base class and your class implements the IPlugin interface. They are not the same thing. You can cast an variable to any type that it inherits either directly or indirectly, or to any interface that it implements. That means that if you have a variable of type MyCom then you can cast it as UserControl, IPlugin, Object, or any other type between Object and UserControl, including Control, Component, etc. Whatever type you cast it as is the type whose members you can access. If you cast as IPlugin then you cannot access members of the UserControl class, only the IPlugin interface.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0] Issues setting up a Plugin system

    But it throws an exception stating "Invalid Cast Exception from MyCom to IPlugin"

    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Issues setting up a Plugin system

    Are you sure that it is the same IPlugin interface that the application and the library are referring to? You haven't declared two interfaces with the same name, one in the application and one in the library, have you? As I say, I've never done this myself but I would assume that the IPlugin interface would have to be declared in a library spearate to both the application and the plugin, so that both could reference it and be referring to the same type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved Re: [2.0] Issues setting up a Plugin system

    Quote Originally Posted by jmcilhinney
    Are you sure that it is the same IPlugin interface that the application and the library are referring to? You haven't declared two interfaces with the same name, one in the application and one in the library, have you? As I say, I've never done this myself but I would assume that the IPlugin interface would have to be declared in a library spearate to both the application and the plugin, so that both could reference it and be referring to the same type.
    Ah, thanks! Works perfectly now!

    I didn't know I couldn't declare the two interfaces in seperate libraries. I assume since they were in the same namespace and had the same names it would be nice. Whoops
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  10. #10

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [RESOLVED] [2.0] Issues setting up a Plugin system

    I do have 1 more question about this topic. When I use the same technique as sunburnt posted. I need to constantly check data on my plugins (sometimes it may be a lot in a few seconds, sometimes it may be none for an hour). If I create a List<IPlugin> and load all of the interfaces into the list, how expensive will that be? Will it include the whole UserControl? Or should I use a different method (like an array of strings that have the Plugin's path and just seem reloading it (though this may not fit with my design))?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Issues setting up a Plugin system

    A collection merely contains references to objects. If the objects already exist then creating a collection to store references to them is very little overhead at all. It's just basically an instance of a class that has an internal array, the elements of which are references to your IPlugin objects. No additional IPlugin objects are created.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width