Solved

Simple COM/DCOM interface question (IUnknown)

Posted on 2004-10-17
5
713 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to properly read C++ console input into several variables with scanf 2 29
How do i run a c++ file? 15 51
Outlook 13 79
Finding Divisors 5 35
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

713 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