-
Hello All
I've got a real interesting question. I'm creating a program which is to be very robust. I would like to at a later time include plugins for it which will further extend its functionality. I have found the nice and easy use of activeX exe's but found that i must include the reference in the calling program. This means i'd have to release a new version of the main program with the release of each and every plugin. This is just unexceptable.
Can any one help?
-
You can try this. I use it on occasion when I think I might be adding new stuff to my program later.
Code:
Option Explicit
Private Type GUIDs
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Const ACTIVEOBJECT_STRONG = 0
Private Const ACTIVEOBJECT_WEAK = 1
Private Declare Function CLSIDFromProgID Lib "ole32.dll" (ByVal ProgID As Long, rclsid As GUIDs) As Long
Private Declare Function CoDisconnectObject Lib "ole32.dll" (ByVal pUnk As IUnknown, pvReserved As Long) As Long
Private Declare Function RegisterActiveObject Lib "oleaut32.dll" (ByVal pUnk As IUnknown, rclsid As GUIDs, ByVal dwFlags As Long, pdwRegister As Long) As Long
Private Declare Function RevokeActiveObject Lib "oleaut32.dll" (ByVal dwRegister As Long, ByVal pvReserved As Long) As Long
Dim OLEInstance As Long
'************************************************************************************
'Declare the Public Objects, Subs, Functions, and Events related to this application
'************************************************************************************
'*********************************************
'Private Variables for use with the Properties
'*********************************************
Private ini_MarcText As String
Private ini_visible As Boolean
Private Sub Class_Initialize()
Dim mGUID As GUIDs
Dim lp As Long
OLEInstance = 0
'Change the Your app to a public class in you
'active x EXE
lp = CLSIDFromProgID(StrPtr("YourApp.Application"), mGUID)
If lp = 0 Then
lp = RegisterActiveObject(Me, mGUID, ACTIVEOBJECT_WEAK, OLEInstance)
End If
End Sub
Public Sub Quit()
If OLEInstance <> 0 Then
RevokeActiveObject OLEInstance, 0
End If
CoDisconnectObject Me, 0
End Sub
Private Sub Class_Terminate()
RevokeActiveObject OLEInstance, 0
End Sub
In your program, when it first strats up, you would need to
initialize this class to make it active
Code:
Dim WithEvents myObj as Application
Private sub Form_Load()
'IsSet is a boolean in a module that
'lets me know if the object has been set
'But is just because I often find myself
'writing programs that have a dozen or so
'windows that need to be accessable.
If IsSet = False Then
Set myObj = New Application
'Again, a global variable to keep track of
'the object.
Set objMaster = myObj
IsSet = True
Else
Set myObj = objMaster
End If
Emd sub
Now the try part is that your program needs to be able to
figure out what the plugins are that will be access your app.
The way that I deal with that is to create a wizard, a have
users "register plugins" so that I can save the info into an
ini file, in the registry, or make the plugins accessible through
a menu option. To call the plugin then, all you need to do
is pass the object name and class to a function like:
Code:
'Probably should go in a module
Public sub StartPlugin(byval Obj_Name as string)
'Since you won't be referencing them, using events is out of
'the question. I generally adapt to this by having my program
'raise the events for them internally.
Dim genericObject as object
On Error REsume Next
Set genericObject=CreateObject(Obj_Name)
If Err<>0 then
msgbox "Active X control could not be initialized"
End if
'This is a function that I require all plugins to have.
Call genericObject.StartPlugin()
Set genericObject=Nothing
End sub
Now all you have to do is have your Plugin "talk" to you
program. This is done doing something like this:
Code:
'This is the plugin code for StartPlugin
'This should be an Active X dll or Exe
Public Sub StartPlugin()
Set obj_App as Object
Set obj_App=GetObject(,"YourApp.Application")
'Call whatever functions, forms or subs from here.
End sub
Hopefully this is helpful. Like I said, I know that it
isn't exactly like a plugin but I find that it works fine
enough. (As long as I am developing the plugins, or give
others clear enough documentation.)