Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" ( _
ByRef lpMutexAttributes As Any, _
ByVal bInitialOwner As Long, _
ByVal lpName As String) As Long
Private Declare Function ReleaseMutex Lib "kernel32" ( _
ByVal hMutex As Long) As Long
Private Declare Function OpenMutex Lib "kernel32" Alias "OpenMutexA" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal lpName As String) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Const ERROR_ALREADY_EXISTS = 183&
Private Const MUTEX_ALL_ACCESS = &H1F0001
Private lngMutex As Long
Sub Auto_Open()
lngMutex = OpenMutex(MUTEX_ALL_ACCESS, 0, "Test Mutex")
If lngMutex = 0 Then
lngMutex = CreateMutex(ByVal 0, 0, "Test Mutex")
Else
Call CloseHandle(lngMutex)
Call MsgBox("The App is running already")
End If
End Sub
Sub Auto_Close()
Call ReleaseMutex(lngMutex)
End Sub