The following code demonstrates how to use an ActiveX EXE to enable your application to communicate with other instances of itself,
allowing you to implement features like that of Enqueue in WinAmp.
In an ActiveX EXE Project called "AppMgr", add 2 Class Modules; "AppConnector" and "AppManager" and a Standard Module:
Standard Module:Class - AppConnector:VB Code:
Option Explicit ' Keep track of the number of instances of ' The AppConnector class that have been created ' So we know when to instanciate and destroy ' The single instance of the AppManager Class. Public plConnectionCount As Long ' Keep a Public Reference to the AppManager Instance ' So that each instance of the AppConnector class ' Can get a reference to this instance, allowing apps ' To Share the instance and communicate through it. Public pobjSharedAppManager As AppManagerClass - AppManager:VB Code:
' Class Name: AppConnector ' Instancing: MuliUse ' This Class is created by each instance of ' Our parent application and is used to ' get a reference to a shared "AppManager" class ' Which then allows all Applications to communicate ' Through it. Option Explicit Private Sub Class_Initialize() ' Increment the Connection Count plConnectionCount = plConnectionCount + 1 ' If this is the first connection, ' Instanciate the AppManager Class and ' Store a Public Reference to it for future ' Use by other instances. If plConnectionCount = 1 Then Set pobjSharedAppManager = New AppManager End If End Sub Private Sub Class_Terminate() ' Decrement the Connection Count plConnectionCount = plConnectionCount - 1 ' If this is the last connection, destroy the ' Public reference to the AppManager class ' So that the ActiveX EXE can be released from ' Memory. If plConnectionCount = 0 Then Set pobjSharedAppManager = Nothing End If End Sub Public Sub QueueFile(ByVal sFileList As String) ' This Method is used by an application instance ' To pass a file/list of files to another ' Instance of the application. pobjSharedAppManager.SendFile sFileList End Sub Public Function GetAppManager() As AppManager ' This Method returns a reference to the shared ' AppManager Instance so the parent application can ' Use it's event(s) within the application. Set GetAppManager = pobjSharedAppManager End FunctionVB Code:
' Class Name: AppManager ' Instancing: PublicNotCreatable ' This class is only ever instanciated once via the ' AppConnector class, because it's not creatable the ' Parent application can only get an instance via ' the AppConnector Class. Option Explicit ' This event will be used to pass files ' between application instances Public Event QueueFile(ByVal sFileList As String) Friend Sub SendFile(ByVal sFileList As String) ' This Method is only accessable to the ' AppConnector class and is used to ' Raise the AppManagers "QueueFile()" Event ' Passing the File/FileList as a parameter. RaiseEvent QueueFile(sFileList) End Sub
Compile into an EXE; "AppMgr.exe"
Create a Standard Project; add a Reference to the "AppMgr.exe":VB Code:
Option Explicit ' Create an AppManager Connection Class Object Private mobjConn As New AppMgr.AppConnector ' Create an AppManger Class Object "With Events" Private WithEvents MyAppMgr As AppMgr.AppManager Private Sub Form_Load() ' If there is a previous instance of this app ' Already open, just pass the FileList on to ' That Instance by calling the "QueueFile()" method ' Of the AppConnector Class which will pass the filelist ' To the existing instance via the AppManager's ' QueueFile Event. If App.PrevInstance Then mobjConn.QueueFile Command ' Exit this Instance, so only 1 instance is ever open. End Else ' Otherwise, set our AppManager class object to ' reference the Shared ActiveX EXE Object so we ' Can Receive Events raised by other instances. Set MyAppMgr = mobjConn.GetAppManager() ' Then Call the Queue Method of the Connector to ' Queue the passed File list - Optional, could just ' Open these files or call the "QueueFiles" Event ' Directly. mobjConn.QueueFile Command End If End Sub Private Sub Form_Resize() ' Resize the Listbox to fit the form. ' Note: Listbox is set to "IntegralHeight = False" On Error GoTo Resize_Error lstFiles.Move 0, 0, ScaleWidth, ScaleHeight Resize_Error: End Sub Private Sub Form_Unload(Cancel As Integer) ' When the Form/App Closes, Remove the AppMgr ' AppConnector and AppManager References so the ' Server can unload and be released from memory. Set MyAppMgr = Nothing Set mobjConn = Nothing End Sub Private Sub MyAppMgr_QueueFile(ByVal sFileList As String) ' This Event of the AppManager Class is raised ' By "any" instance of the AppConnector class ' Calling the "QueueFile()" Method. Dim sFile As String Dim lPos As Long ' Parse the Passed File/FileList and add each ' File to the Listbox. Do If Left(sFileList, 1) = Chr(34) Then lPos = InStr(2, sFileList, Chr(34)) + 1 Else lPos = InStr(sFileList, " ") End If If lPos > 0 Then sFile = Left(sFileList, lPos - 1) sFileList = Mid(sFileList, lPos + 1) Else sFile = sFileList sFileList = "" End If If Len(sFile) > 0 Then lstFiles.AddItem sFile Loop While lPos > 0 End Sub
Example Scenario:
Compile this EXE, then drag some files onto it, they'll appear in the listbox,
then drag some files onto the EXE again, this time the first instance of the EXE will just add the files to it's existing list.
I've attached the complete source with demonstration project.