idispatch.go 2.9 KB

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