Link to home
Start Free TrialLog in
Avatar of cmp2
cmp2

asked on

Simple COM/DCOM interface question (IUnknown)

Hello,

What I understand that in COM programming all interfaces MUST inherit from IUnknown.  For example,
class IPatient : public IUnknown
{
};

Is this assumption true on ALL cases?  That is, do ALL COM compliant interfaces inherit from IUnknown?  The reason that I ask is because I am writing a basic COM management library and it doesn't make sense that I need to know the interface in order to call the Release method. For example is it always going to be safe to type cast an interface pointer to IUnknown to call the Release method? For example,

void* pInterface = GetInterfacePtr(CLSID,IID);
((IUnknown*)pInterface)->Release();

Thanks for the help!!
Avatar of jkr
jkr
Flag of Germany image

As a genreal rule: If your interfaces are exposed to more parties than just you and yourself, it is always a good idea to follow the design that MS gave for that situation. The penalty you'll pay for implementing IUnknown's methods (which is in fact neglectible) is lesser than the one you pay for not doing so in the long run.
Avatar of cmp2
cmp2

ASKER

Thanks for the response, however, not quite what I was after.  Of course you don't have to implement IUnknown's methods but if your COM object is to behave like a COM object at all you are going to have to implement the IUnknown's interface methods.  If you derive from IUnknown you inherit its interface but you still need to implement the interface.  Microsoft, and other literature, state that all Interfaces must inherit from IUnknown.  However, I am wondering if in the field this is the common practice...I mean I could conceivably implement AddRef(), Release(), and QueryInterface() without implementing the IUnknown interface. I was wondering how it is really done in the field versus how it "should" be done.  Basically, if I make the assumption that all COM interfaces derive from IUnknown and attempt to cast a void* interface pointer to IUnknown to call the Release method the OS isn't going to be happy if that assumption isn't correct.
>>However, I am wondering if in the field this is the common practice...I mean I could conceivably implement AddRef(),
>>Release(), and QueryInterface() without implementing the IUnknown interface

But that would not really make sense in a COM world. The idea is that IUnknown serves as the least common denominator on an *interface* level (and I'm talking C++ interfaces here) to ensure vtable compatibility.

Remember that when using

interface IQI {
public:
virtual HRESULT QueryInterface (...) = 0;
};

interface IRef {
public:
virtual void AddRef () = 0;
virtual void Release () = 0;
};

class ISomething : public IQI, public IRef {

//...

};

is NOT identical to

class ISomething: public IUnknown {
};

Avatar of cmp2

ASKER

Thanks for the response jkr,

So, in your professional opinion, I should never run into a problem with the following:

IMyInterface* pInterface = GetInterfacePtr(m_CLSID, myInterface_IID);

((IUnknown*)pInterface)->Release();

Since all COM objects must inherit the IUnknown interface (regardless of how they implement the IUnknown interface methods)?

The reason, obviously, is because I don't want or care to know the actual interface that the CLSID and IID refer to.  If I have a valid pointer to some interface I should beable to cast it to a IUnknown interface to call the Release() method.  Since it is overidden (throught vtable magic :) )  the appropriate functionalty to decrement the reference count on the object should be performed.





ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial