VB icon

TWTCIPIAO - Three ways to check if program is already open

Email
Submitted on: 2/3/2015 8:18:00 AM
By: Filip Wielewski (from psc cd)  
Level: Beginner
User Rating: By 9 Users
Compatibility: VB 6.0
Views: 466
 
     This code shows 3 ways to check if program is already open. First way doesn't use any API (it uses App.PrevInstance property). Second way to find out if program is already open is to use FindWindow function. Third and the best way is to create mutex object.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :TWTCIPIAO - Three ways to check if program is already open
'**************************************
Option Explicit
'API function for 2nd way
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName _
As String, ByVal lpWindowName As String) As Long
'API functions for 3rd way
Public Declare Function CreateMutex Lib _
"kernel32" Alias "CreateMutexA" _
(lpMutexAttributes As Any, ByVal bInitialOwner _
As Long, ByVal lpName As String) As Long
Public Declare Function ReleaseMutex _
Lib "kernel32" (ByVal hMutex As Long) As Long
Public Declare Function CloseHandle _
Lib "kernel32" (ByVal hObject As Long) As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: TWTCIPIAO - Three ways to check if program is already open
' Description:This code shows 3 ways to check if program is already open. First way doesn't use any API (it uses App.PrevInstance property). Second way to find out if program is already open is to use FindWindow function. Third and the best way is to create mutex object.
' By: Filip Wielewski (from psc cd)
'**************************************

'3WTCIPIAO - 3 ways to check if program
'is already open
'By Filip Wielewski
'Sorry for my english
'==================1==================
'First way doesn't use any API.
'For example:
Option Explicit
Private Sub Form_Load()
 
 'If this program is already open
 'then end
 If App.PrevInstance = True Then End
 
End Sub
'App.PrevInstance may be True or
'False. If it is True, that means this
'program is already open.
'!!! - if there are already open the
'same programs but exe files' paths
'are different then App.PrevInstance
'retrieves False. Use App.PrevInstance
' only if paths of your programs
'are the same.
'==================2==================
'Second way to find out if program
'is already open is to use FindWindow
'function. For example:
Option Explicit
Private Sub Form_Initialize()
 
 'Find window that has the same
 'caption as Form1
 If FindWindow(vbNullString, _
 Form1.Caption) <> 0 Then End
 
End Sub
'When function retrieves 0 that means
'there is no window with caption like
'in Form1 (if window is found then
'function retrieves handle to it).
'But if there exists Explorer's
'window which caption is the same
'like Form1.Caption then function
'does also retrieve a handle to that
'window!
'To avoid that situation you have
'to know name of class of your Form
'(window). For example:
Option Explicit
Private Sub Form_Initialize()
 
 'Find window that has the same
 'caption like Form1
 If FindWindow("ThunderFormDC", _
 Form1.Caption) <> 0 Then End
 
End Sub
'!!! - Use FindWindow function in
'Form_Initialize(), not in Form_Load()
'because when you use FindWindow
'function in Form_Load() then program
'will find itself (in Form_Initialize()
'form isn't loaded yet so you can
'use FindWindow function to check if
'program is already open).
'==================3==================
'Third and the best way is to create
'mutex object. For example:
Option Explicit
Dim lonMutex As Long 'It will store a
 'handle to
 'mutex object.
 
Private Sub Form_Load()
 
 Const ERROR_ALREADY_EXISTS = 183&
 
 'Is this application already open?
 '(If it is open then end program)
 lonMutex = CreateMutex(ByVal 0&, _
 1, App.Title)
 If (Err.LastDllError = 183&) Then
'free memory
ReleaseMutex lonMutex
CloseHandle lonMutex
End
 End If
End Sub
Private Sub Form_Unload(Cancel As _
Integer)
 
 'free memory
 ReleaseMutex lonMutex
 CloseHandle lonMutex
 
End Sub
'CreateMutex function creates mutex
'object which represents our
'application in memory. If Err object
'returns error ERROR_ALREADY_EXISTS
'that means mutex object representing
'our application already exists.
'In this case free memory
'destroying mutex object and closing
'handle to it.


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.