The primary
type library header file consists of seven sections:
Heading
boilerplate: Consists of comments, #include statement for
COMDEF.H (which defines some standard macros used in the header), and
other miscellaneous setup information.
Forward references and typedefs: Consists of structure declarations such
as struct IMyInterface,
and typedefs for any TKIND_ALIAS items.
Smart pointer declarations: The template class
_com_ptr_t is a
smart-pointer implementation that encapsulates interface pointers and
eliminates the need to call AddRef, Release, QueryInterface functions. In addition, it hides the
CoCreateInstance call in creating a new COM object. This section uses
macro statement _COM_SMARTPTR_TYPEDEF to establish typedefs of COM
interfaces to be template specializations of the _com_ptr_t template
class. For example, for interface IFoo, the .TLH file will contain:
Type
IFooPtr can then be used in place of the
raw interface pointer IFoo*.
Consequently, there is no need to call the various IUnknown member
functions.
Typeinfo
declarations: Primarily consists of class definitions and other items
exposing the individual typeinfo items returned by ITypeLib:GetTypeInfo.
In this section, each typeinfo from the type library is reflected in the
header in a form dependent on the
TYPEKIND information.
Optional
old-style GUID definition: Contains initializations of the named GUID
constants. These are names of the form CLSID_CoClass and IID_Interface, similar to those generated by the MIDL compiler.
#include
statement for the secondary type library header.
Footer boilerplate: Currently includes
#pragma pack(pop).
All sections,
except the heading boilerplate and footer boilerplate section, are enclosed
in a namespace with its name specified by the library statement in
the original IDL file. You can use the names from the type library header
either by an explicit qualification with the namespace name or by including
the following statement:
using namespace MyLib;
immediately
after the #import statement in the source code.
The namespace
can be suppressed by using the no_namespace attribute of the #import
directive. However, suppressing the namespace may lead to name collisions.
The namespace can also be renamed by the rename_namespace attribute.
The compiler
provides the full path to any type library dependency required by the type
library it is currently processing. The path is written, in the form of
comments, into the type library header (.TLH) that the compiler generates
for each processed type library.
If a type
library includes references to types defined in other type libraries, then
the .TLH file will include comments of the following sort:
//
// Cross-referenced type libraries:
//
// #import "c:\path\typelib0.tlb"
//
The actual
filename in the #import comment is the full path of the
cross-referenced type library, as stored in the registry. If you encounter
errors that are due to missing type definitions, check the comments at the
head of the .TLH to see which dependent type libraries may need to be
imported first. Likely errors are syntax errors (e.g. C2143, C2146, C2321),
C2501 (missing decl-specifiers), or C2433 ('inline' not permitted on data
declaration) while compiling the .TLI file.
You must
determine which of the dependency comments are not otherwise provided for by
system headers and then provide an #import directive at some point
before the #import directive of the dependent type library in order
to resolve the errors.