Share via


Walkthrough: Exposing a .NET Plug-in Model

This walkthrough demonstrates extending the classic Scribble MFC sample to support a plug-in model using .NET technologies.

The MFC Scribble solution is extended to expose a .NET object model. Furthermore, the sample also exposes a plug-in architecture that enables .NET plug-ins to interact seamlessly with the MFC Scribble solution.

Prerequisites

ScribbleDotNET Sample: Extending Scribble with CLR Features

To build and run this sample

  1. Open the solution file, Scribble.sln, in the Visual Studio development environment.

  2. Build the solution using either the Debug or Release configurations.

  3. Under the Solution Explorer open Solution Items plugins.xml.

  4. In the plugins.xml file, change the first part of the path in the Assembly elements to correspond to the configuration you built under, e.g. change "VBShapes.dll" to "Debug\VBShapes.dll" and change "CSPenColor.dll" to "Debug\CSPenColor.dll" if you built using the Debug configuration.

  5. Run the built executable.

  6. Select Plugins from the menu bar.

    1. Select the Draw Shapes menu item. This will run the VBShapes plug-in which will draw three outlines of different colored squares.
  7. Select Plugins from the menu bar.

    1. Select the Change Pen Color menu item. This will run CSPenColor plug-in which will allow you to select the pen color through a color dialog box.

How it Works

This sample is implemented in two major parts:

  1. Exposing an Object Model for the plug-in writers to use

    Two main interfaces are exposed to implement the plug-in model (see the ScribbleApp Project for definitions of these interfaces):

    • IScribbleApp – this interface exposes a set of features that will allow the plug-in writer to interact with a running Scribble application. The interface is implemented by Scribble (see ScribbleApp.cpp in the Scribble Project) and an instance of the implemented interface is passed to the Run() member function of IScribblePlugin, discussed next

    • IScribblePlugin – this interface is implemented by the plug-in writer. The plug-in writer will use the features exposed by IScribbleApp to implement their plug-in.

  2. Extending the base Scribble application to find, load, and execute the plug-ins

    • First an XML file containing a list of plug-ins to be loaded is parsed. Each plug-in found is loaded and verified to be of the correct type by using System.Reflection. More specifically, we use reflection to search through the types exposed by the plug-in module and ensure that one of them implements the IPlugin interface that we have exposed. The implementation for this can be found in PluginBld.cpp in the Scribble Project.

    • CScribbleApp then uses the routines implemented in PluginBld.cpp to obtain a list of all the discovered plugins. For each plugin in this list a menu item is dynamically created under the menu Plugins. When the user selects a menu item from Plugins the appropriate plugin will be run.

Technologies Used

  • Using XML to specify the location of the plug-ins

  • Using reflection to verify the plug-in specified in the XML file is of the appropriate type

  • Defining an object model for creating plug-ins

  • Loading and running the plug-in in a C++ application

See Also

Tasks

SCRIBBLE Sample: MFC MDI Drawing Application