-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.go
More file actions
72 lines (61 loc) · 1.75 KB
/
startup.go
File metadata and controls
72 lines (61 loc) · 1.75 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
// getStartupShortcutPath returns the path to the startup shortcut.
func getStartupShortcutPath() string {
return filepath.Join(startupDir, "AutoExitNode.lnk")
}
// isStartupEnabled checks if the startup shortcut exists.
func isStartupEnabled() bool {
_, err := os.Stat(getStartupShortcutPath())
return err == nil
}
// addStartupShortcut creates a Windows shortcut for autostart.
func addStartupShortcut() {
if err := ole.CoInitialize(0); err != nil {
fmt.Println("CoInitialize error:", err)
return
}
defer ole.CoUninitialize()
exePath, err := os.Executable()
if err != nil {
fmt.Println("Error getting executable path:", err)
return
}
shellObj, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
fmt.Println("CreateObject error:", err)
return
}
defer shellObj.Release()
wshell, err := shellObj.QueryInterface(ole.IID_IDispatch)
if err != nil {
fmt.Println("QueryInterface error:", err)
return
}
defer wshell.Release()
shortcutPath := getStartupShortcutPath()
sc, err := oleutil.CallMethod(wshell, "CreateShortcut", shortcutPath)
if err != nil {
fmt.Println("CreateShortcut error:", err)
return
}
defer sc.Clear()
shortcut := sc.ToIDispatch()
_, _ = oleutil.PutProperty(shortcut, "TargetPath", exePath)
_, _ = oleutil.PutProperty(shortcut, "WorkingDirectory", filepath.Dir(exePath))
_, _ = oleutil.PutProperty(shortcut, "WindowStyle", 7)
_, _ = oleutil.CallMethod(shortcut, "Save")
}
// removeStartupShortcut deletes the autostart shortcut.
func removeStartupShortcut() {
path := getStartupShortcutPath()
if err := os.Remove(path); err != nil {
fmt.Println("Failed to remove startup shortcut:", err)
}
}