forked from capnspacehook/taskmaster
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomhelper.go
More file actions
126 lines (115 loc) · 2.97 KB
/
Copy pathcomhelper.go
File metadata and controls
126 lines (115 loc) · 2.97 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//go:build windows
// +build windows
package taskmaster
import (
"fmt"
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
// oleHelper performs a batch of COM property reads/writes against Task Scheduler
// objects, recording the first error encountered. Once an error is recorded,
// every subsequent operation becomes a no-op returning the zero value, so a long
// sequence of calls can be issued and the accumulated error checked once at a
// convenient point via h.err.
//
// This replaces the panic-on-error oleutil.Must* helpers with ordinary error propagation
//
// IMPORTANT: getObject and query return nil once an error has been recorded.
// Callers MUST check h.err immediately after such a call, before dereferencing or
// deferring Release on the result, e.g.:
//
// obj := h.getObject(parent, "Child")
// if h.err != nil {
// return h.err
// }
// defer obj.Release()
type oleHelper struct {
err error
}
func (h *oleHelper) getString(obj *ole.IDispatch, name string) string {
if h.err != nil {
return ""
}
v, err := oleutil.GetProperty(obj, name)
if err != nil {
h.err = fmt.Errorf("error getting property %q: %w", name, err)
return ""
}
defer v.Clear()
return v.ToString()
}
func (h *oleHelper) getInt(obj *ole.IDispatch, name string) int64 {
if h.err != nil {
return 0
}
v, err := oleutil.GetProperty(obj, name)
if err != nil {
h.err = fmt.Errorf("error getting property %q: %w", name, err)
return 0
}
defer v.Clear()
return v.Val
}
func (h *oleHelper) getBool(obj *ole.IDispatch, name string) bool {
if h.err != nil {
return false
}
v, err := oleutil.GetProperty(obj, name)
if err != nil {
h.err = fmt.Errorf("error getting property %q: %w", name, err)
return false
}
defer v.Clear()
b, ok := v.Value().(bool)
if !ok {
h.err = fmt.Errorf("property %q is not a boolean", name)
return false
}
return b
}
func (h *oleHelper) getVariant(obj *ole.IDispatch, name string) *ole.VARIANT {
if h.err != nil {
return nil
}
v, err := oleutil.GetProperty(obj, name)
if err != nil {
h.err = fmt.Errorf("error getting property %q: %w", name, err)
return nil
}
return v
}
func (h *oleHelper) getObject(obj *ole.IDispatch, name string) *ole.IDispatch {
if h.err != nil {
return nil
}
v, err := oleutil.GetProperty(obj, name)
if err != nil {
h.err = fmt.Errorf("error getting property %q: %w", name, err)
return nil
}
d := v.ToIDispatch()
if d == nil {
h.err = fmt.Errorf("property %q is not an object", name)
return nil
}
return d
}
func (h *oleHelper) put(obj *ole.IDispatch, name string, args ...any) {
if h.err != nil {
return
}
if _, err := oleutil.PutProperty(obj, name, args...); err != nil {
h.err = fmt.Errorf("error setting property %q: %w", name, err)
}
}
func (h *oleHelper) query(obj *ole.IDispatch, iid *ole.GUID) *ole.IDispatch {
if h.err != nil {
return nil
}
d, err := obj.QueryInterface(iid)
if err != nil {
h.err = fmt.Errorf("error querying COM interface: %w", err)
return nil
}
return d
}