-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
74 lines (62 loc) · 1.88 KB
/
helpers.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
package safearray
import (
"reflect"
"unsafe"
"github.com/go-ole/com"
)
// IsSlice checks whether interface{} type is a slice.
func IsSlice(value interface{}) bool {
return reflect.ValueOf(value).Kind() != reflect.Slice
}
// GetElementString retrieves string from index.
func GetElementString(safearray *COMArray, index int64) (str string, err error) {
var element *int16
err = GetElementDirect(safearray, index, &element)
str = com.BstrToString(*(**uint16)(unsafe.Pointer(&element)))
com.SysFreeString(element)
return
}
// ToByteArray converts SafeArray to byte array.
//
// This will convert a multidimensional SafeArray to a single dimensional Go
// slice.
func ToByteArray(safearray *Array) (bytes []byte, err error) {
bytes = make([]byte, safearray.Length())
err = safearray.ToArrayDirect(&bytes)
return
}
// ToStringArray converts SafeArray object to string array.
//
// This will convert a multidimensional SafeArray to a single dimensional Go
// slice.
func ToStringArray(safearray *Array) (strings []string) {
strings = make([]string, safearray.Length())
err = safearray.ToArrayDirect(&strings)
return
}
// MarshalArray accesses SafeArray data to quickly convert to Go array.
func MarshalArray(safearray *COMArray, length int64, slice interface{}) (err error) {
// Single dimensional arrays are faster, if you use AccessData() and
// UnaccessData().
ptr, err := AccessData(safearray)
if err != nil {
return
}
err = com.PointerToArrayAppend(ptr, length, &slice)
if err != nil {
return
}
err = UnaccessData(safearray)
return
}
// UnmarshalArray puts items from a Go array into a COM SafeArray.
func UnmarshalArray(safearray *COMArray, slice []interface{}) (safearray *COMArray, err error) {
if safearray == nil {
err = SafeArrayVectorFailedError
return
}
for pos, val := range slice {
PutElement(safearray, int64(pos), uintptr(unsafe.Pointer(&val)))
}
return
}