Search code examples
c++comidl

COM: how to specify a specific type of COM object as an argument in idl


Currently I have some code that looks like this

void calc_run(Calculation *c, Input *i);

STDMETHODIMP CCalculation::run(IUnknown* input)
{
    calc_run(calc,((CMyInputClass*)input)->get_input());  
    return S_OK;
}

In other words CCalculation::run wants a pointer to a CMyInputClass, but currently it takes IUnknown and downcasts.

Presumably this is bad.

But how can I specify more precisely to COM which object I want? I tried changing the .c, .h and .idl files but the compiler doesn't recognise CMyInputClass* as a type specification in the idl.

interface ICalculation : IDispatch{
[id(2), helpstring("method run")] HRESULT run([in] CMyInputClass* input);

What is the correct way to do this?


Solution

  • The COM way would be to introduce a COM interface that CMyInputClass would implement and use that interface in run() declaration:

    interface ICalculationInput : IUnknown {
         //some methods here
    };
    
    interface ICalculation : IDispatch{
         [id(2), helpstring("method run")] HRESULT run([in] ICalculationInput* input);
    };