Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Avoiding Multiple instances of .exe

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
Subo123
Asked:
Subo123
1 Solution
 
sdm395Commented:
You can check the App.PrevInstance property - if it is true then end the application

0
 
cmgarnettCommented:
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
 
ArkCommented:
Hi
Take a look at my code at
http://www.freevbcode.com/ShowCode.Asp?ID=2201
Cheers
0
 
DanRollinsCommented:
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
 
Computer101Commented:
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now