0

I have 3 interfaces: IGetInformation, IAccount, ICreateATM and they inherit IDispatch. Also i have class CoATM that inherits these 3 interfaces.

How should I implement IDispatch for all my interfaces to work? I have this implementation and only IGetInformation is working:

STDMETHODIMP CoATM::GetTypeInfoCount(UINT* pctinfo)
{
    *pctinfo = 1;
    return S_OK;
}
STDMETHODIMP CoATM::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
{
    HRESULT hr;

    *ppTInfo = 0;

    if (iTInfo)
        return DISP_E_BADINDEX;

    if (m_pTypeInfo)
    {
        m_pTypeInfo->AddRef();
        hr = S_OK;
    }
    else
        hr = LoadMyTypeInfo();
    if (SUCCEEDED(hr))
        *ppTInfo = m_pTypeInfo;
    return hr;

}
STDMETHODIMP CoATM::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
{
    if (!m_pTypeInfo)
    {
        HRESULT hr;
        if (FAILED(hr = LoadMyTypeInfo()))
            return hr;
    }

    return DispGetIDsOfNames(m_pTypeInfo, rgszNames, cNames, rgDispId);
}
STDMETHODIMP CoATM::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
    if (!IsEqualIID(riid, IID_NULL))
        return DISP_E_UNKNOWNINTERFACE;

    if (!m_pTypeInfo)
    {
        HRESULT hr;
        if (FAILED(hr = LoadMyTypeInfo()))
            return hr;
    }

    return DispInvoke(this, m_pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

}

HRESULT CoATM::LoadMyTypeInfo(void)
{
    HRESULT hr;
    LPTYPELIB pTypeLib;

    if (SUCCEEDED(hr = LoadRegTypeLib(CLSID_ATMTypeLib, 1, 0, 0, &pTypeLib)))
    {
        if (SUCCEEDED(hr = pTypeLib->GetTypeInfoOfGuid(IID_IGetInformation, &m_pTypeInfo)))
        {
            pTypeLib->Release();
            m_pTypeInfo->AddRef();
        }
        else if (SUCCEEDED(hr = pTypeLib->GetTypeInfoOfGuid(IID_IAccount, &m_pTypeInfo)))
        {
            pTypeLib->Release();
            m_pTypeInfo->AddRef();
        }
        else if (SUCCEEDED(hr = pTypeLib->GetTypeInfoOfGuid(IID_ICreateATM, &m_pTypeInfo)))
        {
            pTypeLib->Release();
            m_pTypeInfo->AddRef();
        }
    }

    return hr;
}

I think i only have IGetInformation working, because i first get it in LoadMyTypeInfo(). But i don't know how to get all my interfaces.

Mental
  • 1
  • 1
    Have you tried constructing your project with MSVC as a COM project? It does a lot of this plumbing automatically. If you want to do it yourself, maybe you can try it using MSVC, and see what it generates. Then pattern yours after that. (Although it is normally much simpler to use the tools in MSVC to do this stuff) – ttemple Oct 25 '20 at 11:58
  • Ok, thx for your advice. – Mental Oct 25 '20 at 12:04

0 Answers0