Solved

Previous Instance Checking

Posted on 1998-06-04
12
305 Views
Last Modified: 2010-05-03
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.
0
Comment
Question by:ravink
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 2

Expert Comment

by:marti
ID: 1462594
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
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1462595
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
0
 

Author Comment

by:ravink
ID: 1462596
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.
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1462597
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
0
 

Expert Comment

by:tad
ID: 1462598
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)
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1462599
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
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:ravink
ID: 1462600
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.
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1462601
Thanks, Dalin. :)

ravink, You could reject the current proposed answer and let
me propose my answer to lock it.
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1462602
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
0
 

Author Comment

by:ravink
ID: 1462603
Thanks, Dalin
Yowkee, Please lock this.
0
 
LVL 4

Accepted Solution

by:
yowkee earned 100 total points
ID: 1462604
Thanks again, Dalin. :)

Answer as my comment.
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1462605
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now