-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.go
52 lines (45 loc) · 1.5 KB
/
singleton.go
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
package main
import (
"fmt"
"os"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var (
// Import the MessageBox function from user32.dll
user32 = windows.NewLazySystemDLL("user32.dll")
procMessageBox = user32.NewProc("MessageBoxW")
// Function to show a MessageBox
// hWnd is the owner window, lpText is the message text, lpCaption is the window title, uType is the type of message box.
MessageBox = func(hWnd uintptr, lpText, lpCaption string, uType uint) int {
ret, _, _ := procMessageBox.Call(
hWnd,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpText))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpCaption))),
uintptr(uType),
)
return int(ret)
}
)
func InstanceMutex() func() {
// Attempt to create a named mutex
mutexName := syscall.StringToUTF16Ptr("Global\\MuteinyAppMutex")
mutex, err := windows.CreateMutex(nil, false, mutexName)
if err != nil {
fmt.Println("Error creating mutex:", err)
fmt.Println("Another instance of Muteiny is already running.")
MessageBox(0, "Another instance of Muteiny is already running.", "Error: Muteiny", 0)
os.Exit(1)
}
// If GetLastError returns ERROR_ALREADY_EXISTS, another instance is running
if windows.GetLastError() == windows.ERROR_ALREADY_EXISTS {
fmt.Println("Another instance of Muteiny is already running.")
MessageBox(0, "Another instance of Muteiny is already running.", "Muteiny", 0)
os.Exit(1)
}
return func() {
fmt.Println("Closing 'Global\\MuteinyAppMutex' mutex")
windows.CloseHandle(mutex)
}
}