Click here to Skip to main content
15,893,564 members
Articles / Desktop Programming / ATL

Pluggable Components using Component Categories Part I

Rate me:
Please Sign up or sign in to vote.
4.96/5 (17 votes)
18 Sep 20034 min read 68.4K   1.1K   56  
An article on using component categories to create pluggable components
// ConfigDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ComCatApp.h"
#include "ConfigDlg.h"

#include "initguid.h"
#include "ComCatDrawCategory.h"

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

/////////////////////////////////////////////////////////////////////////////
// CConfigDlg dialog


CConfigDlg::CConfigDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConfigDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConfigDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_clsid = CLSID_NULL;
}


void CConfigDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConfigDlg)
	DDX_Control(pDX, IDC_COMPONENT_LIST, m_list);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CConfigDlg message handlers

BOOL getFriendlyName(const CLSID& clsid, LPCTSTR szFriendlyName, int iLength)
{
	HKEY hKey;
	char szKeyBuf[1024] ;
//	char szCLSID[39] ;
	BSTR bCLSID;

	// Convert the clsid to a string.
//	CLSIDtochar(clsid, szCLSID, 39) ;
	StringFromCLSID(clsid, &bCLSID);

	CString strCLSID(bCLSID);

	// Make the key.
	sprintf(szKeyBuf, "CLSID\\%s", strCLSID) ;

	// Create and open key and subkey.
	long lResult = RegOpenKeyEx(	HKEY_CLASSES_ROOT ,
									szKeyBuf, 
									0, 
									KEY_ALL_ACCESS, 
									&hKey) ;
	if (lResult != ERROR_SUCCESS)
	{
		return FALSE ;
	}

	// Set the Value.
	ASSERT(szFriendlyName != NULL) ;
	DWORD dwSize = iLength ;
	lResult = RegQueryValueEx( hKey, NULL, NULL, NULL, (BYTE *)szFriendlyName, &dwSize);

	RegCloseKey(hKey);

	return lResult == ERROR_SUCCESS;
}

BOOL CConfigDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CComPtr<ICatInformation> pInfo;
	if (FAILED(CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatInformation, (void**)&pInfo)))
		return FALSE;

	int cIDs = 1;
	CATID IDs[1];
	IDs[0] = CATID_ComCatDrawCategory;

	CComPtr<IEnumCLSID> pEnumCLSID = NULL;
	if (FAILED(pInfo->EnumClassesOfCategories(cIDs, IDs, 0, NULL, &pEnumCLSID)))
		return FALSE;

	char szFriendlyName[128] ;
	CLSID clsid ;

	while (pEnumCLSID->Next(1, &clsid, NULL) == S_OK)
	{
		if (getFriendlyName(clsid, szFriendlyName, sizeof(szFriendlyName)))
		{
			int index = m_list.AddString(szFriendlyName) ;

			CLSID* pclsid = new CLSID ;
			*pclsid = clsid ;

			m_list.SetItemDataPtr(index, pclsid) ;
		}
	}
	
	if (m_list.GetCount() > 0)
		m_list.SetCurSel(0) ;
		
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CConfigDlg::OnOK() 
{
	if (m_list.GetCount() != LB_ERR)
	{
		int index = m_list.GetCurSel() ;
		if (index == LB_ERR) 
			index = 0 ;
		
		m_clsid = *(reinterpret_cast<CLSID*>(m_list.GetItemDataPtr(index))) ;
	}
	else
		m_clsid = CLSID_NULL ;
		
		
	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 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
Web Developer
United States United States
I started programming at 15 with a TI-82 calclator in Z80 assembly (oh, those were the days . . .) I am pretty much a self taught programmer. I've taught myself Visual Basic, C/C++, Java, and am currently working on C#. I also like to experiment with system administration and security issues, and occassionally I work on web design. For the last 4 years, I have worked for Leitch, Inc. as a Software Engineer and graduated from Old Dominion University with bachelor's degrees in Computer Science, Mathematics, and Business Management in December of 2004.

Comments and Discussions