forked from StackExchange/wmi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnection.go
342 lines (299 loc) · 8.96 KB
/
connection.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// +build windows
package wmi
import (
"errors"
"fmt"
"reflect"
"sync"
"github.com/bi-zone/go-ole"
"github.com/bi-zone/go-ole/oleutil"
"github.com/hashicorp/go-multierror"
"github.com/scjalliance/comshim"
)
var (
// ErrConnectionClosed is returned for methods called on the closed
// SWbemServicesConnection.
ErrConnectionClosed = errors.New("SWbemServicesConnection has been closed")
)
// SWbemServicesConnection is used to access SWbemServices methods of the
// single server.
//
// Ref: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemservices
type SWbemServicesConnection struct {
sync.Mutex
Decoder
sWbemServices *ole.IDispatch
}
// ConnectSWbemServices creates SWbemServices connection to the server defined
// by @connectServerArgs. Actually it just creates `SWbemLocator` and invokes
// `SWbemServices ConnectServer` method. Args are passed to the method as it.
//
// Ref: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemlocator-connectserver
func ConnectSWbemServices(connectServerArgs ...interface{}) (conn *SWbemServicesConnection, err error) {
services, err := NewSWbemServices()
if err != nil {
return nil, err
}
defer func() {
if closeErr := services.Close(); closeErr != nil {
err = multierror.Append(err, closeErr)
}
}()
return services.ConnectServer(connectServerArgs...)
}
// ConnectSWbemServices creates SWbemServices connection to the server defined
// by @args.
//
// Ref: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemlocator-connectserver
func (s *SWbemServices) ConnectServer(args ...interface{}) (c *SWbemServicesConnection, err error) {
// Be aware of reflections and COM usage.
defer func() {
if r := recover(); r != nil {
err = multierror.Append(err, fmt.Errorf("runtime panic; %v", r))
}
}()
// Notify that we are going to use COM. We will care about at least one
// reference for connection.
comshim.Add(1)
defer func() {
if err != nil {
comshim.Done()
}
}()
serviceRaw, err := oleutil.CallMethod(s.sWbemLocator, "ConnectServer", args...)
if err != nil {
return nil, fmt.Errorf("SWbemServices ConnectServer error; %v", err)
}
service := serviceRaw.ToIDispatch()
if service == nil {
return nil, errors.New("SWbemServices IDispatch returned nil")
}
// Resulting IDispatch uses the same mem as a variant, and a variant will not clear anything
// (ref: https://docs.microsoft.com/en-us/windows/desktop/api/oleauto/nf-oleauto-variantclear)
// so we have no need to care about of serviceRaw and moreover call clear on it.
conn := &SWbemServicesConnection{
Decoder: s.Decoder,
sWbemServices: service,
}
conn.Decoder.Dereferencer = conn
return conn, nil
}
// Close will clear and release all of the SWbemServicesConnection resources.
func (s *SWbemServicesConnection) Close() error {
s.Lock()
defer s.Unlock()
if s.sWbemServices == nil {
return nil // Already stopped.
}
s.sWbemServices.Release()
s.sWbemServices = nil
comshim.Done()
return nil
}
// Query runs the WQL query using a SWbemServicesConnection instance and appends
// the values to dst.
//
// More info about result unmarshalling is available in `Decoder.Unmarshal` doc.
//
// Query is performed using `SWbemServices.ExecQuery` method.
//
// Ref: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemservices-execquery
func (s *SWbemServicesConnection) Query(query string, dst interface{}) error {
s.Lock()
if s.sWbemServices == nil {
s.Unlock()
return ErrConnectionClosed
}
s.Unlock()
sliceRefl := reflect.ValueOf(dst) // TODO: Double argument check?
if sliceRefl.Kind() != reflect.Ptr || sliceRefl.IsNil() {
return ErrInvalidEntityType
}
sliceRefl = sliceRefl.Elem() // "Dereference" pointer.
argType, elemType := checkMultiArg(sliceRefl)
if argType == multiArgTypeInvalid {
return ErrInvalidEntityType
}
return s.query(query, &queryDst{
dst: sliceRefl,
dsArgType: argType,
dstElemType: elemType,
})
}
// Get retrieves a single instance of a managed resource (or class definition)
// based on an object @path. The result is unmarshalled into @dst. @dst should
// be a pointer to the structure type.
//
// More info about result unmarshalling is available in `Decoder.Unmarshal` doc.
//
// Get method reference:
// https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemservices-get
func (s *SWbemServicesConnection) Get(path string, dst interface{}) (err error) {
s.Lock()
if s.sWbemServices == nil {
s.Unlock()
return ErrConnectionClosed
}
s.Unlock()
// Be aware of reflections and COM usage.
defer func() {
if r := recover(); r != nil {
err = multierror.Append(err, fmt.Errorf("runtime panic; %v", r))
}
}()
dstRef := reflect.ValueOf(dst)
if dstRef.Kind() != reflect.Ptr && dstRef.Elem().Kind() != reflect.Struct {
return fmt.Errorf("dst should be a pointer to struct")
}
resultRaw, err := s.dereference(path)
if err != nil {
return err
}
result := resultRaw.ToIDispatch()
defer func() {
if clErr := resultRaw.Clear(); clErr != nil {
err = multierror.Append(err, clErr)
}
}()
return s.Unmarshal(result, dst)
}
// Dereference performs `SWbemServices.Get` on the given path, but returns the
// low level result itself not performing unmarshalling.
func (s *SWbemServicesConnection) Dereference(referencePath string) (v *ole.VARIANT, err error) {
s.Lock()
if s.sWbemServices == nil {
s.Unlock()
return nil, ErrConnectionClosed
}
s.Unlock()
// Be aware of reflections and COM usage.
defer func() {
if r := recover(); r != nil {
err = multierror.Append(err, fmt.Errorf("runtime panic; %v", r))
}
}()
return s.dereference(referencePath)
}
func (s *SWbemServicesConnection) dereference(referencePath string) (v *ole.VARIANT, err error) {
return oleutil.CallMethod(s.sWbemServices, "Get", referencePath)
}
type queryDst struct {
dst reflect.Value
dsArgType multiArgType
dstElemType reflect.Type
}
func (s *SWbemServicesConnection) query(query string, dst *queryDst) (err error) {
// Be aware of reflections and COM usage.
defer func() {
if r := recover(); r != nil {
err = multierror.Append(err, fmt.Errorf("runtime panic; %v", r))
}
}()
// result is a SWBemObjectSet
resultRaw, err := oleutil.CallMethod(s.sWbemServices, "ExecQuery", query)
if err != nil {
return err
}
result := resultRaw.ToIDispatch()
defer func() {
if clErr := resultRaw.Clear(); clErr != nil {
err = multierror.Append(err, clErr)
}
}()
count, err := oleInt64(result, "Count")
if err != nil {
return err
}
enumProperty, err := result.GetProperty("_NewEnum")
if err != nil {
return err
}
defer func() {
if clErr := enumProperty.Clear(); clErr != nil {
err = multierror.Append(err, clErr)
}
}()
enum, err := enumProperty.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
if err != nil {
return err
}
if enum == nil {
return fmt.Errorf("can't get IEnumVARIANT, enum is nil")
}
defer enum.Release()
// Initialize a slice with Count capacity
dst.dst.Set(reflect.MakeSlice(dst.dst.Type(), 0, int(count)))
var errFieldMismatch error
for itemRaw, length, err := enum.Next(1); length > 0; itemRaw, length, err = enum.Next(1) {
if err != nil {
return err
}
// Closure for defer in the loop.
err := func() error {
item := itemRaw.ToIDispatch()
defer item.Release()
ev := reflect.New(dst.dstElemType)
if err = s.Unmarshal(item, ev.Interface()); err != nil {
if _, ok := err.(ErrFieldMismatch); ok {
// We continue loading entities even in the face of field mismatch errors.
// If we encounter any other error, that other error is returned. Otherwise,
// an ErrFieldMismatch is returned.
//
// Note that we are unmarshalling into the slice, so every element of the
// result will have the same error thus we can save the only error occurred.
errFieldMismatch = err
} else {
return err
}
}
if dst.dsArgType != multiArgTypeStructPtr {
ev = ev.Elem()
}
dst.dst.Set(reflect.Append(dst.dst, ev))
return nil
}()
if err != nil {
return err
}
}
return errFieldMismatch
}
type multiArgType int
const (
multiArgTypeInvalid multiArgType = iota
multiArgTypeStruct
multiArgTypeStructPtr
)
// checkMultiArg checks that v has type []S, []*S for some struct type S.
//
// It returns what category the slice's elements are, and the reflect.Type
// that represents S.
func checkMultiArg(v reflect.Value) (m multiArgType, elemType reflect.Type) {
if v.Kind() != reflect.Slice {
return multiArgTypeInvalid, nil
}
elemType = v.Type().Elem()
switch elemType.Kind() {
case reflect.Struct:
return multiArgTypeStruct, elemType
case reflect.Ptr:
elemType = elemType.Elem()
if elemType.Kind() == reflect.Struct {
return multiArgTypeStructPtr, elemType
}
}
return multiArgTypeInvalid, nil
}
func oleInt64(item *ole.IDispatch, prop string) (val int64, err error) {
v, err := oleutil.GetProperty(item, prop)
if err != nil {
return 0, err
}
defer func() {
if clErr := v.Clear(); clErr != nil {
err = multierror.Append(err, clErr)
}
}()
i := int64(v.Val)
return i, nil
}