Click here to Skip to main content
15,947,472 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I am creating an out-of-process COM server to host a number of 32-bit DLLs so that they can be accessed by a 64-bit process through 'wrapper interfaces'. Each of these wrapper interfaces implements the IDispatch interface as well as a number of 'wrapper methods' that access the 32-bit DLLs.

Because the implementations of the IUnknown and IDispatch methods are nearly identical in each wrapper interface, I thought I could save some time by implementing these methods in a base class and having each of the wrapper interfaces inherit these implementations.

The way I have structured my code, the base interface inherits IDispatch, and the base class inherits the base interface. The child interface inherits nothing, and the child class inherits the child interface and the base class.

According to Microsoft, "If you are using C++ multiple inheritance to implement multiple interfaces, the various interfaces can share one implementation of IUnknown."

It will be some time before I can really test this out. Has anyone tried something like this before? Is it possible for COM objects to inherit implementations of common interfaces?

Thanks in advance for your comments or suggestions.



My code looks something like this:

MIDL
//Filename: Base.idl

import "oaidl.idl";
import "ocidl.idl";
import "unknwn.idl";
[
    object,
    uuid(...),
    dual,
    oleautomation
]
interface IBase : IDispatch
{ }
[ uuid(...) ]
library BaseLib
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");
    interface IBase;

    [ uuid(...) ]
    coclass Base
    {
        [default] interface IBase;
    }
}




C++
************************
//Filename: Base.h

#include "Base_h.h"

class Base : public IBase
{
public:

    //Construction
    Base();
    ~Base();

    //IUnknown implementation
    HRESULT STDMETHODCALLTYPE QueryInterface(...);
    ULONG   STDMETHODCALLTYPE AddRef();
    ULONG   STDMETHODCALLTYPE Release();

    //IDispatch implementation
    HRESULT STDMETHODCALLTYPE GetTypeInfoCount(...);
    HRESULT STDMETHODCALLTYPE GetTypeInfo(...);
    HRESULT STDMETHODCALLTYPE GetIDsOfNames(...);
    HRESULT STDMETHODCALLTYPE Invoke(...);

    //Helper methods

protected:
    
    /* Some virtual helper methods here... */

    long m_RefCount;
};


MIDL
*********************
//Filename: Child.idl

import "oaidl.idl";
import "ocidl.idl";
[
    object,
    uuid(...),
    dual,
    oleautomation
]
interface IChild
{ // Child interface methods }

[ uuid(...)]
library ChildLib
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");
    interface IChild;
    [ uuid(...) ]
    coclass Child
    {
        [default] interface IChild;
    }
}


C++
*******************
//Filename: Child.h

#include "Base.h"
#include "IChild_h.h"

class Child : public IChild, public Base
{
public:
    //Construction
    Child();
    ~Child();

    //Child Interface Methods
    // ....

protected:

    /* Helper method implementations ... */

};
Posted

You should be good, this is what a lot of the ATL base/template classes do. Example, IDispatchImpl provides an IDispatch implementation base.
 
Share this answer
 
Comments
Espen Harlinn 24-Feb-11 18:04pm    
Right, my 5 - he might as well use ATL and the services it provides - process lifetime is often an issue that people have trouble with, or forgets, when they attempt to roll their own (D)COM framework. If he doesn't have a nested object hierarchy ATL allows him to implment his server without thinking about it - and if he does, ATL has the required mechanisms to deal with it :)
Sergey Alexandrovich Kryukov 24-Feb-11 18:41pm    
Correct, my 5.
Yet another developer hit this problem.
--SA
Merlin5p0 25-Feb-11 11:28am    
Thanks, 5. It's good to know I'm heading in the right direction!
Nish Nishant 25-Feb-11 11:31am    
You are welcome.
Just use ATL[^] - If I understood your question correctly - it does what you want to do.

Having said that - I'd just drop the whole DCOM idea - and go for a mixed mode C++/CLI wrapper aound your 32-bit dll's and expose their services through a 32-bit self hosting WCF service.

I've provided a number of links that may help you in another answer:
How can I deploy web service?[^]

This design will probably be much simpler to implement :)

Regards
Espen Harlinn
 
Share this answer
 
Comments
Nish Nishant 24-Feb-11 17:59pm    
Voted 5 for the WCF idea.

If he's really daring, he can skip the C++/CLI wrapper and expose the C++ code via a WWS service (which can be consumed by WCF clients) :-)
Espen Harlinn 24-Feb-11 18:05pm    
Good idea - I'd like to give you another 5 :)
Sergey Alexandrovich Kryukov 24-Feb-11 18:45pm    
Great, 5.
--SA
The problem itself is not so easy. (I actually used my own solution, for the sake of portability to non-Windows systems (yes, COM compatibility for Unix)). The real reason is how multiple inheritance is designed in C++.
You no that this problem does not exist in Delphi and .NET, but those are system with weak form of multiple inheritance.
--SA
Espen Harlinn 24-Feb-11 18:54pm    
The way ATL uses multiple inheritance and templates is rather neat - one of the really neat features of deriving from templates is that the derived class can be a template argument to its ancestor :)
Merlin5p0 25-Feb-11 11:39am    
Some great suggestions here... 5+. I'm thinking ATL might be my best shot on this one, but a WCF service might also be another route to explore. Thanks for the links.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900