Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Using the Interface Definition Language

As discussed in earlier chapters, COM is a language-neutral specification that defines how objects interact on a binary level. Although some languages are more COM-friendly than others, COM makes no demands on languages except the following:

Given these relatively small requirements, most procedural languages can be made to work with COM.

This openness causes a problem with respect to defining interfaces for COM classes. How should an interface be defined in order to be used by any modern language? There are nearly as many ways to define interfaces as there are computer languages.

All COM interfaces are defined using the Interface Definition Language (IDL). IDL is similar to C and C++ header files, with new syntax elements specific to distributed computing added. IDL files are compiled to create source files and binary type libraries that can be used by practically all programming environments.

IDL was not created for COM—it has been used for years to define the interactions between systems that use Remote Procedure Call (RPC) interfaces.

Microsoft uses a version of IDL known as Microsoft Interface Definition Language (MIDL). MIDL is very much like IDL, with a few extra syntax features used to help define COM classes and interfaces. Listing 29.6 shows a typical MIDL code fragment.

Example 29.6. A Typical MIDL Source File

import "oaidl.idl";
import "ocidl.idl";
    [
        object,
        uuid(78EDC064-9077-4CE5-AA93-A096B9766DBD),
        dual,
        helpstring("ILatte Interface"),
        pointer_default(unique)
    ]
     interface ILatte : IDispatch
    {
        [id(1), HRESULT AddFoam([in] short nDepth);
        [id(2), HRESULT GetFoamDepth([out] short* pnDepth);
        [id(3), HRESULT AddVanillaShot([in] short nShots);
    };

[
    uuid(063FDFB3-8ACA-4A64-BFE3-3795F81388CA),
    version(1.0),
    helpstring("JensCoffee 1.0 Type Library")
]
library JensCoffeeLib
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");

    [
        uuid(2AE38564-6EB0-44EE-8063-4064A40143A7),
        helpstring("Latte Class")
    ]
   coclass Latte
    {
        [default] interface ILatte;
    };
};

The MIDL source provided in Listing 29.6 has three main sections (the first line of each section is shown in bold):

  • The declaration of the ILatte interface. This section describes the attributes of the interface and specifies the signature for each function in the interface.
  • The declaration of JensCoffeeLib, which is used to generate a type library. A type library is a binary representation of the IDL and is discussed in Chapter 30, "Creating COM Objects Using ATL."
  • The declaration of the Latte coclass. The COM class, or coclass, is declared inside the library declaration. It lists the interfaces (and other objects) that are part of the Latte COM class.

Each of these objects begins with a list of descriptive attributes enclosed in brackets, like this:

[object]

Attributes are discussed in the next section.

In addition to interfaces and COM classes, here are some other objects you might find frequently in a MIDL file:

dispinterface Marks an Automation-compatible interface as being derived from IDispatch
enum Creates an enumerated type, just as in C and C++
struct Creates an aggregated type, just as in C and C++
union Creates an aggregated type that contains one of several possible types, just as in C and C++

This is only a partial list. The MIDL documentation included on your MSDN CD-ROM contains a complete MIDL reference.

When you create a COM class using ATL and the associated Visual C++ wizards, the IDL for the ATL class is created for you automatically. Simple controls and ATL classes often can use the auto-generated IDL file without further modification. More complex COM components often require a manual edit of the IDL source, however.

In practice, you will need to know how to manipulate the IDL file manually—the more you know about IDL, the more effective you will be as a COM developer. Using IDL with the ATL class library is discussed in more detail in Chapter 30.

Share ThisShare This

Informit Network