Solved

Simple COM/DCOM interface question (IUnknown)

Posted on 2004-10-17
5
715 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

615 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