github.com/hspan/go-ole@v0.0.0/idispatch.go (about) 1 package ole 2 3 import "unsafe" 4 5 type IDispatch struct { 6 IUnknown 7 } 8 9 type IDispatchVtbl struct { 10 IUnknownVtbl 11 GetTypeInfoCount uintptr 12 GetTypeInfo uintptr 13 GetIDsOfNames uintptr 14 Invoke uintptr 15 } 16 17 func (v *IDispatch) VTable() *IDispatchVtbl { 18 return (*IDispatchVtbl)(unsafe.Pointer(v.RawVTable)) 19 } 20 21 func (v *IDispatch) GetIDsOfName(names []string) (dispid []int32, err error) { 22 dispid, err = getIDsOfName(v, names) 23 return 24 } 25 26 func (v *IDispatch) Invoke(dispid int32, dispatch int16, params ...interface{}) (result *VARIANT, err error) { 27 result, err = invoke(v, dispid, dispatch, params...) 28 return 29 } 30 31 func (v *IDispatch) GetTypeInfoCount() (c uint32, err error) { 32 c, err = getTypeInfoCount(v) 33 return 34 } 35 36 func (v *IDispatch) GetTypeInfo() (tinfo *ITypeInfo, err error) { 37 tinfo, err = getTypeInfo(v) 38 return 39 } 40 41 // GetSingleIDOfName is a helper that returns single display ID for IDispatch name. 42 // 43 // This replaces the common pattern of attempting to get a single name from the list of available 44 // IDs. It gives the first ID, if it is available. 45 func (v *IDispatch) GetSingleIDOfName(name string) (displayID int32, err error) { 46 var displayIDs []int32 47 displayIDs, err = v.GetIDsOfName([]string{name}) 48 if err != nil { 49 return 50 } 51 displayID = displayIDs[0] 52 return 53 } 54 55 // InvokeWithOptionalArgs accepts arguments as an array, works like Invoke. 56 // 57 // Accepts name and will attempt to retrieve Display ID to pass to Invoke. 58 // 59 // Passing params as an array is a workaround that could be fixed in later versions of Go that 60 // prevent passing empty params. During testing it was discovered that this is an acceptable way of 61 // getting around not being able to pass params normally. 62 func (v *IDispatch) InvokeWithOptionalArgs(name string, dispatch int16, params []interface{}) (result *VARIANT, err error) { 63 displayID, err := v.GetSingleIDOfName(name) 64 if err != nil { 65 return 66 } 67 68 if len(params) < 1 { 69 result, err = v.Invoke(displayID, dispatch) 70 } else { 71 result, err = v.Invoke(displayID, dispatch, params...) 72 } 73 74 return 75 } 76 77 // CallMethod invokes named function with arguments on object. 78 func (v *IDispatch) CallMethod(name string, params ...interface{}) (*VARIANT, error) { 79 return v.InvokeWithOptionalArgs(name, DISPATCH_METHOD, params) 80 } 81 82 // GetProperty retrieves the property with the name with the ability to pass arguments. 83 // 84 // Most of the time you will not need to pass arguments as most objects do not allow for this 85 // feature. Or at least, should not allow for this feature. Some servers don't follow best practices 86 // and this is provided for those edge cases. 87 func (v *IDispatch) GetProperty(name string, params ...interface{}) (*VARIANT, error) { 88 return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYGET, params) 89 } 90 91 // PutProperty attempts to mutate a property in the object. 92 func (v *IDispatch) PutProperty(name string, params ...interface{}) (*VARIANT, error) { 93 return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYPUT, params) 94 }