How-To Create a Hello World Snap-in
Basically, a managed code MMC 3.0 snap-in is nothing more than an assembly with System.Configuration.Install.Installer support. So, we'll start by creating a new Class Library project in Visual Studio 2008. 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. This 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 HelloWorldSnapIn.cs. When this code is compiled, it will create a snap-in dll called HelloWorldSnapIn.dll.
This is the first screen after creating the project
Now comes the somewhat tricky part of referring to Microsoft.ManagementConsole.dll. This assembly can be found in the GAC (%windir%\assembly) but doesn't appear in the Visual Studio 2008 Add Reference dialog by default.
C:\Users\Bart>cd %windir%\assembly\GAC_MSIL\Microsoft.ManagementConsole\
3.0.0.0__31bf3856ad364e35
C:\Windows\assembly\GAC_MSIL\Microsoft.ManagementConsole\
3.0.0.0__31bf3856ad364e35>copy Microsoft.ManagementConsole.dll c:\temp
1 file(s) copied.
We need to add reference to the Microsoft.ManagementConsole.dll as below
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)]
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.
////// The RunInstaller attribute allows the .Net framework to install the assembly. /// [RunInstaller(true)] public class InstallHelloWorldSupport : SnapInInstaller { }
Please do not forget to add reference to using System.Configuration.Install
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. 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.
[SnapInSettings("{63154B48-C39C-48aa-9B80-DB0BEC417190}",
DisplayName = "HelloWorld SnapIn Sample",
Description = "Gigy's Hello World SnapIn")]
public class HelloWorlMainSnapIn : SnapIn
{
public HelloWorlMainSnapIn()
{
// Update tree pane with a node in the tree
this.RootNode = new ScopeNode();
this.RootNode.DisplayName = "Hello World";
}
}
We have done with the coding part. If everything is fine, we should be able to see HelloWorldSnapIn.dll in the bin folder.
Full Source Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ManagementConsole;
using System.ComponentModel;
using System.Security.Permissions;
using System.Configuration.Install;
namespace HelloWorldSnapIn
{
///
/// The RunInstaller attribute allows the .Net framework to install the assembly.
///
[RunInstaller(true)]
public class InstallHelloWorldSupport : SnapInInstaller
{
}
[SnapInSettings("{63154B48-C39C-48aa-9B80-DB0BEC417190}",
DisplayName = "HelloWorld SnapIn Sample",
Description = "Gigy's Hello World SnapIn")]
public class HelloWorlMainSnapIn : SnapIn
{
public HelloWorlMainSnapIn()
{
// Update tree pane with a node in the tree
this.RootNode = new ScopeNode();
this.RootNode.DisplayName = "Hello World";
}
}
}
Cd to the bin\Debug folder of your project and installutil.exe the assembly as shown below:
installutil -i HelloWorldSnapIn.dll
Make sure that you run this utility as an Administrator, if you are trying this on Vista.In the registry you should now find a key called HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MMC\SnapIns\FX:{63154B48-C39C-48aa-9B80-DB0BEC417190}.The FX: prefix which indicates the managed code our snap-in is built with.
Now it's time to test the snap-in. Open mmc.exe. The MMC 3.0 version looks as follows and has the Action pane on the right.
Now press CTRL-M (or go to File, Add/Remove Snap-in...) and add the "MMC 3.0 to the max" snap-in to the Console Root as shown below. Add the HelloWorld to the Selected snap-ins list
HelloWorld snap-in is ready
MMC 3.0 makes it really easy for managed code developers to create appealing MMC snap-ins to manage their applications. To uninstall it try this way
installutil -i HelloWorldSnapIn.dll /uninstall
