Re: to #import or not to #import
"Jason S" <jmsachs@gmail.com> schrieb im Newsbeitrag
news:1151584687.821303.132370@x69g2000cwx.googlegroups.com...
I'm wondering what those of you with lots of COM experience who write
clients using C++ do for error checking/handling:
1) straight COM calls, e.g hr = p->something(blah); if (!SUCCEEDED(hr))
{ return hr; } // or throw exception
2) something like Chris Sells' "Young Person's Guide to COM Programming
Style":
http://www.sellsbrothers.com/writing/default.aspx?content=a_young_person.htm
3) use #import and the _com_ptr_t / automated-wrapper stuff which
automatically throws exceptions for most non-SUCCEEDED HRESULTs.
I have two purposes:
a) for myself, to make life easier w/o a huge performance hit -- until
recently I was unaware of the automated wrappers produced by #import, &
am trying to figure out the costs of doing this (benefits = simpler
programming for the most part). Some of what I do is
processor-intensive & I'm not sure how much time my CPU is spending
creating "smart" objects like _bstr_t, _variant_t, _com_ptr_t, and
exception objects.
If whatever you are doing already does require a lot of work by your
processor, those little wrappers will not add very much. And in the time you
are fixing a single AddRef/Release bug in "traditional" COM code, your
program could create quite a lot of those little helpers. So go ahead and
make YOUR life as easy as possible. If it really turns out to be too time
consuming, you can still optimize the critical parts of your programe, once
you know your program is working and you know what the critical parts really
are. And remember the two primary rules of optimization
* don't do it now
* don't do it
b) I am developing COM libraries & working w/ people who would be
writing clients; they don't have much COM experience and I'm trying to
come up w/ some simple understandable examples for them to use my
library. Simplicity / robustness are almost certainly more important
than performance here.
What would be easier for a less experience user? Using #import and its
wrappers or doing all those CoCreateInstance's, AddRef's, Release's and
if(FAILED's manually? And don't forget, when your less experienced
co-workers will run into trouble, they will ask you for advice, so it will
be you, who will be fixing their bugs if you suggest them difficult
techniques. So teach them what is easy for you. If they think they are smart
enough to do it the complicated way, that's ok, but it will not be your
fault.
I do a lot of COM developing myself, and usually only describe my interfaces
as if those wrappers generated by #import are the only way to use them.
Those descriptions can be understood by both C++ and VB users, and those who
want or have to use raw interfaces should know enough about COM to know how
those wrappers should be interpreted in terms of raw interfaces.
HTH
Heinz