Library tutorials & articles
Writing Plugin-Based Applications
- Introduction
- The Interfaces & First Plugin
- The Host Application
- Wrapping Up
The Interfaces & First Plugin
Writing the Interfaces
First we create a new project of type Class Library, and define the two interfaces. For the purposes of this tutorial they will be very simple. Each plugin will offer a property with the name of the plugin, and a function which accepts two integers and returns a double. The host interface will expose a method so that the plugin can cause the host to show a message box.
Public Interface IPlugin
Sub Initialize(ByVal Host As IHost)
ReadOnly Property Name() As String
Function Calculate(ByVal int1 As Integer, ByVal int2 As Integer) As Double
End Interface
Public Interface IHost
Sub ShowFeedback(ByVal strFeedback As String)
End Interface
Lastly we set the build output directory to a common directory, where we'll also put the host application and the plugins.
Writing the first plugin
Since we'll need at least one to test the host application with, now we can write a simple plugin. Again, we create a new project of type Class Library, set the build output directory, and make sure we reference the interface class library we just created. Next, we modify the class provided thus:
Public Class Class1
Implements PluginSample.Interfaces.IPlugin
Private objHost As PluginSample.Interfaces.IHost
Public Sub Initialize(ByVal Host As PluginSample.Interfaces.IHost) _
Implements PluginSample.Interfaces.IPlugin.Initialize
objHost = Host
End Sub
Public ReadOnly Property Name() As String Implements _
PluginSample.Interfaces.IPlugin.Name
Get
Return "Example Plugin 1 - Adds two numbers"
End Get
End Property
Public Function Calculate(ByVal int1 As Integer, ByVal int2 As Integer) As Double _
Implements PluginSample.Interfaces.IPlugin.Calculate
Return int1 + int2
End Function
End Class
We've just created a simple plugin, which adds the two numbers provided together. Although we are accepting and storing a reference to the host interface, we don't actually use it. We'll do that in the next plugin we write.
Related articles
Related discussion
-
How to write the category attribut in a class dynamically
by converter2009 (1 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
VB.Net Button Problem
by pysdex (0 replies)
-
Unable to access AxInterop.AcoPdflib.dll on 64 bit OS
by Shaila14041981 (0 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
hi
when i run the host i found this error : Warning 1 Could not resolve this reference. Could not locate the assembly "Interfaces". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. Plugin 1
"PluginSample.Interfaces.IPlugin" is not defined
can u help me to splve this problem
thanks
!--removed tag-->Just add event handlers to the menu items before returning from InitializeMenu, e.g.
[quote]menu1.Click += new EventHandler (nameOfCallbackFunction);[/quote]
I found a very easy way to add controls to the host.
Dim Menu As New ToolStrip
Dim menu1 As New ToolStripMenuItem("Plugin")
menu1.DropDown = CreateCheckImageContextMenuStrip("Calculate")
Menu.Items.Add(menu1)
Menu.Dock = DockStyle.Top
Return Menu
End Function
Me.ToolStripContainer1.TopToolStripPanel.Controls.Add(objPlugin.InitializeMenu)
The only thing i did not figure out yet is how to handle when you click on the control.
I rewrote your application in C# and everthing works except one thing.
After loading assembly and creating plugin instance I am casting plugin instance to plugin interface and I get excepion "Specified cast is not valid"
I have no idea what is wrong.
I needed the plug in functionality to swap and update plug-ins on a regular basis, for an ASP.Net application. However, it seems I must shut down the site completely to replace an existing plug-in with a new version...
Great article!
This will come in very handy with an application I have in mind
Just a note about using this in VB2005 (I'm using Express edition right now, hopefully I'll upgrade):
The CreateInstance method of the PluginServices works fine, and does return an instance of the class that can be used but only when put into an Object type.
You don't have the full list of functions and subroutines available to you using the object.
When using the DirectCast, to cast the instance to an variable declared as the Interface - it doesn't like that cast and throws an exception.
To fix this do what Prozac mentiond - change the references of [Assembly].LoadFrom() to [Assembly].LoadFile() and it works fine.
Thanks for the great article!
I think your article is great, i'm looking for it for months, and finally...
I just have a question:
If I want that a plugin add a button ont the host form, is it possible? can you help me please?
thanks in advance,
Ivan
The complete code is available in the source code, however I'll post it for you anyway.
Public Structure AvailablePlugin
Public AssemblyPath As String
Public ClassName As String
End Structure
Enjoy!
I noticed that when I used [Assembly].LoadFrom(), it kept on loading the first plugin that was initially loaded,
to fix this; use [Assembly].LoadFile() insted.
DotNet Framework 1.1, Visual Studio 2003, Visual Basic.NET.
I can't get AvailablePlugin to work. VB.NET reports that
"Type 'AvailablePlugin' is not defined." Has anyone else had this problem?
Hi.
I've been trying to find the way of doing an application with pluggable components over vb6, but have not found any help. I read the subject of this article and was very excited but when I realized it was about .net I got frustrated. Could you please explain me how it is possible to make a plugin-based app in vb6.
Thanx
malloc
This thread is for discussions of Writing Plugin-Based Applications.