How Do I Add Custom Item Types to the Snap-In Object?

By default, the ATL Object Wizard creates a single data class, derived from CSnapInItemImpl, for the snap-in object.

In some cases, you need to implement one or more custom item types for a snap-in object. The simplest approach is to copy and modify a new class from the existing data class and make some additional modifications to the project. This procedure is accomplished in three steps:

Creating a Custom Item Data Class

The first step involves copying the declaration of the existing data class for the snap-in object and then modifying the results to create a separate class.

The data class declaration can be found at the beginning of the PROJNAME.H file of your snap-in project. Locate this class declaration, and copy and paste the result below the first declaration.

In the newly pasted declaration, replace any occurrences of the first class name with the name of the new second class. For instance, if your project data class is called CMySnapInData and you want to create a custom data item class called CMyCustomSnapInData, first copy and paste the declaration of the initial data class. Then search for any occurrences of CMySnapInData (in the pasted section) and replace it with CMyCustomSnapInData.

Implementing the GUIDs for the Custom Data Class

In addition to the class declaration, the data class has a GUID declaration and four static data members. The GUID and data members are defined at the end of the PROJNAME.CPP file. The following is an example declaration:

static const GUID CProjNameGUID_NODETYPE = 
{ 0xb386c6b, 0x8633, 0x11d1, { 0xb0, 0x8, 0x0, 0xc0, 0x4f, 0xb9, 0x94, 0x36 } };
const GUID*  CProjNameData::m_NODETYPE = &CsdfdsfGUID_NODETYPE;
const TCHAR* CProjNameData::m_SZNODETYPE = _T("0B386C6B-8633-11D1-B008-00C04FB99436");
const TCHAR* CProjNameData::m_SZDISPLAY_NAME = _T("Csdfdsf");
const CLSID* CProjNameData::m_SNAPIN_CLASSID = &CLSID_sdfdsf;

Because these data members depend upon a GUID, you need to first generate a new GUID (using GUIDGEN) and then copy and paste the five declarations, of the initial data class, directly below. After you create a copy, modify it to use the newly created GUID. For example, using the preceding code sample and generating a new GUID, the result for the second data class would be as follows:

static const GUID CMyCustomSnapInGUID_NODETYPE = 
    { 0x8dbeb252, 0x9201, 0x11d1, { 0xa8, 0xd6, 0x0, 0x60, 0x8, 0x93,
        0x8f, 0xb8 } };
const GUID*  CMyCustomSnapIn::m_NODETYPE = &CMySnapInGUID_NODETYPE;
const TCHAR* CMyCustomSnapIn::m_SZNODETYPE =
    _T("8DBEB252-9201-11D1-A8D6-006008938FB8");
const TCHAR* CMyCustomSnapIn::m_SZDISPLAY_NAME = _T("CMyCustomSnapIn");
const CLSID* CMyCustomSnapIn::m_SNAPIN_CLASSID = &CLSID_MyCustomSnapIn;

After these modifications are complete, there is one step left: modifying the registry file for the snap-in object.

Modifying the .RGS File of the Snap-In Project

The purpose of the .RGS file is to register the snap-in object and all its components. Because you have added a custom data item class, you need to modify the .RGS file to register this class too.

Note   The GUID you generated in the previous step is used in this step.

There are two areas in the .RGS file you need to modify. Both can be found quickly by searching for the NodeTypes keyword.

The first occurrence is under the NoRemove Snapins section. In the nested NodeTypes section, add the new GUID immediately after the existing one, using the same GUID format as the existing one.

The second occurrence is in the NoRemove NodeTypes section. Copy the code found in this section, paste it in immediately after, and change the referenced GUID to the GUID of the custom class.

This completes the modifications of the snap-in project. After a successful compile, you will have a new custom item type.