Solved

Simple COM/DCOM interface question (IUnknown)

Posted on 2004-10-17
5
708 Views
Last Modified: 2009-07-29
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!!
0
Comment
Question by:cmp2
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12335247
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.
0
 

Author Comment

by:cmp2
ID: 12339068
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12339797
>>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 {
};

0
 

Author Comment

by:cmp2
ID: 12342893
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.





0
 
LVL 86

Accepted Solution

by:
jkr earned 180 total points
ID: 12343584
Yes, that's the idea. Since inheriting from IUnknown ensures the the "virtual-function-compatibility" (gnaaa, what a terminus) , that can't happen.

>>If I have a valid pointer to some interface I should beable to cast it to a IUnknown interface to call
>>the Release() method

That's the beauty about that :o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now