![]() |
|
[.NET] [TUTORIAL] Make your application support Plugins (FULL SOURCE) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] [TUTORIAL] Make your application support Plugins (FULL SOURCE) (/showthread.php?tid=2169571) |
[TUTORIAL] Make your application support Plugins (FULL SOURCE) - Washi - Feb 1, 2012 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. Code Imports PluginConnectorIf 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 Form1Now 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 Form1As 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 OpenFileDialogThen 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: ![]() 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 π * - Ethernal Five - Feb 1, 2012 Looks like a very good tutorial, very detailed and explaned alot. Thanks ;) * - MoBaT - Feb 1, 2012 This is such a well built tutorial. I will be sure to use this. * - ibloodqman - Feb 1, 2012 Greatest tutorial i ever seen (honestly). Big Vouch, i'd rep you but i can't.. keep it up * - Androcir - Feb 1, 2012 Very nice tutorial mate, you made it very compreensive (if that's how it is spelled... I don't know..) Anyways.. ;) * - Washi - Feb 1, 2012 I'm glad you all liked it π Thanks for the commentary. * - nuk - Feb 1, 2012 Very nice indeed. Great share man! Def. a keeper on the tutorials :) * - M0D3M - Feb 1, 2012 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. * - nrd - Feb 1, 2012 this was a great tutorial, can come in handy for some programs in the future * - The MammoTh - Feb 1, 2012 This one is better then I coded, do you mind if I use it? * - ParkII - Feb 1, 2012 Yeah...I needed this a long time ago ... * - Sixem - Feb 1, 2012 That's pretty damn cool, detailed and easy to understand, great job. * - Washi - Feb 2, 2012 (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)Shaqways Wrote: That's pretty damn cool, detailed and easy to understand, great job. I'm glad you two liked it π * - log dot zip - Nov 12, 2012 Yip yippity kiyay! (Didn't get the reference? Oh well) Anyways: AWESOME JOB. * - KΕtei - Nov 12, 2012 Needs more publicity, truly a grand guide. |