Jump to content

Create an app that supports Plugins


Recommended Posts

How do you go about creating a program that supports the use of Plugins?

Im still creating my Win Tweak Compiler and I want to create a plugin for a main interface that I have created and hopefully I will be able to use it for meny others too

Please could you help :(

PS Its in VB.NET

Link to comment
Share on other sites


its a lot easier to do in C++... but the way i do it( which is probably the completly the wrong way to do it, but it works) is to create a common gateway framework for all your apps, such as this is the function that you will call to access the plugin and these are the variables... Anyway, what you would do(not sure if you can do it in vb) is create the DLL plugin, and then call the LoadLibraryEX function...

Check out the MSDN documentation on it, there is a great article, albeit old entitled 'DLL's the dynamic way', you should check it out, it is a C++ article, but its pretty self explanatory so long as you understand some C++... Also, my recomendation is to look at the source code of other apps that support plugins, SourceForge and OSDN host plenty projects like this...

ah, the url of that article mention above is: http://msdn.microsoft.com/library/default..../dlldynamic.asp

Link to comment
Share on other sites

Most intesting, yea thats just great, if only I could program in V++ or C# :blushing:

I found and intresting VB6 code that alows for plugins, but it will no bought take me quite a while getting my head around it

Can forms be included in the plugins?

I am looking into C++ as I have just bought £130 of books that hopefully will help me understand it a little better :wacko:

Will have to see when I release some othe source code for my main MDI Interface that I intend to use, then maybe I will be able to create DLL to access databases, files....Etc

both Client side and Server Side controls

Link to comment
Share on other sites

The trick (as I know it in VB6) is to begin the whole project with the creation of a dll that will be the host for your plugin stub -- which is a cls file, let's call this one "wtStub" -- and perhaps some generic string/form/number manipulation classes. Call the project something like "cjWinTwekStuff". Add some properties &/ methods...

   Option Explicit

   Public Property Get StubName() As String
   '
   End Property

   Public Function Add(ByVal PropKey As String, ByVal PropVal As String) As Long
   '
   End Function

   Public Sub Process()
   '
   End Sub

   Public Property Result() As String
   '
   End Property

Compile this into a dll, and, hey; remember to set it's compatibility to "Project" or you'll end up in DLL Hell when you come to the conclusion that you want to add something to your dll and recompile it and nothing matches...

Start a new ActiveX DLL project. Open the cls file. Type the following...

   Option Explicit

   Implements cjWinTwekStuff.wtStub

If you know look under the object drop down list box in the coding window, you'll notice the object "wtStub" there. Click it, and in the other drop down box, you'll find all the available properties and methods of "wtStub". NOTE: You can't, which you can when it comes to instances of regular controls, skip any property/method. You must implement them. But implementing them is as easy as clicking the name in the drop down menu and adding a ' instead of any code, like this:

   Public Property Get wtStub_StubName() As String
       '
   End Property

But on most occasions however you propably would like to do something inside the plugin so...

   Option Explicit

   Implements cjWinTwekStuff.wtStub

   Dim PropKeysVals As Collection

   Private Sub Class_Initialize()
       Set PropKeysVals = New Collection
   End Sub

   Private Sub Class_Terminate()
       Set PropKeysVals = Nothing
   End Sub

   Public Property Get wtStub_StubName() As String
       StubName = "Win Tweak Do Something"
   End Property

   Public Sub wtStub_Add(ByVal PropKey As String, ByVal PropVal As String)
       PropKeysVals.Add PropVal, PropKey
   End Sub

   Public Sub wtStub_Process()
       'Do something. I don't know what. If you want to you can open a window here (only vbModal I'm afraid, but still) or something...
   End Sub

   Public Property wtStub_Result() As String
       'Return the result of what you did in Process.
   End Property

Compile this into a dll. Call the class say "DoSomething" and the dll "wtPlgDoSomething". Once again, remember the "Project" compatibility. ;) Anyho.

Start a new EXE project. Double-click the form and in the top of the form enter...

   Private pPlugins() As cjWinTweakStuff.wtStub

... And in the Form_Load event type this...

   Private Sub Form_Load()
       Redim pPlugins(1)
 
       Set pPlugins(0) = CreateObject("wtPlgDoSomething.DoSomething")
       'Redim Preserve and add more plugins.
   End Sub

Ok. So here's the catch (there's just got to be one): Look at the name of your plugin. Look again. How does your application even know that there are plugins available when they have to be named things like "wtPlgDoSomething.DoSomething"? The quick answer is: It doesn't. So you have to find a way of letting your application know that there are plugins.

