Convert C++ COM class to C# for invocation by C++ executable

1

Apologies but this is a C++ integration with C# question from a C# person!

I have a C++ EXE which invokes methods from an COM DLL which is registered as an InProcServer32 with a specific class ID.

The EXE is invoked thus:

EXENAME --UseClassId {CLSID}

The COM DLL is built in C++ at this moment and the core ".h" file looks like this:

class DECLSPEC_UUID("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX1}") ImplementationClass : public ComClass<IInterfaceMain>
{
public:
    IFACEMETHODIMP Method1(IInterfaceSub1 **param1) _NOEXCEPT override { return 1; }
    IFACEMETHODIMP Method2(IInterfaceSub1** param2) _NOEXCEPT override { return 1; }
};

CoCreatableClass(ImplementationClass)
}}}

The interface it uses is defined thus:

MIDL_INTERFACE("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX2")
    IInterfaceMain : public IUnknown
    {
    public:
        virtual HRESULT STDMETHODCALLTYPE Method1(IInterfaceSub1 **param1) = 0;        
        virtual HRESULT STDMETHODCALLTYPE Method2(IInterfaceSub2 **param2) = 0;                       
    };

... and the interfaces used by the above are defined in a similar way.

Now, I want to create this COM DLL in .Net instead. So the code currently looks like this:

[ComVisible(true), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX1"), ClassInterface(ClassInterfaceType.None)]
public class ImplementationClass : IInterfaceMain
{
    public ImplementationClass() {}

    public int Method1(IInterfaceSub1 param1) { return 1; }
    public int Method2(IInterfaceSub2 param2) { return 1; }
}

I have created the main interface in C# also, like this (but without any MIDL_INTERFACE / Guid):

public interface IInterfaceMain
{
    int Method1(IInterfaceSub1 param1);
    int Method2(IInterfaceSub2 param2);
}

Invoking the main EXE (the code of which isn't available) returns these two errors depending on the build architecture (top is "x64" bottom is "Any CPU").

0x80004002 No such interface supported
0x800401f9 Error in the DLL.

As I said, I'm not fluent with C++ but to my mind this should work.

c#
c++
interface
com
asked on Stack Overflow Feb 2, 2016 by James Harcourt • edited Feb 2, 2016 by James Harcourt

1 Answer

0

Courtesy of Hans Passant, the answer is to decorate the interfaces with the correct [ComVisible], [Guid] and [InterfaceType] attributes.

answered on Stack Overflow Feb 4, 2016 by James Harcourt

User contributions licensed under CC BY-SA 3.0