views:

81

answers:

3

I'm trying to embed a browser control in my application (IWebBrowser2). I need to implement IDispatch, IDocHostShowUI, IDocHostUIHandler etc to make this work. I am doing this in pure C++/Win32 api. I'm not using ATL, MFC or any other framework.

I have a main class, called TWebf, that creates a Win32 window to put the browser control in and makes all the OLE calls needed to make it work. It's also used for controlling the browser control, with methods like Refresh(), Back(), Forward() etc.

Right now this is implemented with composition. TWebf has classes implementing all the different interfaces (IDispatch, IDocHostShowUI...) as (stack allocated) members. First thing TWebf does in its constructor is give all those members a pointer back to itself (dispatch.webf = this; etc). QueryInterface, AddRef and Release are implemented as calls to those methods in TWebf for all interface implementations (by calling return webf->QueryInterface(riid, ppv); for example)

I don't like this circular dependency between TWebf and the classes implementing the interfaces. TWebf has a TDispatch member that has a TWebf member that has a...

So I was thinking about solving this with multiple inheritance instead. That would also simplify QueryInterface to always be able to just return this.

A UMLish sketch of what I want would be something like this: (Click for a bigger view)

As can be seen in the uml I want to provide bare-minimum implementations of all the interfaces so I only have to override those methods in the interfaces I actually want to do something substantial in TWebf.

Is my "multiple inheritance implementation" possible? Is it a good idea? Is it the best solution?

EDIT:

For future discussion, here's the current implementation of QueryInterface in TWebf

HRESULT STDMETHODCALLTYPE TWebf::QueryInterface(REFIID riid, void **ppv)
{
    *ppv = NULL;

    if (riid == IID_IUnknown) {
        *ppv = this;
    } else if (riid == IID_IOleClientSite) {
        *ppv = &clientsite;
    } else if (riid == IID_IOleWindow || riid == IID_IOleInPlaceSite) {
        *ppv = &site;
    } else if (riid == IID_IOleInPlaceUIWindow || riid == IID_IOleInPlaceFrame) {
        *ppv = &frame;
    } else if (riid == IID_IDispatch) {
        *ppv = &dispatch;
    } else if (riid == IID_IDocHostUIHandler) {
        *ppv = &uihandler;
    }

    if (*ppv != NULL) {
        AddRef();
        return S_OK;
    }

    return E_NOINTERFACE;
}

EDIT 2:

I tried implementing this for just a couple of the interfaces. Having TWebf inherit from IUnknown and TOleClientSite seems to work fine, but when I added TDispatch to the inheritance list it stopped working.

Apart from the warning C4584: 'TWebf' : base-class 'IUnknown' is already a base-class of 'TDispatch' warning I also get runtime errors. The runtime error is "Access violation reading location 0x00000000"

The runtime error happens on a line dealing with IOleClientSite, not IDispatch for some reason. I don't know why this is happening, or if it really has to do with the multiple inheritance or not. Any clues anyone?

EDIT 3:

A bad implementation of QueryInterface seems to have been the reason for the runtime exception. As Mark Ransom correctly noted the this pointer needs to be casted before it's assigned to *ppv, and special care is needed when IUnknown is requested. Read Why exactly do I need an explicit upcast when implementing QueryInterface in an object with multiple inheritance for an excellent explanation of that.

Why exactly I got that specific runtime error I still do not know.

+2  A: 

Multiple inheritance is a very common way to do COM interfaces, so yes it's possible.

However QueryInterface must still cast the pointer for each interface. One interesting property of multiple inheritance is that the pointer may get adjusted for each class type - a pointer to IDispatch won't have the same value as a pointer to IDocHostUIHandler, even though they both point to the same object. Also make sure that a QueryInterface for IUnknown always returns the same pointer; since all the interfaces derive from IUnknown you'll get an ambiguous cast if you try just to cast directly to it, but that also means you can use any interface as an IUnknown, just pick the first one in the parent list.

Mark Ransom
All the interfaces have to provide the `IUnknown` functions, (else how would you QI a different interface?) so there's multiple copies of `IUnknown` (virtual can't be used, see linked questions). But all the `IUnknown` implementations have to share a single count. So you end up with elements of composition even when you have a design based on inheritance.
Ben Voigt
@Mark Ransom: Yes, I've seen it done before, but then the class corresponding to TWebf in my example always inherit directly from COM interfaces. Is it also okay to have intermediate implementation classes like TDispatch for example, like I have?
Tobbe
@Tobbe, I don't think there's any problem with the intermediate classes. It's been a few years since I've done this though, I might be forgetting something.
Mark Ransom
@Ben Voigt, Microsoft declares that multiple inheritance allows the IUnknown interfaces to share the same implementation (but I forget the mechanical details): http://msdn.microsoft.com/en-us/library/ms680509(VS.85).aspx
Mark Ransom
A: 

Multiple inheritance has a couple of limitations

  1. If two interfaces ask for an implementation of a function with the same name/signiture its impossible to provide two different behaviors using multiple inheritance. In some cases you want the same implementation, but in others you don't.

  2. There will be multiple IUnknown interfaces on the virtual table of your class which can add up to extra memory usage. They do share the same implementations which is nice.

MerickOWA
The vtables are only one per class, not one per object, so the extra memory usage is negligible.
Mark Ransom
1. I think the only functions with the same name/signature are the IUnknown functions, and they can share a single implementation, so I think that's ok.2. Memory usage is not a very big concern in this case.
Tobbe
A: 

It would be substantially easier to remain with composition. MI has many pitfalls, like virtual inheritance, and suffers greatly from maintainability. If you have to pass this in to the composed classes as a data member, you've done it wrong. What you should do is pass this in on method calls if they need to access the other provided methods. Since you control all method calls to the composed object, it should be no problem to insert the extra pointer. This makes life MUCH, MUCH easier for maintenance and other operations.

DeadMG
I don't see how that would work. Can you please provide an example of how the composed classes would call TWebf's QueryInterface for example when their own QI is called. I have no control over the call to their QI, it's some other COM class that calls QI, and it doesn't provide the "this" pointer needed.If I could remove the circular dependency and keep the composition that would be a solution I'm happy with too.
Tobbe
I'm not sure that "composition" is the right term here. COM seems to use the word "aggregation" for all the sibling interfaces of an object delegating the `IUnknown` implementation. http://msdn.microsoft.com/en-us/library/ms686558.aspx
Ben Voigt
@Tobbe: You didn't mention that you didn't have control over the classes you were inheriting from.
DeadMG