Wednesday, February 27, 2008 12:04 AM bart

The managed MMC 3.0 snap-in cookbook

My recent series of "cookbook" posts has been very well-received and coincidentally I got mail today about MMC 3.0 snap-ins. I wrote on the subject a while (read: Vista RC1 timeframe) ago, so in this post I'll revisit the topic in cookbook style in order to provide the foundation for my next post on MMC 3.0 and PowerShell layering, a topic I talked about on TechEd EMEA 2007 but that never made it to a blog post.

For the record, my previous cookbook posts include the following:

For regular readers of my blog, step 1 and 2 will sound repetitive but in true PowerShell-style I'd say "verbosity is your friend" (J. Snover), especially in cookbooks.

 

Step 1 - Create a Class Library project

Create a new (C#) class library project, e.g. called MyMmcSnapIn:

image

 

Step 2 - Import references

In Solution Explorer, right-click the project node and select Add Reference... On the Browse tab, navigate to the %programfiles%\Reference Assemblies folder and locate the Microsoft\mmc\v3.0 subfolder:

image

Note: Reference Assemblies are installed by the Windows SDK - a must-have for each platform developer.

In there, select the Microsoft.ManagementConsole.dll file:

image

Click OK. Next, go back to the Add Reference dialog and select System.Configuration.Install from the .NET tab:

image

 

Step 3 - Create your snap-in

Snap-ins derive from the SnapIn class. Go ahead and rename Class1.cs to MySnapIn.cs. Next, inherit the class from SnapIn which will require to import the Microsoft.ManagementConsole namespace:

image

For sake of this post, let's keep things simple and just implement the bare minimum, i.e. setting the RootNode property to some node:

image

I'm using C# 3.0 syntax to do this, as shown below:

image

resulting in this piece of code:

image

A real implementation (see next post) would likely create a node class that derives from ScopeNode to create a custom node that consumes data (e.g. from PowerShell, see next post).

 

Step 4 - Adding metadata

Our snap-in needs to carry some metadata using the SnapInSettings custom attribute. This one needs a GUID so go to Tools, Create GUID to create one:

image

Click Copy and Exit which will put a GUID on the clipboard:

image

Now add the SnapInSettings custom attribute to your class:

image

and specify DisplayName and Description (other properties are not really required in this case):

image

 

Step 5 - The installer

In order to register the snap-in on the machine we need to add an installer class. This is as easy as creating a class deriving from SnapInInstaller:

image

Here's the resulting complete code:

image

 

Step 6 - Compile and register

Time to compile the project. Next, open up a Visual Studio 2008 Command Prompt running as Administrator and cd into the bin\Debug folder of your project. Now run installutil on the created assembly:

image

To verify the installation was successful, you can take a look in the registry under HKLM\Software\Microsoft\MMC\SnapIns and look for a key called FX:myguid where myguid is the one specified on the SnapInSettingsAttribute. It should point at your newly created assembly:

image

 

Step 7 - Create an MMC console for debugging

Time to test our snap-in. Go to Start, Run and specify mmc.exe. In the MMC console go to File, Add/Remove Snap-In (CTRL-M). You should see the registered MMC snap-in in there:

image

Notice the name and description. Select it and click Add. Finally click OK. The result should look like:

image

Quite minimalistic, I agree, but we're alive and kicking! Finally choose File, Save and save the console configuration to a file called Debug.msc under your project's folder:

image

Finally, close the MMC console (otherwise the loaded snap-in dll would remain locked, blocking subsequent builds).

 

Step 8 - Setting up the debugger

Back in Visual Studio, go to the project properties. On the tab Debug enter the path to mmc.exe (in the system32 folder) and specify the relative path (starting from bin\Debug) to Debug.msc created in the previous step for the command-line arguments:

image

Set a breakpoint in the code:

image

and press F5. You'll see the breakpoint getting hit:

image

 

Congratulations - your MMC debugging dinner is ready to be served!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# Tiny IIS Manager - Layering MMC 3.0 snap-ins on top of Windows PowerShell

Thursday, February 28, 2008 2:25 AM by B# .NET Blog

Back in manageability land. On TechEd EMEA Developers 2007 I delivered a talk on "Next-generation

# Daily Bits - February 29, 2008 | Alvin Ashcraft's Daily Geek Bits

Pingback from  Daily Bits - February 29, 2008 | Alvin Ashcraft's Daily Geek Bits

# re: The managed MMC 3.0 snap-in cookbook

Monday, March 03, 2008 1:20 AM by Christian Malpas

How can I localize the SnapIn attributes?

Help file redirection, as explained at msdn2.microsoft.com/.../ms692743(VS.85).aspx, does not work as I would like in Windows XP and Server 2003, and the SnapInHelpTopicAttribute class is sealed, so I cannot customize help support in a derived class.

# Windows Server 2008 Open House Presentation and Demos

Friday, March 14, 2008 1:03 PM by All Your Base Are Belong To Us

Alon Fliess and I have presented at three Open House sessions at Microsoft on the subject of the upcoming

# TechEd 2008 South Africa Demo Resources

Saturday, August 09, 2008 7:39 AM by B# .NET Blog

Last week, I had the honor to speak at TechEd 2008 South Africa on a variety of topics. In this post