Solved

Previous Instance Checking

Posted on 1998-06-04
12
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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