How-To Create a Hello World Snap-in

This sample shows you how to create a "Hello World" snap-in. While this snap-in itself does not have any interesting behavior, this procedure is intended to familiarize you with the basic steps to get a snap-in up and running using the MMC 3.0 SDK. The subsequent samples will show you how to create snap-ins with real functionality.

This sample shows you how to create your first snap-in. It shows you how to write the snap-in code in a managed language such as C#. If you choose C#, you can start by creating a file called SimpleSnapIn.cs. When this code is compiled, it will create a snap-in dll called SimpleSnapInSample.dll. This topic will guide you in a step-by-step fashion on how to write, compile, install, and run the snap-in code.

How-To create a "Hello World" snap-in

  1. Begin by adding the using directives that reference the assemblies that are required for this sample. The first directive is a reference to the MMC 3.0 assembly; the second directive is a reference to the component model assembly that is the source of the RunInstaller attribute. The PermissionSetAttribute allows security actions for a permission set to be applied to code using declarative security.

    using Microsoft.ManagementConsole;
    using System.ComponentModel;
    using System;
    using System.Security.Permissions;
    
    [assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Unrestricted = true)]
    
    
  2. Define a namespace for creating this and subsequent samples.

    namespace Microsoft.ManagementConsole.Samples
    
    
  3. Installutil.exe is a command-line utility that ships with .NET. It requires an Installer-derived class to be declared within the target assembly and further requires that the RunInstaller attribute be defined. Derive a class from SnapInInstaller. It automatically discovers registration information in the assembly and registers the discovered snap-ins. Set the RunInstaller attribute to True. This provides an entry point for tools such as Installutil.exe to execute the framework installer code.

    /// <summary>
    /// The RunInstaller attribute allows the .Net framework to install the assembly.
    /// </summary>
    [RunInstaller(true)]
    public class InstallUtilSupport : SnapInInstaller
    {
    }
    
    
  4. Every snap-in must define a new class that is derived from the SnapIn class. Create a new scope node and assign it to be the root node for the snap-in. Give the root node a display name.

    /// <summary>
    /// The constructor.
    /// </summary>
    public SimpleSnapIn() 
    {
        // Update tree pane with a node in the tree
        this.RootNode = new ScopeNode();
        this.RootNode.DisplayName = "Hello World";
    }
    
    
  5. Add the attribute SnapInSettingsAttribute. This attribute must be defined for any snap-in that must be featured on the Add/Remove dialog box in the MMC. This attribute defines the GUID for the snap-in, a display name for the snap-in, and a short description of the snap-in.

    /// <summary>
    /// The main entry point for the creation of the snap-in.
    /// </summary>
    [SnapInSettings("{CFAA3895-4B02-4431-A168-A6416013C3DD}", 
         DisplayName = "- Simple SnapIn Sample", 
         Description = "Simple Hello World SnapIn")]
    
    

The complete sample code for SimpleSnapIn.cs is given in the following Example section. Compile the code to create a dll called SimpleSnapIn.dll. All the code for this sample is available in the <MMC 3.0 Samples>\SimpleSnapInSample directory.

Steps to create, install, and run the snap-in

  1. To install this snap-in, run the .NET Framework InstallUtil.exe program using the following command-line command: InstallUtil.exe SimpleSnapInSample.dll. Note that if the Microsoft.ManagementConsole dll is not in the GAC, both the Microsoft.ManagementConsole.dll and the SimpleSnapInSample.dll must be in the same directory. If you need to uninstall the snap-in later, run the previous InstallUtil.exe command with the /uninstall switch.

  2. The InstallUtil.exe command attempts to install your snap-in using the SnapInSettingsAttribute. The utility creates an InstallUtil.InstallLog file to show the success or failure of the install and all the actions that were taken.

  3. InstallUtil.exe populates the registry entries for the given snap-in under the HKLM/Software/Microsoft/MMC/SnapIns key.

  4. After the snap-in is installed, the Snap-in is visible to MMC and can be added to the MMC Console using the Add/Remove dialog. To test this snap-in, run MMC 3.0 (mmc.exe) and use the Add/Remove Snap-in menu. The SimpleSnapIn appears in the Add dialog and can be loaded in the MMC console.

Example

Here is the complete sample code for SimpleSnapIn.cs that creates the "Hello World" snap-in.

using Microsoft.ManagementConsole;
using System.ComponentModel;
using System;
using System.Security.Permissions;

[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Unrestricted = true)]
namespace Microsoft.ManagementConsole.Samples
{
    /// <summary>
    /// The RunInstaller attribute allows the .Net framework to install the assembly.
    /// </summary>
    [RunInstaller(true)]
    public class InstallUtilSupport : SnapInInstaller
    {
    }
   
    /// <summary>
    /// The main entry point for the creation of the snap-in.
    /// </summary>
    [SnapInSettings("{CFAA3895-4B02-4431-A168-A6416013C3DD}", 
		 DisplayName = "- Simple SnapIn Sample", 
		 Description = "Simple Hello World SnapIn")]
	public class SimpleSnapIn : SnapIn
	{
        /// <summary>
        /// The constructor.
        /// </summary>
        public SimpleSnapIn() 
		{
			// Update tree pane with a node in the tree
			this.RootNode = new ScopeNode();
			this.RootNode.DisplayName = "Hello World";
		}
	} // class
 } // namespace

See Also

Show:
© 2014 Microsoft