COM coobjects and interfaces C#

150 views Asked by At

I am completely new to this and am following a tutorial on msdn: http://msdn.microsoft.com/en-us/library/aa645736(v=vs.71).aspx#vcwlkcominteroppart1cclienttutorialanchor2

My question is why do we use COM Interfaces AND COM CoClasses? It seems odd to instantiate an instance of a COM CoClass, instantiate a new object that is the old object but casted as the COM Interface.

I really am just wanting to grasp the ideology here...any help is useful.

2

There are 2 answers

0
dkackman On BEST ANSWER

The theory is to seprate the interface (what an object can do) from the implementation (how it does it or more specifically what specific code is instantiated to do it).

Defining types only as interfaces allows COM to specifiy how objects across multiple languages inter-operate; this being one of its main goals. Since the interface specification is a contract for how to pass data and invoke methods any language can expose a COM object without clients needing to know about the inner workings of the implementor (the CoClass).

This allows VB6, .net, C++, deplhi and any number of other languages to interoperate within the same application.

The wikipedia article on COM has some good background in this regard.

0
Russel Yang On

This is a basic design principle in software engineering, interface is a contract, in com world, coClass is a implementation. for COM, the interface can be defined in IDL, but implementation (coClass) can be done in VB, C++, .net etc.