github.com/hspan/go-ole@v0.0.0/safearrayconversion.go (about) 1 // Helper for converting SafeArray to array of objects. 2 3 package ole 4 5 import ( 6 "unsafe" 7 ) 8 9 type SafeArrayConversion struct { 10 Array *SafeArray 11 } 12 13 func (sac *SafeArrayConversion) ToStringArray() (strings []string) { 14 totalElements, _ := sac.TotalElements(0) 15 strings = make([]string, totalElements) 16 17 for i := int32(0); i < totalElements; i++ { 18 strings[int32(i)], _ = safeArrayGetElementString(sac.Array, i) 19 } 20 21 return 22 } 23 24 func (sac *SafeArrayConversion) ToByteArray() (bytes []byte) { 25 totalElements, _ := sac.TotalElements(0) 26 bytes = make([]byte, totalElements) 27 28 for i := int32(0); i < totalElements; i++ { 29 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&bytes[int32(i)])) 30 } 31 32 return 33 } 34 35 func (sac *SafeArrayConversion) ToValueArray() (values []interface{}) { 36 totalElements, _ := sac.TotalElements(0) 37 values = make([]interface{}, totalElements) 38 vt, _ := safeArrayGetVartype(sac.Array) 39 40 for i := int32(0); i < totalElements; i++ { 41 switch VT(vt) { 42 case VT_BOOL: 43 var v bool 44 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 45 values[i] = v 46 case VT_I1: 47 var v int8 48 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 49 values[i] = v 50 case VT_I2: 51 var v int16 52 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 53 values[i] = v 54 case VT_I4: 55 var v int32 56 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 57 values[i] = v 58 case VT_I8: 59 var v int64 60 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 61 values[i] = v 62 case VT_UI1: 63 var v uint8 64 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 65 values[i] = v 66 case VT_UI2: 67 var v uint16 68 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 69 values[i] = v 70 case VT_UI4: 71 var v uint32 72 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 73 values[i] = v 74 case VT_UI8: 75 var v uint64 76 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 77 values[i] = v 78 case VT_R4: 79 var v float32 80 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 81 values[i] = v 82 case VT_R8: 83 var v float64 84 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 85 values[i] = v 86 case VT_BSTR: 87 var v string 88 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 89 values[i] = v 90 case VT_VARIANT: 91 var v VARIANT 92 safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) 93 values[i] = v.Value() 94 default: 95 // TODO 96 } 97 } 98 99 return 100 } 101 102 func (sac *SafeArrayConversion) GetType() (varType uint16, err error) { 103 return safeArrayGetVartype(sac.Array) 104 } 105 106 func (sac *SafeArrayConversion) GetDimensions() (dimensions *uint32, err error) { 107 return safeArrayGetDim(sac.Array) 108 } 109 110 func (sac *SafeArrayConversion) GetSize() (length *uint32, err error) { 111 return safeArrayGetElementSize(sac.Array) 112 } 113 114 func (sac *SafeArrayConversion) TotalElements(index uint32) (totalElements int32, err error) { 115 if index < 1 { 116 index = 1 117 } 118 119 // Get array bounds 120 var LowerBounds int32 121 var UpperBounds int32 122 123 LowerBounds, err = safeArrayGetLBound(sac.Array, index) 124 if err != nil { 125 return 126 } 127 128 UpperBounds, err = safeArrayGetUBound(sac.Array, index) 129 if err != nil { 130 return 131 } 132 133 totalElements = UpperBounds - LowerBounds + 1 134 return 135 } 136 137 // Release Safe Array memory 138 func (sac *SafeArrayConversion) Release() { 139 safeArrayDestroy(sac.Array) 140 }