-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.5 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//go:build windows
// +build windows
package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"unicode/utf16"
"unsafe"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
procMessageBoxW = user32.NewProc("MessageBoxW")
mbOk = uintptr(0)
mbIconError = uintptr(0x10)
messageBoxFlags = mbOk | mbIconError
messageBoxTitle = "Mihomo Party Runner"
paramFileName = "param.txt"
)
func main() {
if err := run(); err != nil {
showError("Error: " + err.Error())
}
}
func run() error {
args := os.Args
if len(args) != 2 {
return errors.New("invalid arguments")
}
exePath, err := os.Executable()
if err != nil {
return err
}
exeDir := filepath.Dir(exePath)
paramPath := filepath.Join(exeDir, paramFileName)
contentBytes, err := os.ReadFile(paramPath)
if err != nil {
contentBytes = []byte{}
}
content := strings.TrimSpace(string(contentBytes))
cmd := exec.Command(args[1], content)
err = cmd.Start()
if err != nil {
errorMessage := "Failed to start program\n" + args[1] + "\n" + err.Error() + "\n请尝试以管理员权限启动软件"
return errors.New(errorMessage)
}
return nil
}
func UnicodeString(s string) *uint16 {
encoded := utf16.Encode([]rune(s + "\x00"))
return &encoded[0]
}
func showError(message string) {
modaless, _ := syscall.UTF16PtrFromString(messageBoxTitle)
msg, _ := syscall.UTF16PtrFromString(message)
procMessageBoxW.Call(0,
uintptr(unsafe.Pointer(msg)),
uintptr(unsafe.Pointer(modaless)),
messageBoxFlags)
}