4

I'm having to immerse myself in C++ COM programming again, and have forgotten most of the hard-learned lessons from last time. (when I think the phrase 'never again' may have been used in error.)

What are the most common mistakes and anti-patterns of COM development in C++? I'm using Borland C++Builder, but I'm after things that apply to all C++ compilers.

Roddy
  • 66,617
  • 42
  • 165
  • 277

2 Answers2

2

My mistake was not reading the manual. I just tried to get by using tutorials and code samples found online. I wasted many hours on problems that would have been easy to solve had I gotten a good basic understanding of COM.

StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • 1
    This. +1. Definitely. You will drown yourself if you don't master what is going on under the hood. This is sad (and proves that COM is a really poor framework), but hard learned. Make sure you understand the object model, the types, the common interfaces (esp. IDispatch if you are using it). – Alexandre C. Apr 07 '11 at 13:14
0

I'll kick the ball rolling myself with the first one I tripped over again:

Don't pass literal strings to functions that require BSTR parameters. See the remarks section here.

CComPtr<IFoo> foo;

foo->bar("Bletch!"); // No valid BSTR prefix, so bad things will happen.

Instead, use...

foo->bar(CComBSTR("Bletch!"));
Roddy
  • 66,617
  • 42
  • 165
  • 277
  • This way BSTR will leak unless the called code takes ownership of it (the latter is very unusual). You should use a wrapper class like `CComBSTR` or `_bstr_t`. – sharptooth Apr 08 '11 at 05:58
  • @sharptooth. Thanks, fixed. I think that's a common mistake, too :-( – Roddy Apr 12 '11 at 11:19