The strategy I use in one of my creations (which is on, I don't know, a delta stage when it comes to the GUI, and a beta on the rest, maybe) is to let my application scan a directory for files with a certain extension and then take the rest of the filename and try to call CreateObject on that part (in your case, maybe the extension could be something like ".wtplg", which would name your file "wtPlgDoSomething.DoSomething.wtplg"). On failure I log a message, and on success I read the properties of the plugin and store them in an internal plugin handler class in as a Type in an array of Types of my own, matching design and then I set the newly created object to nothing and proceed to the next. Why do I do this? Because of Error 47. "Too many DLL application clients". The VB Runtime can only handle 50 simultanious instances of applications &/ dll's and ocx's, therefore "you must not waste resources or you'll be wasted by thy neighbour". Or at least disliked.

To continue to the next problem: How does your plugins know where to install their wtplg files &/ themselves? The way I see it is that I avoid the registry as much as possible, because it is downright clumsy if you, as an end user, want to move an application from one place to another (for backup purposes for instance), and I want to make the use of applications that I write as slim and intuitive as possible where possible. Still it is absolutely great when it comes to letting two applications know the whereabouts of each other. So add your application to the registry and let the plugin installation program look for that particular key in the registry and then install to the correct subdirectory. If you let your application do this on startup, and correct the path if it is wrong, you'll always (well, not really, but see bellow for that) be able to distribute plugins that install to the correct location. And that's nice.

Welcome to the "always (well, not really" part. I can think of one occasion where it fails and that is if someone move you application from one directory to another and doesn't start it, but immediately start to install plugins. Then the registry entry will point to the old directory. That is solvable too, for instance by letting your plugin installation program detect that a certain directory is empty or doesn't exist and then let the user select the directory where your application actually resides.

Maybe that will get you started in the right direction...?

Best wishes

/Samuel

Link to comment
Share on other sites

what i usualy do is create a common interface and a few dls'(for testing purposes). the dlls start with a specific name like ex_ for export add-ons and im_ for import add-ons. then, when the program starts, i will search for the dll's in my program folder and subfolders and cal a common function in those dll's using LoadLibraryEx and other items. the common function is named (in my dll's atleast) Reg_Info to call the info that is needed by my program to work with the dll's. i also get a list of the exported functions, thru that function Reg_Info, in that dll and can work with that. if you want to see the plugin working, just download my RunOnceEx.cmd Creator or ask for the sources (Delphi 7)

Link to comment
Share on other sites

Im going to redo the app in C++ for the main reason, I want to learn it and also because it looks like it will be the best option to make the program with, in my eyes

Has anyone made a plugin that has its own forms in it and called them from events that also added by the plugin? say I click a menu item, would I be able to get it to bring up a form that resides in the DLL or not?? if so Please could you put example of how too

Cheers

Link to comment
Share on other sites

would it be possable to create a Class with the Form info in it and send it to the app via the plugin interface and it called a Sub in my main app to show a MDI

Sub NewMdi(FormClass as New form)
  FormClass.MDIParrent = frmMain
  FormClass.Show
End Sub

This isn't C++ Code I know this is just off the top of my head as I don't have Visual Studio .NET 2003 to hand, its more like VB.NET as I know that to exstent

Any ideas? Example code would be EXCLENT

Stupid PC keeps becoming useless very time I open up My Computer :realmad:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...