-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomutil.go
281 lines (253 loc) · 7.74 KB
/
comutil.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
package comutil
import (
"fmt"
"unsafe"
"github.com/go-ole/go-ole"
"github.com/google/uuid"
)
// CreateObject supports local creation of a single component object
// model interface. The class identified by the given class ID will be asked to
// create an instance of the supplied interface ID. If creation fails an error
// will be returned.
//
// It is the caller's responsibility to cast the returned interface to the
// correct type. This is typically done with an unsafe pointer cast.
func CreateObject(clsid uuid.UUID, iid uuid.UUID) (iface *ole.IUnknown, err error) {
serverInfo := &CoServerInfo{}
var context uint = ole.CLSCTX_SERVER
results := make([]MultiQI, 0, 1)
results = append(results, MultiQI{IID: GUID(iid)})
err = CreateInstanceEx(clsid, context, serverInfo, results)
if err != nil {
return nil, err
}
iface = results[0].Interface
if results[0].HR != ole.S_OK {
err = ole.NewError(results[0].HR)
} else if iface == nil {
err = ErrCreationFailed
}
return
}
// CreateRemoteObject supports remote creation of a single component object
// model interface. The class identified by the given class ID will be asked to
// create an instance of the supplied interface ID. If creation fails an error
// will be returned.
//
// If the provided server name is empty, this function will create an instance
// on the local machine. It is then the same as calling CreateObject.
//
// It is the caller's responsibility to cast the returned interface to the
// correct type. This is typically done with an unsafe pointer cast.
func CreateRemoteObject(server string, clsid uuid.UUID, iid uuid.UUID) (iface *ole.IUnknown, err error) {
var bserver *int16
if len(server) > 0 {
bserver = ole.SysAllocStringLen(server)
if bserver == nil {
return nil, ole.NewError(ole.E_OUTOFMEMORY)
}
defer ole.SysFreeString(bserver)
}
serverInfo := &CoServerInfo{
Name: bserver,
}
var context uint
if server == "" {
context = ole.CLSCTX_SERVER
} else {
context = ole.CLSCTX_REMOTE_SERVER
}
results := make([]MultiQI, 0, 1)
results = append(results, MultiQI{IID: GUID(iid)})
err = CreateInstanceEx(clsid, context, serverInfo, results)
if err != nil {
return nil, err
}
iface = results[0].Interface
if results[0].HR != ole.S_OK {
err = ole.NewError(results[0].HR)
} else if iface == nil {
err = ErrCreationFailed
}
return
}
// SafeArrayFromStringSlice creates a SafeArray from the given slice of strings.
//
// See http://www.roblocher.com/whitepapers/oletypes.html
func SafeArrayFromStringSlice(slice []string) *ole.SafeArray {
array, _ := SafeArrayCreateVector(ole.VT_BSTR, 0, uint32(len(slice)))
if array == nil {
panic("Could not convert []string to SAFEARRAY")
}
for i, v := range slice {
element := ole.SysAllocStringLen(v)
err := SafeArrayPutElement(array, int32(i), unsafe.Pointer(element))
ole.SysFreeString(element)
if err != nil {
panic(err)
}
}
return array
}
// VariantToValue attempts to convert the given variant to a native Go
// representation.
//
// If the value contains an IUnknown or IDispatch interface, it is the
// caller's responsibility to release it.
//
// If the value contains an an array with IUnknown or IDispatch members,
// it is the caller's responsibility to release them.
func VariantToValue(variant *ole.VARIANT) (value interface{}, err error) {
if array := variant.ToArray(); array != nil {
return SafeArrayToSlice(array)
}
return variant.Value(), nil
}
// SafeArrayToSlice converts the given array to a native Go representation. A
// slice of appropriately typed elements will be returned.
//
// If the array contains IUnknown or IDispatch members, it is the
// caller's responsibility to release them.
func SafeArrayToSlice(array *ole.SafeArrayConversion) (value interface{}, err error) {
vt, err := array.GetType()
if err != nil {
return
}
if ole.VT(vt) == ole.VT_VARIANT {
return SafeArrayToVariantSlice(array)
}
return SafeArrayToConcreteSlice(array)
}
// SafeArrayToConcreteSlice converts the given non-variant array to a native Go
// representation. A slice of appropriately typed elements will be returned.
//
// If the array contains variant elements an error will be returned.
//
// Only arrays of integers and bytes are supported. Support for additional
// types may be added in the future.
func SafeArrayToConcreteSlice(array *ole.SafeArrayConversion) (value interface{}, err error) {
vt, elems, err := arrayDetails(array)
if err != nil {
return
}
if vt == ole.VT_VARIANT {
return nil, ErrVariantArray
}
switch vt {
case ole.VT_UI1:
out := make([]byte, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_I1:
out := make([]int8, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_UI2:
out := make([]uint16, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_I2:
out := make([]int16, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_UI4:
out := make([]uint32, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_I4:
out := make([]int32, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_UI8:
out := make([]uint64, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
case ole.VT_I8:
out := make([]int64, elems)
for i := int32(0); i < elems; i++ {
copyArrayElement(array.Array, i, unsafe.Pointer(&out[i]), &err)
}
value = out
default:
err = ErrUnsupportedArray
}
return
}
// SafeArrayToVariantSlice converts the given variant array to a native Go
// representation. A slice of interface{} members will be returned.
//
// If the array does not contain variant members an error will be returned.
//
// If the array contains IUnknown or IDispatch members, it is the
// caller's responsibility to release them.
func SafeArrayToVariantSlice(array *ole.SafeArrayConversion) (values []interface{}, err error) {
vt, elems, err := arrayDetails(array)
if err != nil {
return
}
if vt != ole.VT_VARIANT {
return nil, ErrNonVariantArray
}
for i := int32(0); i < elems; i++ {
element := &ole.VARIANT{}
ole.VariantInit(element)
err = SafeArrayGetElement(array.Array, i, unsafe.Pointer(element))
if err != nil {
err = fmt.Errorf("unable to retrieve array element %d: %v", i, err)
ole.VariantClear(element)
return
}
value, valueErr := VariantToValue(element)
if valueErr != nil {
if err == nil {
err = fmt.Errorf("unable to interpret array element %d: %v", i, valueErr)
}
} else {
values = append(values, value)
}
// When returning a pointer to a COM interface, add a reference
// so that VariantClear doesn't release the interface below.
switch v := value.(type) {
case *ole.IDispatch:
v.AddRef()
case *ole.IUnknown:
v.AddRef()
}
ole.VariantClear(element)
}
return
}
func arrayDetails(array *ole.SafeArrayConversion) (vt ole.VT, elems int32, err error) {
_vt, err := array.GetType()
if err != nil {
return
}
vt = ole.VT(_vt)
dims, _ := SafeArrayGetDim(array.Array) // Error intentionally ignored
if dims != 1 {
err = ErrMultiDimArray
return
}
elems, err = array.TotalElements(0)
return
}
func copyArrayElement(from *ole.SafeArray, index int32, to unsafe.Pointer, err *error) {
e := SafeArrayGetElement(from, index, to)
if e != nil && *err == nil {
*err = fmt.Errorf("unable to retrieve array element %d: %v", index, e)
}
}