Library code snippets
Prevent multiple VB application instances
Often you'll find it beneficial to allow just one instance of a VB
application to run on each PC. To do so, you can use several standard Win32
api calls before loading an application, such as in a main module. Obtain
the application window's handle from the startup form's caption with the
FindWindow() API call. If this function returns a non-zero value, then the
specified window is open. Below, is some example code that uses this
function to prevent the main module from starting.Option Explicit
Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName _
As String) As Long
Private Declare Function IsIconic Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Const SW_RESTORE = 9
'Conatants used for the return of the MultiInst function
Private Const OPEN_APPLICATION = 0
Private Const SINGLE_INSTANCE_OPEN = 1
Sub Main()
Dim MultiInstResult As Integer
'Call procedure to determine if an instance of
'the application is already loaded
MultiInstResult = MultiInst
'Handle the result from the MultiInst function
If MultiInstResult = OPEN_APPLICATION Then
Form1.Show
'No instance of the application is already open,
'continue to load the login form
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
'An instance already exists cancel the
'current application load
End
End If
End Sub
Private Function MultiInst() As Integer
'This function determines if a single instance of the
'application is already running.
Dim hwndFound As Long 'The window handle
Dim strWindowName 'The Caption on the window
'Set the caption of the application form
strWindowName = "Form's Caption Here"
'Get the handle of the application if it is open
hwndFound = FindWindow(vbNullString, strWindowName)
If hwndFound Then
'Set the function return
MultiInst = SINGLE_INSTANCE_OPEN
MsgBox "A instance of the application is already
open." & _
vbCrLf & vbCrLf & _
"Only one open instance
allowed.", vbOKOnly + _
vbExclamation, "App Name"
'If application minimized, restore, show it on top
If IsIconic(hwndFound) Then
ShowWindow hwndFound,
SW_RESTORE
'Show the window infront
of all other windows
SetForegroundWindow hwndFound
Else
'Bring the application
top most on the screen
SetForegroundWindow hwndFound
End If
ElseIf hwndFound = 0 Then
'Set the function return so it will continue loading
MultiInst = OPEN_APPLICATION
End If
End Function
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
hmmm, that is true, the advantage of the method shown here is the fact that FindWindow will give you the hWnd value for the other instance meaning that through subclassing you can send a message to the previous instance - very useful when the user opens a file and there is already an instance running.
Put this line in the subroutine where you program starts.
'If application is already running then get out of here
If App.PrevInstance = True Then End
This thread is for discussions of Prevent multiple VB application instances.