Results 1 to 5 of 5

Thread: Extremely basic MDI plugin system

  1. #1

    Thread Starter
    Lively Member Blupig's Avatar
    Join Date
    Apr 2008
    Posts
    118

    Extremely basic MDI plugin system

    Hi

    I've got an MDI form with a menustrip that already detects all .dll files in a folder and adds them to the menu to show the user which plugins are available. The thing is that I don't know what to do from here on out...

    I'd like to be able to update my program without having to stifle through all my code (it's a very big program) and be able to create basic new features through external DLLs. It's extremely useful for users to make their own features too

    Anyway, what a plugin would look like is most likely just a form with controls on it that the user makes themselves in VB. I'd like the plugin to activate through an init() sub within the plugin by clicking on the menu item shown in my MDI form.

    I kind of started with my own code but it didn't really get anywhere:
    Code:
            ListFilesInMenuStrip(PluginsToolStripMenuItem, App_Path() & "Plugins\")
            For Each c As Control In Me.Controls
                If TypeOf c Is MenuStrip Then
                    For Each child In c.Controls
                        If TypeOf child Is ToolStripMenuItem Then
                            Dim m As ToolStripMenuItem
                            m = child
    
                        End If
                    Next
                End If
            Next
    ...Where ListFilesInMenuStrip() is my function to add all the DLLs to the menu and App_Path is just a shortcut function which serves the same purpose as VB6's App.Path() class.

    I've also done research but only came across all their weird complex CodeDom and other weird things...This seems so simple but I just can't wrap my mind around it :I

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

    Re: Extremely basic MDI plugin system

    Search the web for plugin architecture .net or the like and you'll find various examples. There's likely some on this very site. Basically, you create a DLL that contains an interface and all plugins must implement, often named IPlugin. You will then need a reference to that DLL in your application project and you develop to that. Each plugin author will also need a reference to that DLL in there own project and they then implement your interface. At run time, you find the DLL and use Reflection to interrogate it to see if it contains a type that implements IPlugin. If it does you load it and interact with it via the IPlugin interface.

    The .NET Framework also has an add-in framework built in, in the System.AddIn namespace, but I have no experience with that.
    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
    Lively Member Blupig's Avatar
    Join Date
    Apr 2008
    Posts
    118

    Re: Extremely basic MDI plugin system

    Quote Originally Posted by jmcilhinney View Post
    Search the web for plugin architecture .net or the like and you'll find various examples. There's likely some on this very site. Basically, you create a DLL that contains an interface and all plugins must implement, often named IPlugin. You will then need a reference to that DLL in your application project and you develop to that. Each plugin author will also need a reference to that DLL in there own project and they then implement your interface. At run time, you find the DLL and use Reflection to interrogate it to see if it contains a type that implements IPlugin. If it does you load it and interact with it via the IPlugin interface.

    The .NET Framework also has an add-in framework built in, in the System.AddIn namespace, but I have no experience with that.
    Is there nothing anymore simple that can achieve the same thing? I was thinking simply importing the selected DLL and calling on a method common within each plugin which would be Init() or something similar...Possible?

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

    Re: Extremely basic MDI plugin system

    The way I have explained is not difficult, so you don't need an easier way.
    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

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Extremely basic MDI plugin system

    Quote Originally Posted by Blupig View Post
    Is there nothing anymore simple that can achieve the same thing? I was thinking simply importing the selected DLL and calling on a method common within each plugin which would be Init() or something similar...Possible?
    That's exactly what do, but you need an interface for that. If the plugins that other users write do not contain an Init method, how could you call it? The plugin interface could be as simple as:
    Code:
    Public Interface IPlugin
       Sub Initialize()
    End Interface
    Now, people that want to write a plugin implement your IPlugin interface and are therefore required to have an Initialize method. Now, you do know for certain that their classes will have the Initialize method, and you can call it by creating an instance of their class as an IPlugin, something like
    Code:
    Dim type As Type = 'Get the plugin type from the DLL using reflection
    Dim plugin As IPlugin = DirectCast(Activator.CreateInstance(type), IPlugin)
    plugin.Initialize()

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