The #import
Directive
C++ Specific
The #import
directive is used to incorporate information from a type library. The
content of the type library is converted into C++ classes, mostly describing
the COM interfaces.
Syntax
#import
"filename" [attributes]
#import
<filename> [attributes]
attributes:
attribute1, attribute2, ...
attribute1 attribute2 ...
filename
is the name of the file containing the type library information. A file can
be one of the following types:
- a type
library (.TLB or .ODL) file
- an
executable (.EXE) file
- a library
(.DLL) file containing a type library resource (such as .OCX)
- a compound
document holding a type library
- any other
file format that can be understood by the LoadTypeLib API
The filename
is optionally preceded by a directory specification. The filename must name
an existing file. The difference between the two forms is the order in which
the preprocessor searches for the type library files when the path is
incompletely specified.
|
Syntax Form |
Action |
|
Quoted form |
This form
instructs the preprocessor to first look for type library files in the
same directory of the file that contains the #import statement,
and then in the directories of whatever files that include (#include)
that file. The preprocessor then searches along the paths shown below. |
|
Angle-bracket
form |
This form
instructs the preprocessor to search for type library files along the
paths shown below. |
The compiler
will search in the following directories for the named file:
- the PATH
environment variable path list
- the LIB
environment variable path list
- the path
specified by the /I (additional include directories) compiler option
#import
can optionally include one or more attributes. These attributes tell the
compiler to modify the contents of the type-library headers. A backslash (\)
symbol can be used to include additional lines in a single #import
statement. For example:
#import “test.lib” no_namespace \
rename(“OldName”, “NewName”)
The #import
attributes are listed below:
|
exclude |
high_method_prefix |
|
high_property_prefixes |
implementation_only |
|
include(…) |
inject_statement |
|
named_guids |
no_auto_exclude |
|
no_implementation |
no_namespace |
|
raw_dispinterfaces |
raw_interfaces_only |
|
raw_method_prefix |
raw_native_types |
|
raw_property_prefixes |
rename |
|
rename_namespace |
|
#import
creates two header files that reconstruct the type library contents in C++
source code. The primary header file is similar to that produced by the
Microsoft Interface Definition Language (MIDL) compiler, but with additional
compiler-generated code and data. The primary header file has the same base
name as the type library, plus a .TLH extension. The secondary header file
has the same base name as the type library, with a .TLI extension. It
contains the implementations for compiler-generated member functions, and is
included (#include) in the primary header file.
Both header
files are placed in the output directory specified by the /Fo (name object
file) option. They are then read and compiled by the compiler as if the
primary header file was named by a #include directive.
The following
compiler optimizations come with the #import directive:
- The header
file, when created, is given the same timestamp as the type library.
- When
#import is processed, the compiler first checks if the header exists
and is up to date. If yes, then it does not need to be recreated.
- The compiler
delays initializing the OLE subsystem until the first #import
command is encountered.
The #import
directive also participates in minimal rebuild and can be placed in a
precompiled header file.
|