The #import Technique

In this section, we’ll look at a different technique to communicate with OCRServer. In particular, we will use the #import technique to talk to distributed components. Visual C++ comes with a compiler directive, called #import, that works nicely with COM. Like the MFC ClassWizard, this directive reverse engineers the contents of a type library to create C++ representations of COM interfaces. You know how to add a handler for the Compiler button, so do that first and name the handler OnCompiler. In this handler, you will add code to use the IOcrProcessor interface with the #import technique.

The #import Statement

To use this technique, simply import the appropriate type library into your header or implementation file. For example, to get C++ representations of all interfaces stored within the OCRServer type library, simply add the following lines to OCRClientDlg.cpp:

#import "..\OCRServer\OCRServer.tlb"

The #import directive tells the compiler to generate C++ wrapper code from the type information stored in the indicated type library. Since type information can reside in files with extensions such as TLB, EXE, OCX, and DLL, you can use the #import directive on all these file types, as long as they have COM type information built in. This directive is pretty nifty because it comes with a number of useful attributes. By default, the reverse engineered code is wrapped within a C++ namespace[77] whose name is derived from that of the selected type library; in our case, ...

Get Learning DCOM now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.