|
-
Jul 8th, 2010, 11:36 PM
#1
Thread Starter
Lively Member
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
-
Jul 8th, 2010, 11:50 PM
#2
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.
-
Jul 8th, 2010, 11:58 PM
#3
Thread Starter
Lively Member
Re: Extremely basic MDI plugin system
 Originally Posted by jmcilhinney
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?
-
Jul 9th, 2010, 12:16 AM
#4
Re: Extremely basic MDI plugin system
The way I have explained is not difficult, so you don't need an easier way.
-
Jul 9th, 2010, 01:48 AM
#5
Re: Extremely basic MDI plugin system
 Originally Posted by Blupig
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|