How to create IReference<UINT32> windows COM interface

548 views Asked by At

I am facing a MSVC COM interface creation issue: The COM API requires __FIReference_1_UINT32 type arguments which is also the IReference type.

virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_Value(
/* [out][retval] */ __RPC__deref_out_opt __FIReference_1_UINT32 **value) = 0;           
virtual /* [propput] */ HRESULT STDMETHODCALLTYPE put_Value(
/* [in] */ __RPC__in_opt __FIReference_1_UINT32 *value) = 0;

And I tried RoActivateInstance function. But it gets non register class error.

ComPtr<__FIReference_1_UINT32_t> fValue;
const wchar_t str[] = __FIReference_1_UINT32_t::z_get_rc_name_impl();//L"Windows.Foundation.IReference1<UInt32>";
hr = RoActivateInstance(HString::MakeReference(str).Get(), &fValue);

Really appreciated if someone could give me some hints.

1

There are 1 answers

0
Glen On

I just had a similar problem and did a bit of digging. I found that, as commented, the C++/CX language extension implements IBox to support reference types, its implementation appears to be entirely within the winmd lib file that is linked in so it not available to ISO c++. I was able to get my case to work by creating a basic version a com object impl of IReference by reusing some helper classes I already had. It returns E_NOTIMPL for the IInspectable methods and uses 'stack' based com object semantics, which should be ok for simple setters where i wouldnt expect com references to the holder object to be kept.

// variadic template QueryInterface impl
template <typename T, typename I, typename... Rest>
inline HRESULT Qi(T * t, const IID& riid, void** ppvObject)
{
    if (riid == __uuidof(I))
    {
        I * i = static_cast<I*>(t);
        i->AddRef();
        *ppvObject = i;
        return S_OK;
    }
    return Qi<T, Rest...>(t, riid, ppvObject);
}

// variadic terminator
template <typename T>
inline HRESULT Qi(T *, const IID&, void** ppvObject)
{
    *ppvObject = nullptr;
    return E_NOINTERFACE;
}

// com object with 'stack' based semantics
template <typename I1, typename... I2>
class __declspec(novtable) StackObject : public I1, public I2...
{
    typedef StackObject Self;
protected:
    virtual ~StackObject() {}

public:
    HRESULT STDMETHODCALLTYPE QueryInterface(const IID& riid, void** ppvObject) override
    {
        if (!ppvObject)
        {
            return E_POINTER;
        }
        if (riid == __uuidof(IUnknown) || riid == __uuidof(I1))
        {
            *ppvObject = static_cast<IUnknown*>(static_cast<I1*>(this));
            return S_OK;
        }
        return Qi<Self, I2...>(this, riid, ppvObject);
    }

    ULONG STDMETHODCALLTYPE AddRef() override
    {
        return 2;
    }

    ULONG STDMETHODCALLTYPE Release() override
    {
        return 1;
    }
};

// Reference impl
template <typename T>
class Reference sealed : public StackObject<ABI::Windows::Foundation::IReference<T>>
{
    T val;

public:
    Reference() : val {}
    {}

    Reference(T value) : val(value)
    {}

private:
    HRESULT STDMETHODCALLTYPE GetIids(ULONG *iidCount, IID **iids) override
    {
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE GetRuntimeClassName(HSTRING *className) override
    {
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE GetTrustLevel(TrustLevel *trustLevel) override
    {
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE get_Value(T *value) override
    {
        *value = val;
        return S_OK;
    }
};

then use as...

Reference<UINT32> value(666);
CheckHr(object->put_Value(&value));