Solved

Simple COM/DCOM interface question (IUnknown)

Posted on 2004-10-17
5
703 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

12 Experts available now in Live!

Get 1:1 Help Now