Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[TUTORIAL] Make your application support Plugins (FULL SOURCE)

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.
  • 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 IConnector

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:

Code
Dim frm As Form1

    Public Sub New(ByVal frm As Form1)
  Me.frm = frm
    End Sub

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.

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 IConnector

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.

Code
Imports System.Reflection

Then add a new variable inside the class. This will represents the current running plug-in.

Code
Public CurrentPlugIn As IPlugin

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.

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 If

Then 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 = Nothing

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]




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."
Looks like a very good tutorial, very detailed and explaned alot. Thanks ;)
This is such a well built tutorial. I will be sure to use this.
Greatest tutorial i ever seen (honestly). Big Vouch, i'd rep you but i can't.. keep it up
Very nice tutorial mate, you made it very compreensive (if that's how it is spelled... I don't know..)

Anyways.. ;)
"There's no dead which can eternal lie, and with strange Aeons even death may die."


I'm glad you all liked it 😉
Thanks for the commentary.
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
Very nice indeed. Great share man! Def. a keeper on the tutorials :)
Excellent tutorial. This is the first tutorial I have seen on this subject. However I have not been on in awhile. One green apple for you.
this was a great tutorial, can come in handy for some programs in the future
They say money can't buy happiness, but i beg to differ.
This one is better then I coded, do you mind if I use it?
Yeah...I needed this a long time ago ...
That's pretty damn cool, detailed and easy to understand, great job.
(Feb 1, 2012 05:00 PM)The MammoTh Wrote:  This one is better then I coded, do you mind if I use it?

Of course you 're allowed, this is a tutorial, and you may use the source. 😉

(Feb 1, 2012 05:04 PM)ParkII Wrote:  Yeah...I needed this a long time ago ...

(Feb 1, 2012 08:57 PM)Sixem Wrote:  That's pretty damn cool, detailed and easy to understand, great job.

I'm glad you two liked it 😁
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
Yip yippity kiyay! (Didn't get the reference? Oh well) Anyways: AWESOME JOB.
Needs more publicity, truly a grand guide.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by ( ͡° ͜ʖ ͡°)Sep 9, 2019 11:22 PM 42 16,833 Sep 9, 2019 11:22 PM
Last Post: AnonFawkes
[Java] by TennoFeb 3, 2019 04:49 AM 4 307 Feb 3, 2019 04:49 AM
Last Post: Tenno
[Python] by SkidyMar 29, 2018 06:54 AM 3 164 Mar 29, 2018 06:54 AM
Last Post: .kr.