blob: d4af1240925ddd1560d47e9036741eb4a98d1647 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001package ole
2
3import "unsafe"
4
5type IDispatch struct {
6 IUnknown
7}
8
9type IDispatchVtbl struct {
10 IUnknownVtbl
11 GetTypeInfoCount uintptr
12 GetTypeInfo uintptr
13 GetIDsOfNames uintptr
14 Invoke uintptr
15}
16
17func (v *IDispatch) VTable() *IDispatchVtbl {
18 return (*IDispatchVtbl)(unsafe.Pointer(v.RawVTable))
19}
20
21func (v *IDispatch) GetIDsOfName(names []string) (dispid []int32, err error) {
22 dispid, err = getIDsOfName(v, names)
23 return
24}
25
26func (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
31func (v *IDispatch) GetTypeInfoCount() (c uint32, err error) {
32 c, err = getTypeInfoCount(v)
33 return
34}
35
36func (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.
45func (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.
62func (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.
78func (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.
87func (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.
92func (v *IDispatch) PutProperty(name string, params ...interface{}) (*VARIANT, error) {
93 return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYPUT, params)
94}