IDispatch
From Xojo Documentation
Interface
This method is only available on the Windows platform. For cross-platform compatibility, use #If...#Endif with the Target... specifiers to make sure you will not attempt to use this method on an incompatible platform. |
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.
Method | Description |
---|---|
GetIDofName | Gets a single DISPID (which can be used on calls to Invoke) from the name |
GetTypeInfo | Retrieves the type information for an object, which can then be used to get the type information for an interface. |
GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). |
Invoke | Provides access to properties and methods exposed by an object. |
Methods | ||
|
Examples
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
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
See Also
COM module; IUnknown, IEnumVARIANT, IPicture.