Results 1 to 4 of 4

Thread: VB - Interprocess Communication

  1. #1

    Thread Starter
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    VB - Interprocess Communication

    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:
    VB Code:
    1. Option Explicit
    2.  
    3. ' Keep track of the number of instances of
    4. ' The AppConnector class that have been created
    5. ' So we know when to instanciate and destroy
    6. ' The single instance of the AppManager Class.
    7. Public plConnectionCount As Long
    8.  
    9. ' Keep a Public Reference to the AppManager Instance
    10. ' So that each instance of the AppConnector class
    11. ' Can get a reference to this instance, allowing apps
    12. ' To Share the instance and communicate through it.
    13. Public pobjSharedAppManager As AppManager
    Class - AppConnector:
    VB Code:
    1. ' Class Name:   AppConnector
    2. ' Instancing:   MuliUse
    3.  
    4. ' This Class is created by each instance of
    5. ' Our parent application and is used to
    6. ' get a reference to a shared "AppManager" class
    7. ' Which then allows all Applications to communicate
    8. ' Through it.
    9.  
    10. Option Explicit
    11.  
    12. Private Sub Class_Initialize()
    13.     ' Increment the Connection Count
    14.     plConnectionCount = plConnectionCount + 1
    15.     ' If this is the first connection,
    16.     ' Instanciate the AppManager Class and
    17.     ' Store a Public Reference to it for future
    18.     ' Use by other instances.
    19.     If plConnectionCount = 1 Then
    20.         Set pobjSharedAppManager = New AppManager
    21.     End If
    22. End Sub
    23.  
    24. Private Sub Class_Terminate()
    25.     ' Decrement the Connection Count
    26.     plConnectionCount = plConnectionCount - 1
    27.     ' If this is the last connection, destroy the
    28.     ' Public reference to the AppManager class
    29.     ' So that the ActiveX EXE can be released from
    30.     ' Memory.
    31.     If plConnectionCount = 0 Then
    32.         Set pobjSharedAppManager = Nothing
    33.     End If
    34. End Sub
    35.  
    36. Public Sub QueueFile(ByVal sFileList As String)
    37.     ' This Method is used by an application instance
    38.     ' To pass a file/list of files to another
    39.     ' Instance of the application.
    40.     pobjSharedAppManager.SendFile sFileList
    41. End Sub
    42.  
    43. Public Function GetAppManager() As AppManager
    44.     ' This Method returns a reference to the shared
    45.     ' AppManager Instance so the parent application can
    46.     ' Use it's event(s) within the application.
    47.     Set GetAppManager = pobjSharedAppManager
    48. End Function
    Class - AppManager:
    VB Code:
    1. ' Class Name:   AppManager
    2. ' Instancing:   PublicNotCreatable
    3.  
    4. ' This class is only ever instanciated once via the
    5. ' AppConnector class, because it's not creatable the
    6. ' Parent application can only get an instance via
    7. ' the AppConnector Class.
    8. Option Explicit
    9.  
    10. ' This event will be used to pass files
    11. ' between application instances
    12. Public Event QueueFile(ByVal sFileList As String)
    13.  
    14. Friend Sub SendFile(ByVal sFileList As String)
    15.     ' This Method is only accessable to the
    16.     ' AppConnector class and is used to
    17.     ' Raise the AppManagers "QueueFile()" Event
    18.     ' Passing the File/FileList as a parameter.
    19.     RaiseEvent QueueFile(sFileList)
    20. End Sub

    Compile into an EXE; "AppMgr.exe"

    Create a Standard Project; add a Reference to the "AppMgr.exe":
    VB Code:
    1. Option Explicit
    2.  
    3. ' Create an AppManager Connection Class Object
    4. Private mobjConn As New AppMgr.AppConnector
    5.  
    6. ' Create an AppManger Class Object "With Events"
    7. Private WithEvents MyAppMgr As AppMgr.AppManager
    8.  
    9. Private Sub Form_Load()
    10.     ' If there is a previous instance of this app
    11.     ' Already open, just pass the FileList on to
    12.     ' That Instance by calling the "QueueFile()" method
    13.     ' Of the AppConnector Class which will pass the filelist
    14.     ' To the existing instance via the AppManager's
    15.     ' QueueFile Event.
    16.     If App.PrevInstance Then
    17.         mobjConn.QueueFile Command
    18.         ' Exit this Instance, so only 1 instance is ever open.
    19.         End
    20.     Else
    21.         ' Otherwise, set our AppManager class object to
    22.         ' reference the Shared ActiveX EXE Object so we
    23.         ' Can Receive Events raised by other instances.
    24.         Set MyAppMgr = mobjConn.GetAppManager()
    25.         ' Then Call the Queue Method of the Connector to
    26.         ' Queue the passed File list - Optional, could just
    27.         ' Open these files or call the "QueueFiles" Event
    28.         ' Directly.
    29.         mobjConn.QueueFile Command
    30.     End If
    31. End Sub
    32.  
    33. Private Sub Form_Resize()
    34.     ' Resize the Listbox to fit the form.
    35.     ' Note: Listbox is set to "IntegralHeight = False"
    36.     On Error GoTo Resize_Error
    37.     lstFiles.Move 0, 0, ScaleWidth, ScaleHeight
    38.  
    39. Resize_Error:
    40. End Sub
    41.  
    42. Private Sub Form_Unload(Cancel As Integer)
    43.     ' When the Form/App Closes, Remove the AppMgr
    44.     ' AppConnector and AppManager References so the
    45.     ' Server can unload and be released from memory.
    46.     Set MyAppMgr = Nothing
    47.     Set mobjConn = Nothing
    48. End Sub
    49.  
    50. Private Sub MyAppMgr_QueueFile(ByVal sFileList As String)
    51.     ' This Event of the AppManager Class is raised
    52.     ' By "any" instance of the AppConnector class
    53.     ' Calling the "QueueFile()" Method.
    54.     Dim sFile As String
    55.     Dim lPos As Long
    56.    
    57.     ' Parse the Passed File/FileList and add each
    58.     ' File to the Listbox.
    59.     Do
    60.         If Left(sFileList, 1) = Chr(34) Then
    61.             lPos = InStr(2, sFileList, Chr(34)) + 1
    62.         Else
    63.             lPos = InStr(sFileList, " ")
    64.         End If
    65.         If lPos > 0 Then
    66.             sFile = Left(sFileList, lPos - 1)
    67.             sFileList = Mid(sFileList, lPos + 1)
    68.         Else
    69.             sFile = sFileList
    70.             sFileList = ""
    71.         End If
    72.         If Len(sFile) > 0 Then lstFiles.AddItem sFile
    73.     Loop While lPos > 0
    74. 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.
    Attached Files Attached Files

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269
    Aaron, this is fantastic code but is it possible to have the appmgr run and not appear as a running program in windows. It would be great to have it appear as a process only.

    Anyone?

    Thanks
    KT

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    since it is an activex exe..... it will show up in task manager.

    maybe use an activex dll instead? (i didnt look at the code long enough to know if it was possible to use a dll instead...)

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269
    I am not sure how this could be done, would be nice though as this would really finish this piece of code off to perfection.

    Anyone have any idea how to do it.

    I am still a noobbbb

    KT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width