Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / ATL

Building Rich COMponents with Attributed ATL

Rate me:
Please Sign up or sign in to vote.
4.95/5 (31 votes)
26 Jan 200410 min read 88.7K   918   76  
Tutorial article about how to build components with rich functionality using attributed ATL.
// smServicesEnum.cpp : Implementation of CsmServicesEnum

#include "stdafx.h"
#include "smServicesEnum.h"

#include "smService.h"

// CsmServicesEnum

STDMETHODIMP CsmServicesEnum::Next(unsigned long celt, VARIANT *rgvar, unsigned long *pceltFetched)
{
    if (pceltFetched)
        *pceltFetched = 0;

    if (!rgvar)
        return E_INVALIDARG;

    for (int i = 0; i < celt; i++)
        VariantInit(&rgvar[i]);

    unsigned long fetched = 0;
    while (m_Idx < m_Services.size() && fetched < celt)
    {
        rgvar[fetched].vt = VT_DISPATCH;

        //  Create service
        CService *pService;
        CService::CreateInstance(&pService);

        pService->AddRef();

        pService->Init(m_Services[m_Idx]);
        HRESULT hr = pService->QueryInterface(&rgvar[fetched].pdispVal);
        
        pService->Release();

        if (FAILED(hr))
            break;

        m_Idx++;
        fetched++;
    }

    if (pceltFetched)
        *pceltFetched = fetched;

    return (celt == fetched) ? S_OK : S_FALSE;
}

STDMETHODIMP CsmServicesEnum::Skip(unsigned long celt)
{
    unsigned long i = 0;
    while (m_Idx < m_Services.size() && i < celt)
    {
        m_Idx++;
        i++;
    }

    return (celt == i) ? S_OK : S_FALSE;
}

STDMETHODIMP CsmServicesEnum::Reset()
{
    m_Idx = 0;

    return S_OK;
}

STDMETHODIMP CsmServicesEnum::Clone(IEnumVARIANT **ppenum)
{
    CServicesEnum *pEnum;
    CServicesEnum::CreateInstance(&pEnum);

    pEnum->AddRef();

    pEnum->CloneServices(&m_Services);
    HRESULT hr = pEnum->QueryInterface(ppenum);

    pEnum->Release();

    return hr;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Started professional career in software development back in 2000, in Ukraine. Founder and owner of a boutique software company called ByteGems.com Software. Worked for 6 years at w2bi, Inc in New Jersey USA, currently work in a large multinational company based in Redmond, WA.

My buzzwords at the moment: .NET, C#, ASP.NET, MVC, LINQ, TypeScript, JavaScript, AngularJS, HTML, JSON, services.

Still buzzing: C++, Win32, ATL, MFC, SQL, WinForms, WebForms, EF, Sockets, TCP/IP, Remoting.

Comments and Discussions