Link to home
Start Free TrialLog in
Avatar of ravink
ravink

asked on

Previous Instance Checking

I want to check whether any of the previous instances of my EXE is running and if so I want the caption of active form of previous EXE. I tried by using App.Previnstance in InitProperties event, but I am unable to get a handle to the running exe.Based on some conditions I need to run another instance. So I need a handle to previous instance and caption of the active form of previous instance.
Avatar of marti
marti

When you change the caption of the form you may store it into a regestry so that the next instance can read it and take some actions.
You could also use EnumWindows API to get the captions of all current windows.
If this can help I'll submit additional information.
Regards
ravink:
Try the findWindow API: If you need more help, let me know
Regards
Dalin

In the General Declaration area:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim MyHandle as Long

myHandle = FindWindow(0&, theCaption)
If MyHandle = 0 Then
     
  ' App not Running

Else
' App is running

End If
Avatar of ravink

ASKER

The caption of the form changes in runtime, So I cant use FindWindow api call as it needs caption as a parameter. It helps in finding a particular window running or not. I want to find the window running by the same exe and its caption(Active form's). It looks Marti's solution helps in finding the captions of all windows, Can I get any additional information regarding this.
or any other solution. I appreciate your response.
ravink,

  The following code enumerate all top windows, compare windows caption to your application possible form caption and getting
the handle to previous instance.


----
' Declaration and functions under Module

Public hPrevInst as Long        ' Handle to previous instance
Public asFormCap() as String    ' Array of possible form cation
 
Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
 ByVal lParam As Long) As Long
 
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd
As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" _
        (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function OpenProcess Lib "kernel32" _
        (ByVal dwDesiredAccess As Long, _
         ByVal bInheritHandle As Long, _
         ByVal dwProcessId As Long) As Long


' EnumWinProc is a CALLBACK function which use by EnumWindows or
' EnumDesktopWindows
Function EnumWinProc(ByVal hWnd As Long, lParam As Long) As Long
    Dim sCaption As String
    Dim lRtn As Long
    Dim thID as Long
    Dim procID as Long
    Dim hProcess as Long
    
    sCaption = String(255, " ")
    
    lRtn = GetWindowText(hWnd, sCaption, 255)
    If lRtn > 0 Then
        sCaption = Left$(sCaption, lRtn)    ' Window has text
    Else
        EnumWinProc = True      ' Window with no text          
        Exit Function
    End If

    If ChkCaption(sCaption) = False Then
        EnumWinProc = True
        Exit Function
    End If
       
    thID = GetWindowThreadProcessId(hWnd, procID)

    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, procID)
    hPrevInst = hProcess   '***** This is the handle to previous instance **    
    EnumWinProc = True
End Function

Function ChkCaption(Byval sTmp As String) As String

    Dim iCnt As Integer

    ChkCaption = False
    For iCnt = LBound(asFormCap) To UBound(asFormCap)
        If sTmp = asFormCap(iCnt) Then
            ChkCaption = True
        End If
    Next  
End Function

' The main module
Sub Main()

    Dim lRtn As Long

    ' put your initialize code, such as put possible
    ' Form caption into array asFormCap
   
    ' Enumerate window to find previous instance
    hPrevInst = 0    
    lRtn = EnumWindows(AddressOf EnumWinProc, 0)
    If hPrevInst <> 0 then
       ' Found
       :
    End If
    :
    :   ' Continue
End Sub
Take a look at the currently open question "Determine if a particular app is being executed ?" - there are a few ideas there that might be of use (both NT and 95)
ravink,
Sorry did not realize your caption changes.
I think yowkee's solution may work. My great respect to yowkee for posting solutions even if the question is locked by someone else.  Please reject my answer and credit it to yowkee or marti who you think deserve them the most.
Regards to all of you.
Dalin
Avatar of ravink

ASKER

Yowkee's answer helped me to acheive my requirement. How will I credit these points to yowkee. Thanks for all of you for guiding me to the solution.
Thanks, Dalin. :)

ravink, You could reject the current proposed answer and let
me propose my answer to lock it.
ravink,
When open to this question, you should have a option to grade the answer, in which you should have five choices, choose the one say  "open to other expert" (or sothing like that). Add a commetns say "yowkee, please lock this"
Regards
Dalin
Avatar of ravink

ASKER

Thanks, Dalin
Yowkee, Please lock this.
ASKER CERTIFIED SOLUTION
Avatar of yowkee
yowkee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ravink
Sorry if my locking the question caused any delay for you to get the right answer. I realy appreciate yowkee provide the right answer as comments.
yowkee, you deserve the credit, I want to thank you for putting helping others first.
Regards
Dalin