Working with IComponentData

The section discusses how to implement the IComponentData interface.

Cocreating an IComponentData Object

A snap-in is an in-process server DLL, and the instance of MMC that loads it is its client. For a discussion of how in-process servers are loaded into a caller's process space, refer to the Microsoft Developer Network (MSDN) Library. For a discussion of registering snap-ins, see Registering and Unregistering a Snap-in.

For this example, assume that the CreateInstance method of the snap-in's IClassFactory implementation is called and that the snap-in creates an instance of the IComponentData interface in response. As a result, the console now has a pointer to the snap-in's IComponentData implementation.

Here is the constructor for the IComponentData implementation, CComponentData:

CComponentData::CComponentData()
: m_cref(0), m_ipConsoleNameSpace(NULL), m_ipConsole(NULL)
{
    OBJECT_CREATED
    m_pStaticNode = new CStaticNode;
}

In the constructor, the CComponentData object's reference count is initialized to zero. CComponentData caches pointers to the console's IConsoleNameSpace and IConsole interfaces; these pointers are set during the initialization of CComponentData in its Initialize method.

OBJECT_CREATED is a macro that is defined as follows:

#define OBJECT_CREATED InterlockedIncrement((long *)&g_uObjects);

The g_uObjects global variable keeps a global count of the number of snap-in-provided interfaces that MMC is holding at any moment during the lifetime of the snap-in instance.

The constructor also creates an instance of the CStaticNode object.

CStaticNode inherits from CDelegationBase.

Initializing an IComponentData Object

MMC initializes a snap-in by calling the Initialize method of the IComponentData object. During initialization, IComponentData should query the console for its IConsoleNamespace and IConsole interfaces using the console's IUnknown interface pointer that is passed into the call to Initialize. The snap-in should then cache the returned pointers and use them for calling IConsoleNamespace and IConsole interface methods.

Initialization is also the correct time for a snap-in to add a strip of icons to the image list using the IImageList::ImageListSetStrip method.

Creating an IComponent Object

When the CreateComponent method of the IComponentData object is called, the snap-in can create an instance of the IComponent interface. Typically, the newly created IComponent object caches a pointer to its parent IComponentData object in its constructor.

Be aware that MMC calls CreateComponent each time a new view (a new MDI child window) is created; thus each view is associated with a different IComponent object.

Sending Notifications to the IComponentData Object

The console calls the Notify method to notify the snap-in of an event that occurs, for example, when the user clicks the mouse.

Querying the IComponentData Object for a Data Object

MMC calls the snap-in's implementation of the QueryDataObject method to request a data object for a specific cookie, including the static folder (NULL cookie). The data object type is taken from the DATA_OBJECT_TYPES enumeration and determines the context in which MMC requests the data object. A CCT_SCOPE flag is set to indicate that the data object is for the scope pane. CCT_RESULT is set to indicate that it is for the result pane, and CCT_SNAPIN_MANAGER indicates that the Add/Remove Snap-in dialog box is being used. CCT_UNINITIALIZED indicates that the data object has an invalid type. Given context information, the snap-in can determine the context in which a data object is requested.

As noted earlier, IConsole2 is the snap-in's interface to the console, and objects that expose the IDataObject interface are used to pass context information from the snap-in to the console or between snap-ins.

Getting Display Information

The GetDisplayInfo method retrieves display information for a namespace item in the scope pane.

Destroying the Object

The Destroy method releases all interfaces to the console, such as IConsole and IConsoleNamespace2. You should be aware that MMC remains in a state in which everything can be queried during the call to Destroy so the snap-in can get information from the console. On return from Destroy, however, data integrity cannot be guaranteed.

Comparing Data Objects

The CompareObjects method provides a way for a snap-in component to compare two data objects and determine whether they refer to the same physical object. This method is used, for example, to detect duplicate property sheets.

Working with IComponent

Working with IDataObject

Working with IConsole

Registering and Unregistering a Snap-in