github.com/hspan/go-ole@v0.0.0/safearrayslices.go (about)

     1  // +build windows
     2  
     3  package ole
     4  
     5  import (
     6  	"unsafe"
     7  )
     8  
     9  func safeArrayFromByteSlice(slice []byte) *SafeArray {
    10  	array, _ := safeArrayCreateVector(VT_UI1, 0, uint32(len(slice)))
    11  
    12  	if array == nil {
    13  		panic("Could not convert []byte to SAFEARRAY")
    14  	}
    15  
    16  	for i, v := range slice {
    17  		safeArrayPutElement(array, int64(i), uintptr(unsafe.Pointer(&v)))
    18  	}
    19  	return array
    20  }
    21  
    22  func safeArrayFromStringSlice(slice []string) *SafeArray {
    23  	array, _ := safeArrayCreateVector(VT_BSTR, 0, uint32(len(slice)))
    24  
    25  	if array == nil {
    26  		panic("Could not convert []string to SAFEARRAY")
    27  	}
    28  	// SysAllocStringLen(s)
    29  	for i, v := range slice {
    30  		safeArrayPutElement(array, int64(i), uintptr(unsafe.Pointer(SysAllocStringLen(v))))
    31  	}
    32  	return array
    33  }
    34  
    35  func safeArrayFromInt32Slice(slice []int32) *SafeArray {
    36  	array, _ := safeArrayCreateVector(VT_I4, 0, uint32(len(slice)))
    37  
    38  	if array == nil {
    39  		panic("Could not convert []int32 to SAFEARRAY")
    40  	}
    41  	// SysAllocStringLen(s)
    42  	for i, v := range slice {
    43  		safeArrayPutElement(array, int64(i), uintptr(unsafe.Pointer(&v)))
    44  	}
    45  	return array
    46  }