Skip to content

Commit

Permalink
Fix build error on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Apr 28, 2024
1 parent fece4fc commit 747a121
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Wox/util/single_instance/single_instance_windows.go
Expand Up @@ -5,9 +5,31 @@ package single_instance
import (
"os"
"syscall"
"unsafe"
"wox/util"
)

const (
LOCKFILE_EXCLUSIVE_LOCK = 2
)

var (
kernel32 = syscall.MustLoadDLL("kernel32.dll")
procLockFileEx = kernel32.MustFindProc("LockFileEx")
)

func LockFileEx(hFile syscall.Handle, dwFlags, dwReserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh uint32, lpOverlapped *syscall.Overlapped) (err error) {
r1, _, e1 := syscall.Syscall6(procLockFileEx.Addr(), 6, uintptr(hFile), uintptr(dwFlags), uintptr(dwReserved), uintptr(nNumberOfBytesToLockLow), uintptr(nNumberOfBytesToLockHigh), uintptr(unsafe.Pointer(lpOverlapped)))
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func lock(content string) error {
filename := util.GetLocation().GetAppLockFilePath()
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
Expand All @@ -17,7 +39,7 @@ func lock(content string) error {
defer file.Close()

var overlapped syscall.Overlapped
err = syscall.LockFileEx(syscall.Handle(file.Fd()), syscall.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped)
err = LockFileEx(syscall.Handle(file.Fd()), LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped)
if err != nil {
return err
}
Expand Down

0 comments on commit 747a121

Please sign in to comment.