- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- The History of ATL
- ATL's Advanced Use of Templates
- Commonly Used ATL Classes
- Using the Interface Definition Language
- ATL Wizards
- Summary
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
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:
- The language runtime must provide some way to call a function or method indirectly through an interface function table. This indirection is commonly known as a virtual-function table, or vtable in C++. Some languages that support COM, such as Visual Basic, hide this capability from the programmer and treat it as a language implementation detail.
- The language, or at least its runtime, must be capable of reference count maintenance for COM interfaces. Some languages, such as C and C++, require the programmer to explicitly manage reference counts. Other languages, such as Visual Basic, hide this complexity from the programmer.
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.
ATL Wizards | Next Section

Account Sign In
View your cart