by C.J. (13 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sat 13th February 1999
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
How to avoid loading a second instance of an application when the user already has one instance running. It also sets the focus to the first
'Paste the following code into the code Module for Form1:
Option Explicit
Private Sub Form_Load()
If App.PrevInstance Then
ActivatePrevInstance
End If
End Sub
'2) Add a Standard Module to the Project.
'3) Paste the following code into the module:
Option Explicit
Public Const GW_HWNDPREV = 3
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Sub ActivatePrevInstance()
Dim OldTitle As String
Dim PrevHndl As Long
Dim result As Long
'Save the title of the application.
OldTitle = App.Title
'Rename the title of this application so FindWindow
'will not find this application instance.
App.Title = "unwanted instance"
'Attempt to get window handle using VB4 class name.
PrevHndl = FindWindow("ThunderRTMain", OldTitle)
'Check for no success.
If PrevHndl = 0 Then
'Attempt to get window handle using VB5 class name.
PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
End If
'Check if found
If PrevHndl = 0 Then
'Attempt to get window handle using VB6 class name
PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
End If
'Check if found
If PrevHndl = 0 Then
'No previous instance found.
Exit Sub
End If
'Get handle to previous window.
PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)
'Restore the program.
result = OpenIcon(PrevHndl)
'Activate the application.
result = SetForegroundWindow(PrevHndl)
'End the application.
End
End Sub
No comments have been posted about How to avoid loading a second instance of an application when the user already has one instance run. Why not be the first to post a comment about How to avoid loading a second instance of an application when the user already has one instance run.