-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_windows.go
105 lines (94 loc) · 2.39 KB
/
main_windows.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package shortcut
import (
"path/filepath"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
// _WShell is the OLE Object for "WScript.Shell"
type _WShell struct {
agent *ole.IUnknown
dispatch *ole.IDispatch
}
// _NewWShell creates OLE Object for "WScript.Shell".
func _NewWShell() (*_WShell, error) {
agent, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
return nil, err
}
dispatch, err := agent.QueryInterface(ole.IID_IDispatch)
if err != nil {
agent.Release()
return nil, err
}
return &_WShell{agent: agent, dispatch: dispatch}, nil
}
// Close releases the OLE Object for "WScript.Shell".
func (wsh *_WShell) Close() {
wsh.dispatch.Release()
wsh.agent.Release()
}
// Read reads the data of shortcut file. `path` must be absolute path.
func (wsh *_WShell) Read(path string) (target string, workingdir string, err error) {
shortcut, err := oleutil.CallMethod(wsh.dispatch, "CreateShortCut", path)
if err != nil {
return "", "", err
}
shortcutDis := shortcut.ToIDispatch()
defer shortcutDis.Release()
targetPath, err := oleutil.GetProperty(shortcutDis, "TargetPath")
if err != nil {
return "", "", err
}
workingDir, err := oleutil.GetProperty(shortcutDis, "WorkingDirectory")
if err != nil {
return "", "", err
}
return targetPath.ToString(), workingDir.ToString(), err
}
func _read(path string) (targetPath string, workingDir string, err error) {
path, err = filepath.Abs(path)
if err != nil {
return "", "", err
}
wsh, err := _NewWShell()
if err != nil {
return "", "", err
}
defer wsh.Close()
return wsh.Read(path)
}
// Make makes a shortcut file.`from`,`to` must be absolute path.
func (wsh *_WShell) Make(from, to, dir string) error {
shortcut, err := oleutil.CallMethod(wsh.dispatch, "CreateShortCut", to)
if err != nil {
return err
}
shortcutDis := shortcut.ToIDispatch()
defer shortcutDis.Release()
_, err = oleutil.PutProperty(shortcutDis, "TargetPath", from)
if err != nil {
return err
}
_, err = oleutil.PutProperty(shortcutDis, "WorkingDirectory", dir)
if err != nil {
return err
}
_, err = oleutil.CallMethod(shortcutDis, "Save")
return err
}
func _make(from, to, dir string) error {
from, err := filepath.Abs(from)
if err != nil {
return err
}
to, err = filepath.Abs(to)
if err != nil {
return err
}
wsh, err := _NewWShell()
if err != nil {
return err
}
defer wsh.Close()
return wsh.Make(from, to, dir)
}