C# & COM, exploring COM interfaces

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I just discovered a COM interface which provide some functionality I need.
I know nothing of COM and read the documentation and will apply a slow trial
and error process.

In the documentation I see that before calling into COM you should call
CoInitialize.
And that you shoudl also call CoUnitialize at the end of the app.

Isn't .NET using COM already? (after all the .NET JIT has a COM interface),
do I need to call it?
And how do I ensure CoUnitialize is called at the end of the program?

Beside auto generated code form COM interface don't seems to use it, do I
realy have to use it?
 
Lloyd,

Has the COM component been registered on the machine? If not, try this from
the command-line: regsvr32.exe ComponentName.dll
 
Isn't .NET using COM already? (after all the .NET JIT has a COM interface),
do I need to call it?
And how do I ensure CoUnitialize is called at the end of the program?

Yes it is, and no you don't have to call CoInitializeEx or CoUninitialize
explicitly.


Mattias
 
it's a standart Microsoft component, mlang comes with the paltform SDK!
anyway forget about C# COM generator, I just include the header in my
managed C++ class and it worked.
 
Be warned, COM is not easy, and using COM via COM Interop may look easy, but
it is often even more difficult. It just works is often a much better
solution.

As you mentioned, you have to initialize COM on every thread that wants to
use it. If you are working with Whidbey, your observation that .NET is using
COM already, is true, too. In this case, you do not need to care about
calling CoUninitialize.

Just in case you have to dig deeper into COM: COM can be initialized in
different ways and unless specified otherwise, .NET 2.0 initializes it's
threads into the "Multi Threading apartment".

Marcus Heege
www.heege.net
 
Back
Top