Solved

Avoiding Multiple instances of .exe

Posted on 2001-07-23
5
277 Views
Last Modified: 2006-11-17
I need to avoid running multiple instances of Standard EXE in same machine, even if user executed by mistake multiple times. If the exe is already active then there should not be a new instance of the exe created when the user clicks it. The exe is working as a background process and is not having any active form. The visibilty of the form used in the exe is set to false.
What is the best possible code that can be included in the exe to achieve the above.
.....
I used app.previnstance... but it shows as false....
0
Comment
Question by:Subo123
[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
5 Comments
 
LVL 1

Expert Comment

by:sdm395
ID: 6307832
You can check the App.PrevInstance property - if it is true then end the application

0
 
LVL 3

Accepted Solution

by:
cmgarnett earned 100 total points
ID: 6307842
Always a good question.

One way I have done this is to put this in my prject's startup routine:-

Private Sub Main()

   If App.PrevInstance Then
      pActivateApplication App.Title, True
   Else
      frmMain.Show
   End If

End Sub

And these declarations and subroutines in a separate utilities module:-

Public Const SW_SHOW As Long = 5&
Public Const SW_RESTORE As Long = 9&

Public Const GW_CHILD As Long = 5&
Public Const GW_HWNDNEXT As Long = 2&

Public Declare Function AttachThreadInput Lib "user32" (ByVal idAttach&, ByVal idAttachTo&, ByVal fAttach&) As Long
Public Declare Function BringWindowToTop Lib "user32" (ByVal hWnd&) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hWnd&, ByVal wCmd&) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd&, ByVal lpString$, ByVal cch&) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd&, lpdwProcessId&) As Long
Public Declare Function IsIconic Lib "user32" (ByVal hWnd&) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd&) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hWnd&, ByVal nCmdShow&) As Long


Sub pActivateApplication(ByVal strApplication As String, ByVal blnWholeName)
'====================================================================
'Procedure to activate a previous existence of an application.
'Will only work with VB6 applications
'Input:  strApplication Title of application to activate
'        blnWholeName   True if strApplication is the whole title
'                             of application to activate
'                       False if strApplication is the start of
'                             title of application to activate
'====================================================================
   Dim hAppWIndow&
   Dim strTemp As String
   
   If Not blnWholeName Then
      hAppWIndow = GetWindow(GetDesktopWindow(), GW_CHILD)
      Do
         strTemp = String$(180, False)
         Call GetWindowText(hAppWIndow, strTemp, 179)
         If InStr(strTemp, strApplication) Then
            pActivateInstance hAppWIndow
            Exit Do
         End If
         hAppWIndow = GetWindow(hAppWIndow, GW_HWNDNEXT)
      Loop Until hAppWIndow = 0
   Else
      hAppWIndow = FindWindow("ThunderRT6FormDC", strApplication)
      pActivateInstance hAppWIndow
   End If
   
End Sub

Private Sub pActivateInstance(ByVal hAppWIndow&)
'====================================================================
'Procedure used by pActivateApplication to activate an existing
'instanace of an application.
'Input:  hAppWindow& Handle of application to be activated
'====================================================================
   Dim fShowWindowFlag&, hWndForeground&
   Dim nCurThreadID&, nNextThreadID&
   
   If IsIconic(hAppWIndow) Then
      fShowWindowFlag = SW_RESTORE
   Else
      fShowWindowFlag = SW_SHOW
   End If
   
   Call ShowWindow(hAppWIndow, fShowWindowFlag)
   
   hWndForeground = GetForegroundWindow()
   
   If hAppWIndow <> hWndForeground Then
      nCurThreadID = GetWindowThreadProcessId(hWndForeground, ByVal 0&)
      nNextThreadID = GetWindowThreadProcessId(hAppWIndow, ByVal 0&)
     
      If nCurThreadID <> nNextThreadID Then
         Call AttachThreadInput(nCurThreadID, nNextThreadID, True)
         Call BringWindowToTop(hAppWIndow)
         Call SetForegroundWindow(hAppWIndow)
         Call AttachThreadInput(nCurThreadID, nNextThreadID, False)
      Else
         Call BringWindowToTop(hAppWIndow)
         Call SetForegroundWindow(hAppWIndow)
      End If
   End If

End Sub

Hope this is of help
0
 
LVL 28

Expert Comment

by:Ark
ID: 6307880
Hi
Take a look at my code at
http://www.freevbcode.com/ShowCode.Asp?ID=2201
Cheers
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7156358
Hi Subo123,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept cmgarnett's comment(s) as an answer.

Subo123, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7182236
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

710 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