
Avoiding multiple executables from running
Quote:
>I know how to prevent the same executable from being started twice on
> the same machine.
> But, is there an easy way to prevent another VB executable from
> running. I have two versions of the same program and they lock up if
> both are running because they try to use the same data acquisiton
> device.
Create a mutex. Mutexes are system-wide, so each app can check for the same mutex created by the other app. Here's an example:
-------BEGIN CODE
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const ERROR_ALREADY_EXISTS As Long = 183
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal
bInitialOwner As Long, ByVal lpName As String) As Long
Private Sub Main()
Dim sa As SECURITY_ATTRIBUTES
'Prevent running this program again if it's already running
If Not IsIDE Then 'InIDE Then 'only process in compiled exe
sa.bInheritHandle = 1
sa.lpSecurityDescriptor = 0
sa.nLength = Len(sa)
Call CreateMutex(sa, 1, "MutexName")
If Err.LastDllError = ERROR_ALREADY_EXISTS Then
MsgBox App.Title & " is already running.", vbInformation
Exit Sub
End If
End If
<remaining code for Sub Main
End Sub
Public Function IsIDE() As Boolean
Dim hVB432 As Long
Dim hVB416 As Long
Dim hVB5 As Long
Dim hVB6 As Long
hVB432 = GetModuleHandle("VB32.EXE")
hVB416 = GetModuleHandle("VB.EXE")
hVB5 = GetModuleHandle("VB5.EXE")
hVB6 = GetModuleHandle("VB6.EXE")
IsIDE = (hVB432 > 0 Or hVB416 > 0 Or hVB5 > 0 Or hVB6 > 0)
End Function
-----END CODE
You can use any name you want for the mutex. Simply check for the same name in both apps and take appropriate action if the mutex
exists.
--
Mike
Microsoft Visual Basic MVP