Interface

IDispatch


Description

IDispatch is the common interface exposed by automation servers (like Internet Explorer, Word, Excel, etc.). For automation servers that support dual interfaces you can use the OLEObject class to automate them.

Methods

Name

Parameters

Returns

Shared

GetIDofName

name As String

Integer

Invoke

dispIdMember As Integer, riid As Ptr, lcid As UInt32, wFlags As UInt16, ByRef pDispParams As COM.DISPPARAMS, pVarResult As Ptr, pExcepInfo As Ptr, ByRef puArgErr As UInt32

Integer

Method descriptions


IDispatch.GetIDofName

GetIDofName(name As String) As Integer

Gets a single DISPID (which can be used on calls to Invoke) from the specified name. If the name cannot be found this function returns -1.


IDispatch.Invoke

Invoke(dispIdMember As Integer, riid As Ptr, lcid As UInt32, wFlags As UInt16, ByRef pDispParams As COM.DISPPARAMS, pVarResult As Ptr, pExcepInfo As Ptr, ByRef puArgErr As UInt32) As Integer

Access properties and methods by ID.

Details on COM.DISPPARAMS can be found here.

Sample code

The following automates Internet Explorer, queries for a specific interface (each interface is identified by a unique IID), and invokes a method on the interface

Var ie As New OLEObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate("google.com")

Var unk As New COM.IUnknown(ie.Handle)

' Query for the IID_IWebBrowser2 interface
Var iid As MemoryBlock = COM.IIDFromString("{D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}")
Var out As Ptr
If COM.S_OK = unk.QueryInterface(iid, out) Then
  Var disp As New COM.IDispatch(out)
  Var id As Integer = disp.GetIDofName("FullName")

  ' If id = -1 Then the method doesn't exist
  ' This method has no parameters but we still have to pass in an empty structure
  Var params As COM.DISPPARAMS

  ' This method returns a BSTR which is stored in an OLE VARIANT
  Var result As New MemoryBlock(COM.SIZEOF_VARIANT)

  Var err As UInt32
  Var resultCode As Integer
  resultCode = disp.Invoke(id, COM.IID_NULL, COM.LOCALE_USER_DEFAULT, _
      COM.DISPATCH_METHOD + COM.DISPATCH_PROPERTYGET, params, result, Nil, err)

  If resultCode = COM.S_OK Then
    Var retVal As Variant = COM.VARIANTToRBVariant(result)
    MessageBox("Path to IE: " + retVal)

    ' Remember to free the OLE VARIANT
    COM.FreeVARIANT(result)
  End If
End If

Compatibility

All project types on all supported operating systems.

See also

COM module; IUnknown, IEnumVARIANT, IPicture.