In VB, you can add the COM component to be used through "Reference". In VC ++, you can use the "# import" statement to add the COM component. Using "# import", VC ++ will automatically generate all the packaging classes of interfaces in COM components, and encapsulate the handling of COM exceptions, so that we can call them in a strongly typed way. Of course, "# import" is not necessary, for example, when we access the COM component through the idispatch or idispatchex interface.
# Import Syntax:
# import "FILENAME" [attributes] filename can be a file containing a Type Library, such. olb ,. TLB ,. the DLL file filename can also be the guid of the Type Library, or the COM component id
for example:
// import the mshtml component # pragma warning (Disable: 4192) // avoid a large number of compilation warnings # import "mshtml. TLB "RENAME (" translateaccelerator "," translateaccelerator_html ") // import the MSXML component # import" progid: msxml2.domdocument "// import the ADODB component # import" id lib: 00000205-0000-0010-8000-00aa006d2ea4 "/Rename (" EOF "," endoffile ")/Rename (" error "," adoerror ")
when developing a component for pimshell, the following suggestions are provided:
- try to import components through # import instead of using interfaces provided by Windows SDK. For example, the ixmldomdocument interface of ihtmldocument2 and MSXML components in the mshtml component is provided in the Windows SDK. As we mentioned earlier, when "# import" is used, VC ++ will automatically generate all packaging classes for interfaces in COM components and encapsulate the processing of COM exceptions.
- Use namespaces whenever possible. In this way, you can avoid conflicts with the interfaces provided by the Windows SDK, and make full use of the advantages of VC ++.
- reasonable use of # pragma warning reduces a lot of compilation warnings and accelerates project compilation.