-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmi.go
204 lines (175 loc) · 5.82 KB
/
wmi.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
//go:build windows
// +build windows
/*
Package wmi provides a WQL interface for WMI on Windows.
Example code to print names of running processes:
type Win32_Process struct {
Name string
}
func main() {
var dst []Win32_Process
q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst)
if err != nil {
log.Fatal(err)
}
for i, v := range dst {
println(i, v.Name)
}
}
*/
package wmi
import (
"errors"
"fmt"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"sync"
)
var (
ErrInvalidEntityType = errors.New("wmi: invalid entity type")
// ErrNilCreateObject is the error returned if CreateObject returns nil even
// if the error was nil.
ErrNilCreateObject = errors.New("wmi: create object returned nil")
lock sync.Mutex
)
// S_FALSE is returned by CoInitializeEx if it was already called on this thread.
const S_FALSE = 0x00000001
// Query runs the WQL query and appends the values to dst.
//
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
// the query must have the same name in dst. Supported types are all signed and
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
// Array types are not supported.
//
// By default, the local machine and default namespace are used. These can be
// changed using connectServerArgs. See
// https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/swbemlocator-connectserver
// for details.
//
// CallMethod calls a method named methodName on an instance of the class named
// className, with the given params.
//
// CallMethod is a wrapper around DefaultClient.CallMethod.
func CallMethod(connectServerArgs []interface{}, className, methodName string, params []interface{}) (int32, error) {
return DefaultClient.CallMethod(connectServerArgs, className, methodName, params)
}
// A Client is an WMI query client.
//
// Its zero value (DefaultClient) is a usable client.
type Client struct {
// NonePtrZero specifies if nil values for fields which aren't pointers
// should be returned as the field types zero value.
//
// Setting this to true allows stucts without pointer fields to be used
// without the risk failure should a nil value returned from WMI.
NonePtrZero bool
// PtrNil specifies if nil values for pointer fields should be returned
// as nil.
//
// Setting this to true will set pointer fields to nil where WMI
// returned nil, otherwise the types zero value will be returned.
PtrNil bool
// AllowMissingFields specifies that struct fields not present in the
// query result should not result in an error.
//
// Setting this to true allows custom queries to be used with full
// struct definitions instead of having to define multiple structs.
AllowMissingFields bool
// SWbemServiceClient is an optional SWbemServices object that can be
// initialized and then reused across multiple queries. If it is null
// then the method will initialize a new temporary client each time.
}
// DefaultClient is the default Client and is used by Query, QueryNamespace, and CallMethod.
var DefaultClient = &Client{}
// coinitService coinitializes WMI service. If no error is returned, a cleanup function
// is returned which must be executed (usually deferred) to clean up allocated resources.
func (c *Client) coinitService(connectServerArgs ...interface{}) (*ole.IDispatch, func(), error) {
var unknown *ole.IUnknown
var wmi *ole.IDispatch
var serviceRaw *ole.VARIANT
// be sure teardown happens in the reverse
// order from that which they were created
deferFn := func() {
if serviceRaw != nil {
serviceRaw.Clear()
}
if wmi != nil {
wmi.Release()
}
if unknown != nil {
unknown.Release()
}
ole.CoUninitialize()
}
// if we error'ed here, clean up immediately
var err error
defer func() {
if err != nil {
deferFn()
}
}()
err = ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
if err != nil {
oleCode := err.(*ole.OleError).Code()
if oleCode != ole.S_OK && oleCode != S_FALSE {
return nil, nil, err
}
}
unknown, err = oleutil.CreateObject("WbemScripting.SWbemLocator")
if err != nil {
return nil, nil, err
} else if unknown == nil {
return nil, nil, ErrNilCreateObject
}
wmi, err = unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
return nil, nil, err
}
// service is a SWbemServices
serviceRaw, err = oleutil.CallMethod(wmi, "ConnectServer", connectServerArgs...)
if err != nil {
return nil, nil, err
}
return serviceRaw.ToIDispatch(), deferFn, nil
}
// CallMethod calls a WMI method named methodName on an instance
// of the class named className. It passes in the arguments given
// in params. Use connectServerArgs to customize the machine and
// namespace; by default, the local machine and default namespace
// are used. See
// https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/swbemlocator-connectserver
// for details.
func (c *Client) CallMethod(connectServerArgs []interface{}, className, methodName string, params []interface{}) (int32, error) {
service, cleanup, err := c.coinitService(connectServerArgs...)
if err != nil {
return 0, fmt.Errorf("coinit: %v", err)
}
defer cleanup()
// Get class
classRaw, err := oleutil.CallMethod(service, "Get", className)
if err != nil {
return 0, fmt.Errorf("CallMethod Get class %s: %v", className, err)
}
class := classRaw.ToIDispatch()
defer classRaw.Clear()
// Run method
resultRaw, err := oleutil.CallMethod(class, methodName, params...)
if err != nil {
return 0, fmt.Errorf("CallMethod %s.%s: %v", className, methodName, err)
}
resultInt, ok := resultRaw.Value().(int32)
if !ok {
return 0, fmt.Errorf("return value was not an int32: %v (%T)", resultRaw, resultRaw)
}
return resultInt, nil
}
func oleInt64(item *ole.IDispatch, prop string) (int64, error) {
v, err := oleutil.GetProperty(item, prop)
if err != nil {
return 0, err
}
defer v.Clear()
i := int64(v.Val)
return i, nil
}