go的windows单实例互斥锁
utils
package utils
import (
"golang.org/x/sys/windows"
"syscall"
)
func LockMutex(name string) error {
ptr, _ := syscall.UTF16PtrFromString(name)
_, err := windows.CreateMutex(nil, true, ptr)
if err != nil {
return err
}
return nil
}
main.go
import (
"context"
"embed"
"github.com/gin-gonic/gin"
"github.com/mzky/blink"
"io/fs"
"log"
"net/http"
"os"
"pcmClient/utils"
"time"
)
//go:embed res
var res embed.FS
var (
c utils.CorsVar
winForm *blink.WebView
)
func init() {
c.Port = ":7569"
// 是否支持F5和F12调试模式
// blink.SetDebugMode(false)
if err := blink.InitBlink(); err != nil {
log.Fatal(err)
}
winForm = blink.NewWebView(false, 1280, 800)
if err := utils.LockMutex("PCM_Client"); err != nil {
winForm.MessageBox("消息", "同时只能运行一个升级维护客户端!")
log.Fatalf("客户端已经运行", err)
}
}