Using the OLE 2 IDispatch Interface

Microsoft Excel exposes its objects as OLE Automation objects. OLE 2 allows another application, called an OLE Automation Controller, to access the exposed objects and use their properties and methods with the IDispatch interface or by means of vtbl binding. This chapter describes techniques you can use to write an OLE Automation Controller in standard C that can be used to manipulate objects, properties, and methods in Microsoft Excel.

In Chapters 7 and 8, you will see how an application can use the C API interface to call into Microsoft Excel. The C API interface exposes Microsoft Excel macro-language functions and commands. The OLE 2 IDispatch interface allows an application to call Microsoft Excel and manipulate any object, property, or method that can be called from Visual Basic.

This chapter does not discuss techniques for implementing an OLE Server (an application that creates new OLE objects and exposes their properties and methods using an IDispatch interface) or techniques for using vtbl binding. Microsoft Excel operates as both an OLE Server (Microsoft Excel creates objects that can be accessed by other applications) and as an OLE Automation Controller (Visual Basic in Microsoft Excel can access objects in other applications that support OLE Automation, and in fact, Visual Basic uses OLE Automation to call Microsoft Excel properties and methods). Vtbl binding requires an application to support dual interfaces that can be used by either IDispatch or vtbl calls. Of the 193 interfaces in Microsoft Excel, only 19 of them are marked as dual and can be used for vtbl binding. Becuase of the relatively low number of interfaces, which does not include the Range object, it is doubtful that many developers will eschew IDispatch for vtbl binding.

The examples in this chapter were built and tested with Microsoft Windows 95 and Windows NT Workstation version 4.0 (SP2), using Microsoft Visual C++ version 4.2b. Visual C++ version 4.2b includes the OLE 2 header files and libraries required to create OLE Automation Controller applications. If you use another C compiler, you will also need the Microsoft Win32 SDK.

In addition to the Help provided with Visual C++ and the OLE 2 SDK, Volume 2 of the OLE 2 Programmer's Reference provides detailed information about OLE Automation and the IDispatch interface. This chapter focuses mainly on implementation details for using the IDispatch interface with Microsoft Excel. You should use the OLE 2 Programmer's Reference with this chapter to understand the IDispatch interface.