Feb 1, 2012 10:10 AM
(This post was last modified: Jul 25, 2013 08:38 AM by Washi.)
Plug-in System in VB.NET
Hello all,
Today i'm going to show a way to create a basic plugin system with interfaces. I hope i'm clear enough and don't go to fast. I've split up the
tutorial into 6 steps.
Step 1 - Designing the Main Application
To start, you have to create first a new project that will represent your main application.
Spoiler (Click to View)
Then design the form something like this:
[img]http://imageshack.us/a/img560/6015/pic2ez.png[/img]
The first button is to load the user created plugin file, and the second to run it. I've created a groupbox with two labels in it to show you what the output can be. In this example we will change those label's texts by using the plug-in assembly.
Step 2 - The Connector Library
After the designing of your main application, save it and open a new visual studio window and create another project. This will be a class library that will connect the plugin with the main application.
Spoiler (Click to View)
Delete the contents of the standard class file and create two interfaces.
[img]http://imageshack.us/a/img685/1561/pic4o.png[/img]
What is an interface?
(skip this if you know what it is) Spoiler (Click to View)
The IPlugin and IConnector interfaces will be used in the main application and the plug-in assembly.
Step 3 - The Connector in the Main Application
After creating the connector library, save and build it and go back to your main application. Add your Plugin connector library reference to the project and create a new class, called Connector. Then add the following code to implement the IConnector interface.
If you press enter, the functions/subs Disconnect, GetLabel1Text, GetLabel2Text, SetLabel1Text and SetLabel2Text will be automatically added to your class. Before we add the code of the bodies of the functions and subs, we need to make another sub called Public Sub New(frm as Form1) and another variable. This is to specify the Form you want to edit the labels' texts. You'll get something like this:
Now we can write the code for the four label functions and subs. Just use Return frm.Label.Text for the get functions and frm.Label.Text = ... for the set subs. We'll write the disconnect sub later.
After this, your source will look something like this picture below.
[img]http://imageshack.us/a/img10/3696/pic8fm.png[/img]
Step 4 - Creating the Plug-In
Save the main application project and open another visual studio window, and create another class library. This will be the plug-in assembly itself.
Spoiler (Click to View)
First of all, add the plugin connector library reference to your project . Then add a form and design it to something like this:
[img]http://imageshack.us/a/img837/4160/pic11x.png[/img]
The first button will run the Get functions to get the labels' texts and put them into the textboxes. The Set button will run the Set Functions to set the labels' texts to the textboxes texts.
Back to your first class. Rename it to StartOfPlugin and implement the IPlugin interface. The sub StartPlugIn will be automatically added. In this sub we will create a new form1 and show it.
As you maybe see, the frm.Connector doesn't exist. That's why we have to add the following line at the top of the form's class.
Now double click onto the buttons and fill the subs with code. One to get and one to set. Then add a formclosing event to disconnect from the main application when the form closes.
After this, you have something like this:
[img]http://imageshack.us/a/img198/3456/pic12aj.png[/img]
Save the application and go back to the main application.
Step 5 -Finishing the source
At the very top of the main application form's source add the following line.
Then add a new variable inside the class. This will represents the current running plug-in.
Now double click onto button1 and use the following code to let the user open a file and set the CurrentPlugIn with the selected file.
Then double click onto button2 and add the following line to start the plugin.
Your form code will now look something like this:
[img]http://imageshack.us/a/img713/3707/pic13c.png[/img]
Go back to the Connector class and write the following line into the Disconnect sub to clean the CurrentPlugIn Variable.
Now you're done with the source.
Step 6 - Test!
If you did everything right, you'll get something like this gif animation:
![[Image: ir6MQ.gif]](https://i.imgur.com/ir6MQ.gif)
I hope you enjoyed this tutorial. Feel free to give comments. Please if you use this tutorial, say post a thanks 😉
If you want to download the project, click here
For the ones who don't trust the file. I dont really understand why there are 2 false positives since its a rar archive with sources.
VirusTotal scan
That's all 😉
Hello all,
Today i'm going to show a way to create a basic plugin system with interfaces. I hope i'm clear enough and don't go to fast. I've split up the
tutorial into 6 steps.
- Designing the Main Application
- The Connector Library
- The Connector in the Main Application
- Creating the Plug-in
- Finishing the source
- Test!
Step 1 - Designing the Main Application
To start, you have to create first a new project that will represent your main application.
Spoiler (Click to View)
Then design the form something like this:
[img]http://imageshack.us/a/img560/6015/pic2ez.png[/img]
The first button is to load the user created plugin file, and the second to run it. I've created a groupbox with two labels in it to show you what the output can be. In this example we will change those label's texts by using the plug-in assembly.
Step 2 - The Connector Library
After the designing of your main application, save it and open a new visual studio window and create another project. This will be a class library that will connect the plugin with the main application.
Spoiler (Click to View)
Delete the contents of the standard class file and create two interfaces.
[img]http://imageshack.us/a/img685/1561/pic4o.png[/img]
What is an interface?
(skip this if you know what it is) Spoiler (Click to View)
The IPlugin and IConnector interfaces will be used in the main application and the plug-in assembly.
- The IPlugin will be like the Entrypoint (Start) of the class. That's why this interface contains the sub StartPlugIn.
- The IConnector will be the connector that connects the Main App with the Plug-in. The first two functions will get the labels' texts, and the next two subs will set the labels' texts. The last sub will disconnect the plug-in from the main application.
Step 3 - The Connector in the Main Application
After creating the connector library, save and build it and go back to your main application. Add your Plugin connector library reference to the project and create a new class, called Connector. Then add the following code to implement the IConnector interface.
Code
Imports PluginConnector
Public Class Connector
Implements IConnectorIf you press enter, the functions/subs Disconnect, GetLabel1Text, GetLabel2Text, SetLabel1Text and SetLabel2Text will be automatically added to your class. Before we add the code of the bodies of the functions and subs, we need to make another sub called Public Sub New(frm as Form1) and another variable. This is to specify the Form you want to edit the labels' texts. You'll get something like this:
Code
Dim frm As Form1
Public Sub New(ByVal frm As Form1)
Me.frm = frm
End SubNow we can write the code for the four label functions and subs. Just use Return frm.Label.Text for the get functions and frm.Label.Text = ... for the set subs. We'll write the disconnect sub later.
After this, your source will look something like this picture below.
[img]http://imageshack.us/a/img10/3696/pic8fm.png[/img]
Step 4 - Creating the Plug-In
Save the main application project and open another visual studio window, and create another class library. This will be the plug-in assembly itself.
Spoiler (Click to View)
First of all, add the plugin connector library reference to your project . Then add a form and design it to something like this:
[img]http://imageshack.us/a/img837/4160/pic11x.png[/img]
The first button will run the Get functions to get the labels' texts and put them into the textboxes. The Set button will run the Set Functions to set the labels' texts to the textboxes texts.
Back to your first class. Rename it to StartOfPlugin and implement the IPlugin interface. The sub StartPlugIn will be automatically added. In this sub we will create a new form1 and show it.
Code
Dim frm As New Form1
frm.Connector = connector
frm.Show()As you maybe see, the frm.Connector doesn't exist. That's why we have to add the following line at the top of the form's class.
Code
Public Connector As IConnectorNow double click onto the buttons and fill the subs with code. One to get and one to set. Then add a formclosing event to disconnect from the main application when the form closes.
After this, you have something like this:
[img]http://imageshack.us/a/img198/3456/pic12aj.png[/img]
Save the application and go back to the main application.
Step 5 -Finishing the source
At the very top of the main application form's source add the following line.
Code
Imports System.ReflectionThen add a new variable inside the class. This will represents the current running plug-in.
Code
Public CurrentPlugIn As IPluginNow double click onto button1 and use the following code to let the user open a file and set the CurrentPlugIn with the selected file.
Code
Dim ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
'load the assembly
Dim pluginassembly As Assembly = Assembly.LoadFile(ofd.FileName)
'get the StartOfPlugin class by using GetType("<typename>")
Dim pluginclass As IPlugin = CType(Activator.CreateInstance(pluginassembly.GetType(pluginassembly.GetName.Name & ".StartOfPlugin")), IPlugin)
'sets the loaded plugin to the current plugin
CurrentPlugIn = pluginclass
End IfThen double click onto button2 and add the following line to start the plugin.
Code
CurrentPlugIn.StartPlugIn(Connector)Your form code will now look something like this:
[img]http://imageshack.us/a/img713/3707/pic13c.png[/img]
Go back to the Connector class and write the following line into the Disconnect sub to clean the CurrentPlugIn Variable.
Code
frm.CurrentPlugIn = NothingNow you're done with the source.
Step 6 - Test!
If you did everything right, you'll get something like this gif animation:
![[Image: ir6MQ.gif]](https://i.imgur.com/ir6MQ.gif)
I hope you enjoyed this tutorial. Feel free to give comments. Please if you use this tutorial, say post a thanks 😉
If you want to download the project, click here
For the ones who don't trust the file. I dont really understand why there are 2 false positives since its a rar archive with sources.
VirusTotal scan
That's all 😉
https://github.com/Washi1337
"I'll tell you this UDP joke, and I don't care if you will get it or not."
"I'll tell you this UDP joke, and I don't care if you will get it or not."



