-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodMutex.bas
86 lines (68 loc) · 2.8 KB
/
modMutex.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Attribute VB_Name = "modMutex"
Option Explicit
Private Declare Function CreateMutex Lib "Kernel32.dll" Alias "CreateMutexA" (ByVal lpMutexAttributes As Long, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function ReleaseMutex Lib "Kernel32.dll" (ByVal hMutex As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "Kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Function OpenMutex(Name As String, Optional ByRef StatusOnly As Boolean = False) As Long
Dim handle As Long
Dim lngError
' create mutex object
Err.Clear
handle = CreateMutex(0&, 1, Name)
lngError = Err.LastDllError
'AddLog "lngError: " & lngError, True
Err.Clear
If (lngError = 183&) Then
OpenMutex = 0
'AddLog "name: " & Name & " - handle: " & handle & " openmutex: " & 0, True
Else
OpenMutex = handle
'AddLog "name: " & Name & " - handle: " & handle & " openmutex: " & handle, True
End If
If StatusOnly Then CloseMutex handle
End Function
Public Sub CloseMutex(ByVal handle As Long)
'AddLog "CloseMutex: " & CStr(handle), True
If handle = 0 Then Exit Sub
' release all
Dim t As Integer
For t = 1 To 100
If ReleaseMutex(handle) = 0 Then Exit For
Next t
CloseHandle handle
handle = 0
End Sub
Public Sub CloseAllMutex()
'this only closes the mutex **if the calling process owns it!**
CloseMutex MutexSystemHandle
CloseMutex MutexAdminHandle
CloseMutex MutexUserHandle
End Sub
Public Function IsSysMutexHeld() As Boolean
If OpenMutex(MutexBaseName + "-SysMutex", True) = 0 Then IsSysMutexHeld = True
End Function
Public Function IsAdminMutexHeld() As Boolean
If OpenMutex(MutexBaseName + "-AdminMutex", True) = 0 Then IsAdminMutexHeld = True
End Function
Public Function IsUserMutexHeld() As Boolean
If OpenMutex(MutexBaseName + "-" & getCurrentUser(False), True) = 0 Then IsUserMutexHeld = True
End Function
Public Function GetUserMutex() As Long
AddLog "attempting to reserve User Mutex..", True
MutexUserHandle = OpenMutex(MutexBaseName + "-" & getCurrentUser(False), False)
GetUserMutex = MutexUserHandle
AddLog "GetUserMutex: " & MutexUserHandle, True
End Function
Public Function GetAdminMutex() As Long
AddLog "attempting to reserve Admin Mutex..", True
MutexAdminHandle = OpenMutex(MutexBaseName + "-AdminMutex", False)
GetAdminMutex = MutexAdminHandle
AddLog "GetAdminMutex: " & MutexAdminHandle, True
End Function
Public Function GetSysMutex() As Long
AddLog "attempting to reserve Sys Mutex..", True
MutexSystemHandle = OpenMutex(MutexBaseName + "-SysMutex", False)
GetSysMutex = MutexSystemHandle
AddLog "GetSysMutex: " & MutexSystemHandle, True
End Function