Page #42 (Processing IDL Files)

< BACK  NEXT >
[oR]

IDL File Structure

Now that we know how to process an IDL file, let s see how we go about defining an IDL file.

Broadly speaking, an IDL file is split into three sections: a preprocessor section, an interface section, and the library section.

The Preprocessor Section

The first stage of MIDL processing is to run a C preprocessor on the input file. This means that the familiar C preprocessor directives such as #define, #include, and #if are also available under IDL. Following is an example of a define statement in C that is also a valid IDL statement:

 #define BUFSIZE 512 

As macros are preprocessed, they are not reproduced in the generated C++ header files. For cases where it is desirable for some text to make it through C preprocessing and into the generated C/C++ code, IDL offers a keyword, cpp_quote. The following IDL statement shows its usage:

 cpp_quote("extern char* g_pszMyName;") 

The above IDL statement results in generating the following C declaration in the output header file:

 extern char* g_pszMyName; 

In the preprocessing section, one can also use the #include directive. This causes the information from the specified file to be preprocessed. In the following IDL statement, definitions from the standard include file, stdio.h, are absorbed into the IDL compilation.

 #include <stdio.h> 

All legitimate declarations, including structures and function prototypes, make their way into the generated header file.

For referencing definitions from other IDL files, IDL offers a directive called import. The following IDL statement, for example, will reference the definitions from IDL file wtypes.idl:

 import "wtypes.idl" 

When the IDL file containing the statement above is compiled, MIDL will insert an include directive for wtypes.h in the generated header file as shown here:

 #include "wtypes.h" 

Note that, unlike the #include directive, the import directive ignores function prototypes.

The Interface Section

One or more interfaces, along with user-defined data types such as structures, enumerators, and even typedefs, can be defined in the interface section.

Note that the preprocessor section and the interface section need not be physically separate in the IDL file. One can mix preprocessor statements with interface declarations if desired.

The Type Library Section

The type library section is identified by the keyword library, as shown below:

 [   uuid(318B4AD3-06A7-11d3-9B58-0080C8E11F14),    helpstring("VCR Type Library")  ]  library VcrLib  {   importlib("stdole32.tlb");    importlib("stdole2.tlb");    [     uuid(318B4AD4-06A7-11d3-9B58-0080C8E11F14),      helpstring("VCR Class")    ]    coclass VCR    {     [default] interface IVideo;      interface ISVideo;    };  }; 

The library section is a named entry (VcrLib in the above sample) and is uniquely identified by a GUID.

The library section primarily contains a preprocessor subsection, the interface subsection, and a COM class subsection. [3] The preprocessor and the interface subsections are similar to ones described earlier. Anything that is defined outside the library scope could have been defined within the library scope, if desired. In fact, the whole IDL file could be one big library section. However, in this case, there will be just one output file the type library.

[3] There is also a module subsection. However, it doesn t deserve any attention, as its usage is not relevant to COM programming.

The library section is what causes the type library to be generated. However, declaring the library section is optional. If an IDL file does not contain the library section, a type library will not be generated.

Any interface, structure, enumeration, or any such data type that is either defined or referenced within the library section will be saved in the generated type library.

There is one preprocessor directive, importlib, which can only be used within the library section. We will examine its usage when we deal with multiple IDL files.


< BACK  NEXT >


COM+ Programming. A Practical Guide Using Visual C++ and ATL
COM+ Programming. A Practical Guide Using Visual C++ and ATL
ISBN: 130886742
EAN: N/A
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net