Performing an event in another running instance of a VB6 application
Posted on 2003-03-27
I've ripped some code off the net to bail out of my program if it detects another running instance. It works fine, and even activates the other window before ending itself.
Here's what I need it to do, though. I need it to launch an event in the other instance after it detects the other program is running. I've got a "cmdReload" button on the "frmPayments" form, and I need it to either click the button somehow (it has the Default property set to true, if that makes it easier), or run the "cmdReload_click" procedure.
My guess is that there is a simple statement that will do it, I just don't have any experience with that sort of stuff. I started researching "SendMessage" stuff in the Windows API, but I'm not sure if that's what I need.
Here's the existing code: (most of this is straight off the net)
Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName _
As String) As Long
Private Declare Function IsIconic Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Const SW_RESTORE = 9
'Conatants used for the return of the MultiInst function
Private Const OPEN_APPLICATION = 0
Private Const SINGLE_INSTANCE_OPEN = 1
Dim MultiInstResult As Integer
'Call procedure to determine if an instance of
'the application is already loaded
MultiInstResult = MultiInst
'Handle the result from the MultiInst function
If MultiInstResult = OPEN_APPLICATION Then
'No instance of the application is already open,
'continue to load the login form
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
'An instance already exists cancel the
'current application load
Private Function MultiInst() As Integer
'This function determines if a single instance of the
'application is already running.
Dim hwndFound As Long 'The window handle
Dim strWindowName 'The Caption on the window
'Set the caption of the application form
strWindowName = "Insurance Payments" ' <<< This is the only line I actually had to modify
'Get the handle of the application if it is open
hwndFound = FindWindow(vbNullString, strWindowName)
If hwndFound Then
'Set the function return
MultiInst = SINGLE_INSTANCE_OPEN
'MsgBox "A instance of the application is already open." & _ '<<<OK, commented this out, too.
vbCrLf & vbCrLf & _
"Only one open instance allowed.", vbOKOnly + _
vbExclamation, "App Name"
'>>> My guess is the extra code will go right here<<<
'If application minimized, restore, show it on top
If IsIconic(hwndFound) Then
ShowWindow hwndFound, SW_RESTORE
'Show the window infront of all other windows
'Bring the application top most on the screen
'>>> or maybe here<<<
ElseIf hwndFound = 0 Then
'Set the function return so it will continue loading
MultiInst = OPEN_APPLICATION
THANKS for your help!