How can I have a program check if it is already open (easy), then send information to it (harder)?
Printable View
How can I have a program check if it is already open (easy), then send information to it (harder)?
if app.previnstance = true then
' after that i'm lost ;)
end if
i think he knew that much... technically you could do a sendmessage, send the custom message, then you could make a custom messagehandler in the program... and look for that certain message and use it...
put if you use FindWindow api, you would be sending the info to the same program.
hm... good point... i wish i knew how to do it though (would help w/ the mp3 player that im making...)
BUMP
You could use an ActiveX EXE (Out of Process Server) to allow multiple instances of your application to share a single class instance, thereby giving them a way to communicate with each other, i.e.
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
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 for this example to.
looks perfect. Thank you
Thanks you again. Works exactly as I hoped. Does this take up a lot of extra ram?
Nope, there's hardly anything to it, I think there's more comments than code. :)
are you the original author of this?
for the credits.....
Yes, I just knocked it out.
I had done a similar example (Which should be in this Forum somewhere) a while back called AppExtender which is pretty much identical.
Aaron, once again you are the man!
I have no current use for this code, but you'd better believe I'm thowing it in the library for future use!
Very sweet!