none
#Import

    Question

  • Hey,
      Today I was using MSXML, and the example I was following used #import instead of following the traditional route of including the header for the declarations, and then linking to a library.  To my astonishment my same code worked with this and no #includes/libraries.  I realize that #import does something with the COM Server's type library, but how exactly does this work?  Does it build a header file based off the library that I can't see, and thats why my interface declarations still work etc?

    Basically I'm asking what is going on behind an #import and what all does it do?

    Cheers
    Wednesday, May 27, 2009 11:36 PM

Answers

  • A type library is an accurate representation of the COM interfaces.  It was generated by MIDL.  When you #include <msxml.h>, you are including a .h file that was generated by MIDL on a Microsoft machine.  When you #import <msxml3.dll>, you are importing the type library that was generated by MIDL on a Microsoft machine.  Two different paths, same source.

    #import should be your preference, it generates the _com_ptr_t smart pointers.

    Hans Passant.
    • Marked as answer by Valaris Thursday, May 28, 2009 3:49 AM
    Thursday, May 28, 2009 12:42 AM

All replies

  • It builds a tlh file (and a tli file as well) that is used as a header. Look in your DEBUG or RELEASE folder and take a look at its contents.
    Thursday, May 28, 2009 12:14 AM
  • A type library is an accurate representation of the COM interfaces.  It was generated by MIDL.  When you #include <msxml.h>, you are including a .h file that was generated by MIDL on a Microsoft machine.  When you #import <msxml3.dll>, you are importing the type library that was generated by MIDL on a Microsoft machine.  Two different paths, same source.

    #import should be your preference, it generates the _com_ptr_t smart pointers.

    Hans Passant.
    • Marked as answer by Valaris Thursday, May 28, 2009 3:49 AM
    Thursday, May 28, 2009 12:42 AM
  • Oh wow cool.  I wish I had more experience with COM :)  So the project automatically and invisibly will include these generated .h files at the point of the #import?  IE #import will effectively #include the headers for that module alone, not project wide?
    Thursday, May 28, 2009 12:45 AM
  • As Brian pointed out, they're visible in your build directory.

    Hans Passant.
    Thursday, May 28, 2009 1:05 AM