Click here to Skip to main content
15,879,239 members
Articles / Desktop Programming / ATL

COM: IEnumXXXX to STL-style iterator wrapper class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Feb 2000CPOL 58.1K   922   37  
A simplified method to enumerate a collection of objects.
// AssociateObjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TemplateMFC.h"
#include "AssociateObjectDlg.h"
#include "ComCatInformation.hpp"
#include "ComCatRegister.hpp"
#include "ComGUID.hpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using JetByteTools::CComCatInformation;
using JetByteTools::CComCatRegister;
using JetByteTools::CWin32Exception;
using JetByteTools::CComGUID;

/////////////////////////////////////////////////////////////////////////////
// CAssociateObjectDlg dialog


CAssociateObjectDlg::CAssociateObjectDlg(
	JetByteTools::CGUIDRegistryList &list, 
	ULONG cImplemented, 
	CATID *pCatidImpl,
	ULONG cRequired,
	CATID *pCatidReq,
	CWnd* pParent /*=NULL*/)
	:	CDialog(CAssociateObjectDlg::IDD, pParent),
		m_list(list),
		m_objectsList(list.Begin(), list.End()),
		m_cImplemented(cImplemented),
		m_pCatidImpl(pCatidImpl),
		m_cRequired(cRequired),
		m_pCatidReq(pCatidReq)
{
	//{{AFX_DATA_INIT(CAssociateObjectDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CAssociateObjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAssociateObjectDlg)
	DDX_Control(pDX, IDOK, m_OKButton);
	DDX_Control(pDX, IDC_OBJECTS_LIST, m_objectsList);
	DDX_Control(pDX, IDC_REQUIRE_LIST, m_requireList);
	DDX_Control(pDX, IDC_IMPLEMENT_LIST, m_implementList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAssociateObjectDlg, CDialog)
	//{{AFX_MSG_MAP(CAssociateObjectDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAssociateObjectDlg message handlers

BOOL CAssociateObjectDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Associate the OK button with the list of objects, it will remain 
	// grayed out until an item is highlighted in the list...

	m_objectsList.AssociateButton(
      m_OKButton);

	FillBox(m_implementList, m_cImplemented, m_pCatidImpl);

	FillBox(m_requireList, m_cRequired, m_pCatidReq);


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CAssociateObjectDlg::FillBox(
	CListBox &box, 
	ULONG cCatids, 
	const CATID *pCatids)
{
	try
	{
		CComCatInformation catMgr;

		for (UINT i = 0; i < cCatids; i++)
		{
			LPCTSTR lpCatDesc = catMgr.GetCategoryDesc(pCatids[i]);

			box.AddString(lpCatDesc);

			free((void*)lpCatDesc);
		}
	}
	catch (CWin32Exception &e)
	{
		e.MessageBox(m_hWnd);
	}
}

void CAssociateObjectDlg::OnOK() 
{
	// Need to get the GUID and description of the selected object...

	UINT selectedItem = m_objectsList.GetCurSel();

	if (LB_ERR != selectedItem)
	{
		// Now make it so...

		CComGUID *pGuid = m_objectsList.GetItemData(selectedItem);

		if (pGuid)
		{
			try
			{
				CComCatRegister catMgr;

				if (m_cImplemented != 0)
				{
					catMgr.RegisterClassImplCategories(*pGuid, (GUID*)m_pCatidImpl, m_cImplemented);
				}

				if (m_cRequired != 0)
				{
					catMgr.RegisterClassReqCategories(*pGuid, (GUID*)m_pCatidReq, m_cRequired);
				}

				MessageBox(
					_T("Object: ") + CString(pGuid->GetDescription()) + _T(" - successfully associated with categories"),
					_T("Done"),
					MB_ICONINFORMATION);
			}
			catch(CWin32Exception &e)
			{
				e.MessageBox(m_hWnd);
			}
		}
	}

	CDialog::OnOK();
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) JetByte Limited
United Kingdom United Kingdom
Len has been programming for over 30 years, having first started with a Sinclair ZX-80. Now he runs his own consulting company, JetByte Limited and has a technical blog here.

JetByte provides contract programming and consultancy services. We can provide experience in COM, Corba, C++, Windows NT and UNIX. Our speciality is the design and implementation of systems but we are happy to work with you throughout the entire project life-cycle. We are happy to quote for fixed price work, or, if required, can work for an hourly rate.

We are based in London, England, but, thanks to the Internet, we can work 'virtually' anywhere...

Please note that many of the articles here may have updated code available on Len's blog and that the IOCP socket server framework is also available in a licensed, much improved and fully supported version, see here for details.

Comments and Discussions