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:
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:
Note: Reference Assemblies are installed by the Windows SDK - a must-have for each platform developer.
In there, select the Microsoft.ManagementConsole.dll file:
Click OK. Next, go back to the Add Reference dialog and select System.Configuration.Install from the .NET tab:
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:
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:
I'm using C# 3.0 syntax to do this, as shown below:
resulting in this piece of code:
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:
Click Copy and Exit which will put a GUID on the clipboard:

Now add the SnapInSettings custom attribute to your class:
and specify DisplayName and Description (other properties are not really required in this case):
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:
Here's the resulting complete code:
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:
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:
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:
Notice the name and description. Select it and click Add. Finally click OK. The result should look like:
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:
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:
Set a breakpoint in the code:
and press F5. You'll see the breakpoint getting hit:
Congratulations - your MMC debugging dinner is ready to be served!
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks
Filed under: MMC 3.0