How to access COM component within a project

I'm still using Visual Studio 2005 for some C++ development. Thanks to ATL, I can use any COM component with modest amount of coding. For example, I can simply use #import directive to make a COM component accessible from my C++ code, then use CComPtr for easy access (and don't need to worry about releasing it, as it is an auto pointer).

#import directive takes DLL, ProgID, TLB, etc. This is fine when you use a third-party component, or a component outside of your solution. But I wonder why it doesn't take IDL file (I may be missing something here).

But if there's a COM component project within a solution, and you try to use the COM component from a project within the same solution, #import may not be handy. I don't want to write a code like this.

#import "..\MyComComponent\Release\MyComComponent.dll" no_namespace

The dll file is created under release or debug sub folders. Tlb is the same. I can use ProgID, but I tend to forget what it is.

Because IDL file is accessible, why can't #import take this? Well, the reason may be because you don't need to. I stumbled here.

The COM project automatically creates a header file from the IDL. All I needed to do was to include the header file generated. In the case above, the COM project must have created MyComComponent.h (and MyComCOmponent_i.c). From a project in the solution, it can be included like this.

include "..\MyComComponent\MyComComponent.h"

This look much more elegant than using #import with release/debug sub folders.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Link to Amazon products with: [amazon product_id inline|full|thumbnail]. Example: [amazon 1590597559 thumbnail]
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.