Download example - 102 KB
introduction
The Article / Code Snippet Helps in Creation of MMC Snapin Using C #.
background
The framework for building management consoles for Windows NT / 2000 is developed by Jim. Jim is the author for ironringsoftware.com. In our article we will be using this framework to derive the UI for the management console. Detailed step for generating the snapin console IS explained below.
Code use steps
Here Are The Steps To Use the mmc library create by the author at ironringsoftware.com to create management consoles for Windows NT / 2000
STEP 1: Download Framework
You need to Download The Latest Source of MMC Snapin Framework from IronRingsoftware.com.
The zip file Downloaded Contains Three Projects.
First is
Step 2: View example
. Set
STEP 3: Initialization
Create a directory. "C: / FileActDemo" with few sample files and sub directories in it View the properties of project
Example 1: Let us Take an Example of Creating a Node Which Contains All The Directory Information on The Result Pane.
Open
// this is The Variable Which Holds The Data Passed from the framework.
Protected Hashtable M_Properties;
Initialize The Variable in The Constructor of The Class As Shown in The Code Below
M_Properties = new hashtable ();
THIS Is The Public Property Used to Pass The Values from The Framework To The Form. Add Public Property For The Class To Access The Protected Variable Just Defined As Shown In The Code Below
Public HashTable Properties {
Get {return m_properties;}
Set {m_properties = value;}
}
Add New .cs File Into Project
//
// Code Block for DirectoryNode.cs
//
Using system;
Using system.io;
Using system.data;
Using system.Runtime.InteropServices;
Namespace ironring.management.mmc {
///
/// Any Node Which We Create Should Be Derived from the BaseNode.
/// this node will get displayed in The Left Pane of The MMC.
////// summary>
Public Class DirectoryNode: BaseNode {
Protected DataTable M_Table = NULL;
Public DirectoryNode (SnapinBase Snapin): Base (Snapin) {
}
///
/// this is the public property to get / set the datable value.
/// this DataTable Values Are the ones which Gets Displayed in The Right Pane
/// of the mmc for the respective node item..
/// summary>
Public DataTable Table {
Get {return m_table;}
Set {m_table = value;}
}
///
/// this Method Is Called by the MMC Snap to Populate The Data for ListView
/// in The Right Pane, for The Respective Node Item Selected in The Left Pane.
/// summary>
Public Override void onshow () {
Iconsole2 console = snapin.resultviewconsole;
IHeaderCtrl2 header = console as iHeaderCtrl2;
Onshowheader (HEADER);
IResultData RData = Console As IResultdata;
OnshowData (RDATA);
RData.setViewMode ((int) viewmode.report;
}
///
/// this method is caled by the onshow () Method to
/// display header for the listview
/// in The Right Pane, for The Respective Node Item Selected in The Left Pane.
/// summary>
Public Virtual Void OnShowhead (iHeaderCtrl2 header) {
Try {
IF (Table! = NULL) {
Foreach (Datacolumn COL in Table.columns) {
String name = col.columnname;
Int ORD = col.ordinal;
Header.insertColumn (ORD, Name, (Int) ColumnHeaderFormat.Left, 140);
}
}
}
Catch (comexception e) {
System.Diagnostics.debug.writeline (E.MESSAGE);
Throw e;
}
Catch (Exception E) {
System.Diagnostics.debug.writeline (E.MESSAGE);
Throw e;
}
}
///
/// this method is caled by the onshow () Method to
/// Display data for the listview /// in The Right Pane, for the respective node item, for the respective node.
/// summary>
Public Virtual Void OnShowdata (IResultData Resultdata) {
IF (Table! = NULL) {
INT nrow = 1;
ResultDataItem RDI = New ResultDataItem ();
Foreach (DATAROW ROW in Table.Rows) {
Row.toString ();
Rdi.mask = (uint) RDI.STR | (uint) RDI.Image | (UINT) RDI.Param;
RDI.NIMAGE = -1;
RDI.STR = (INTPTR) (- 1);
Rdi.ncol = 0;
Rdi.lparam = m_ICOOKIE | (NROW << 16);
ResultData.insertitem (Ref RDI);
NROW ;
}
}
}
///
/// this Method Is Called by the MMC Snap To Display
/// Each ListView Item's Value
/// in The right Pane.
/// summary>
Public Override Void getDisplayInfo (Ref resultsDataItem results) {
Bool bcallbase = true;
IF (Table! = NULL) {
INT nrow = (ResultDataItem.lparam >> 16) - 1;
INT ncol = resultddataitem.ncol;
IF ((ResultDataItem.mask & (uint) rdi.str)> 0) {
String data = displayname;
IF (nrow> = 0 && nrow
Ncol> = 0 && ncol
Data = Table.Rows [nROW] .ItemRay [ncol] .tostring ();
BCALLBASE = FALSE;
}
ResultDataItem.str = Marshal.StringTocotaskMemuni (DATA);
}
}
IF ((ResultDataItem.Mask & (UINT) RDI.Image)> 0) {
INT offset = 1;
iF (iSusesmallicons ())
OFFSET = 0;
ResultDataItem.nImage = (cookie << 16) offset;
}
IF (bcallbase)
Base.getdisplayInfo (Ref resultsDataItem);
}
///
/// this Method Creates The DataTable with Appropriate Data Which Servers
/// as the datasource for the listview displayed in the right pane, for the /// respective node it.
/// summary>
Public void createDataTableStructure () {
DataTable DataTable = New DataTable ();
Datacolumn Datacolumn = new datacolumn ();
Datacolumn.DataType = system.type.gettype ("system.string");
Datacolumn.columnname = "name";
Datacolumn.readonly = true;
Datacolumn.unique = true;
// Add the column to the datacolumncollection.
DataTable.columns.add (datacolumn);
Datacolumn = new datacolumn ();
Datacolumn.DataType = system.type.gettype ("system.string");
Datacolumn.columnname = "Last Accessed";
Datacolumn.readonly = true;
Datacolumn.unique = false;
// Add the column to the datacolumncollection.
DataTable.columns.add (datacolumn);
Datacolumn = new datacolumn ();
Datacolumn.DataType = system.type.gettype ("system.string");
Datacolumn.columnname = "Last Written";
Datacolumn.readonly = true;
Datacolumn.unique = false;
// Add the column to the datacolumncollection.
DataTable.columns.add (datacolumn);
// Make The ID Column The Primary Key Column.
Datacolumn [] PrimaryKeyColumns = New Datacolumn [1];
PrimaryKeyColumns [0] = DATATABLE.COLUMNS ["Name"];
DataTable.primaryKey = primaryKeyColumn;
THIS.TABLE = DATATABLE;
}
Public void filldataablevalues () {
DataRow DataRow;
DirectoryInfo DirectoryInfo = New DirectoryInfo
THIS.PROPERTIES ["NodePath"]. TOSTRING ());
FOREACH (FileInfo fileInfo in DirectoryInfo.getfiles ()) {
IF (! fileinfo.name.tostring (). Endswith (". Transfer") &&
FileInfo.name.toString (). Endswith (". config")) {datarow = this.table.newrow ();
DATAROW ["name"] = fileinfo.name;
DataRow ["Last Accessed] = fileInfo.lastaccesstime.tostring ();
DataRow ["Last Written"] = fileInfo.lastwritetime.tostring ();
THIS.TABLE.ROWS.ADD (DATAROW);
}
}
}
}
}
Add New Windows Form to Project
//
// Code Block for FormDirectoryProperty //
//
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Namespace mmctest
{
///
/// this is a sample form which Opens up when menu Button "FileAct Properties"
////////////// '
/// summary>
Public class formdirectoryProperty: system.windows.forms.form {
Private system.componentmodel.Container Components = NULL;
Private hashtable nodeproperties;
Public FormDirectoryProperty () {
//
// Required for Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add Any Constructionor Code After InitializationComponent Call
//
THIS.ICON = NULL;
THIS.WIDTH = 384;
THIS.HEIGHT = 440;
THIS.MAXIMIZEBOX = FALSE;
THIS.MINIMIZEBOX = FALSE;
THIS.TEXT = "Directory Properties";
THIS.SHOWINTASKBAR = FALSE;
THIS.STARTPSITION = formstartPosition.centerparent;
THIS.FORMBORDERSTYLE = formborderstyle.fixeddialog;
}
///
/// Clean Up Any Resources Being Used.
/// summary>
Protected Override Void Dispose (BOOL Disposing) {
IF (disposing) {
IF (Components! = NULL) {
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Required Method for Designer Support - Do Not Modify
/// The contents of this method with the code editor.
/// summary>
Private vidinitiRizeComponent ()
{
This.Components = new system.componentmodel.container ();
This.size = new system.drawing.size (300, 300);
}
#ndregion
Public HashTable NodeProperties {
SET {
NodeProperties = value;
}
}
Private Void FormFileProperty_Load (Object Sender, System.Eventargs E) {
FetchGeneralProperty ();
}
Private void fetchGeneralproperty () {
MessageBox.show (NodeProperties ["NodePath"]. TOSTRING ());
}
}
}
Add New Windows Form To Project
//
// Code Block for FormFileProperty //
//
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Namespace mmctest
{
///
/// this is a sample form which Opens up when menu Button "FileAct Properties"
////////////////////////////////////////////////////////// '
/// summary>
Public class formfileproperty: system.windows.forms.form {
///
/// Required Designer Variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Private hashtable nodeproperties;
Public FormFileProperty () {
//
// Required for Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add Any Constructionor Code After InitializationComponent Call
//
THIS.ICON = NULL;
THIS.WIDTH = 384;
THIS.HEIGHT = 440;
THIS.MAXIMIZEBOX = FALSE;
THIS.MINIMIZEBOX = FALSE;
THIS.TEXT = "File Properties"; this.showintaskbar = false;
THIS.STARTPSITION = formstartPosition.centerparent;
THIS.FORMBORDERSTYLE = formborderstyle.fixeddialog;
}
///
/// Clean Up Any Resources Being Used.
/// summary>
Protected Override Void Dispose (BOOL Disposing) {
IF (disposing) {
IF (Components! = NULL) {
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Required Method for Designer Support - Do Not Modify
/// The contents of this method with the code editor.
/// summary>
Private vidinitiRizeComponent ()
{
This.Components = new system.componentmodel.container ();
This.size = new system.drawing.size (300, 300);
}
#ndregion
Public HashTable NodeProperties {
SET {
NodeProperties = value;
}
}
Private Void FormFileProperty_Load (Object Sender, System.Eventargs E) {
FetchGeneralProperty ();
}
Private void fetchGeneralproperty () {
Messagebox.show (NodeProperties ["NodePath"]. TOSTRING ()
"//" nodeproperties ["noderesultname"]. TOSTRING ());
}
}
}
Add New .cs File Into Project
//
// Code Block for FileActsnapINElement.cs
//
Using system;
Using system.io;
Using system.data;
Using system.drawing;
Using system.collections;
Using system.configuration;
Using system.Runtime.InteropServices;
Using mmctest;
USING IRONRING.MANAGEMENT.MMC;
Namespace mmctest {
///
/// this is the class which Uses the DirecotryNode CREATED ABOVE
/// to populate it on to the mmc.
/// summary>
Public class fileactsnapinelement {
Public FileArgectSnapINElement () {
}
Public void createDirectoryNode (Snapinbase Snapinbase,
String Parentdirectory, BaseNode ParentNode) {
DirectoryInfo DirectoryInfo = New DirectoryInfo (ParentDirectory);
// Create DirectoryNode with All The Default Properties Set
DirectoryNode DirectoryNode = New DirectoryNode (Snapinbase);
DirectoryNode.DisplayName = DirectoryInfo.Name;
DirectoryNode.properties.add ("NodePath", DirectoryInfo.Parent.Fullname
"//" DirectoryInfo.Name);
DirectoryNode.properties.Add ("NodeResultName", "");
DirectoryNode.Properties.Add ("EventTarget", "");
DirectoryNode.createdTATABLESTRUCTURE ();
DirectoryNode.FillDataTableValues ();
ParentNode.Addchild (DirectoryNode);
// add menu and eventhandler for menu for DirectoryNode
Menuitem TopMenuItemFileActproperties = New Menuitem ("FileAct Properties",
"FileAct Properties for SELECTED THEEM",
New MenuCommandrandler (onmenus);
DirectoryNode.AddtopMenuItem (TopMenuItemFileActproperties);
Menuitem TopMenuItemRefresh = New Menuitem ("Refresh", "Refresh The Contents",
New menucommandhandler (onmenurefreshclick);
DirectoryNode.AddtopMenuItem (TopMenuItemRefresh);
// Add other Event Handlers for DirectoryNode
DirectoryNode.OnSelectScopeEvent =
New NodeNotificationHandler (OnseectScopeEvent);
DirectoryNode.OnSelectResulTevent =
New NodenotificationHandler (OnsectResulTevent);
Foreach (DirectoryInfo SubdirectoryInfo in DirectoryInfo.getdirectories ()) {
CreatedIRectoryNode (Snapinbase, SubdirectoryInfo.Parent.FullName
"//" SubdirectoryInfo.Name, DirectoryNode;
}
}
// ******************************************************** ********************* //
// Event Handlers
// Handles Events generated by nodes and the result pane
//
// ******************************************************** ********************* //
// ******************************************************** ********************* //
// mmc Event Handlers
// ******************************************************** ********************* //
///
/// this Method Is Called When The Node Element is selected.
/// summary>
Protected Void OnseTScopeEvent (Object Sender, NodeEventArgs Args) {
DirectoryNode DirectoryNode = (DirectoryNode) Sender;
DirectoryNode.properties ["EventTarget"] = "node";
}
///
/// this Method Is Called When The Node's List / Result Item Is SELECTED.
/// summary>
Protected Void OnsectResulTevent (Object Sender, NodeEventArgs Args) {
DirectoryNode DirectoryNode = (DirectoryNode) Sender;
DirectoryNode.properties ["EventTarget"] = "noderesult";
}
// ******************************************************** ********************* //
// Menu Event Handlers
// ******************************************************** ********************* //
///
/// this Method Is Called When Menu Item "Filect Properties" is Clicked.
/// summary>
Public void onmenufileActPropertiesClick (Object sender, Basenode arg) {
DirectoryNode DirectoryNode = (DirectoryNode) arg;
Uint pitemid;
Iconsole2 console = DirectoryNode.Snapin.ResultViewConsole;
IResultdata Resultdata = Console As IResultdata;
ResultDataItem ResultDataItem = new resultDataItem ();
Try {
IF (DirectoryNode.properties ["EventTarget"]. TOSTRING (). Equals ("node")) {FormDirectoryProperty FormDirectoryProperty = New FormDirectoryProperty ();
FormDirectoryProperty.NodeProperties = DirectoryNode.properties;
FormdirectoryProperty.showdialog ();
}
Else {
For (int loopCounter = 0; loopCounter LoopCounter ) { Resultdata.findItemBylparam (DirectoryNode.cookie | (LoopCounter 1 << 16), OUT PITEMID; ResultDataItem.ncol = 0; ResultDataItem.mask = (uint) RDI.State | (uint) RDI.Param; ResultDataItem.itemid = (int) pitemid; ResultData.GetItem (Ref resultsDataItem); if (ResultDataItem.nState == 3) { DirectoryNode.properties ["noderesultname"] = DirectoryNode.Table.Rows [loopcounter] [0]; Break; } } FormFileProperty formfileProperty = new formfileproperty (); FormfileProperty.NodeProperties = DirectoryNode.properties; FormfileProperty.showdialog (); } } Catch (Exception EX) { Throw EX; } } /// /// this Method Is Called When Menu Item "Refresh" is click. /// summary> Public void onmenurefreshclick (Object Sender, Basenode Arg) { String selectedfilename = null; DirectoryNode DirectoryNode = (DirectoryNode) arg; Uint pitemid; Iconsole2 console = DirectoryNode.Snapin.ResultViewConsole; IResultdata Resultdata = Console As IResultdata; ResultDataItem ResultDataItem = new resultDataItem (); Try { IF (! "" EventTarget "]. TOSTRING (). Equals (" node ") { For (int loopCounter = 0; loopCounter LoopCounter ) { Resultdata.findItemBylparam (DirectoryNode.cookie | (LoopCounter 1 << 16), OUT Pitemid; ResultDataItem.ncol = 0; ResultDataItem.mask = (uint) RDI.State | (uint) RDI.Param; ResultDataItem.itemid = (int) pitemid; ResultData.GetItem (Ref resultsDataItem); if (ResultDataItem.nState == 3) { SELECTEDFILENAME = DirectoryNode.Table.Rows [loopcounter] [0] .tostring (); Break; } } } Resultdata.deleteallrsltItems (); DirectoryNode.Table.Rows.clear (); DirectoryNode.Table.Acceptchanges (); DirectoryNode.FillDataTableValues (); DirectoryNode.OnShowdata (Resultdata); IF (! "" EventTarget "]. TOSTRING (). Equals (" node ") { For (int loopCounter = 0; loopCounter LoopCounter ) { IF (DirectoryNode.Table.Rows [loopCounter] [0] .tostring (). Equals. SELECTEDFILENAME)) { Resultdata.findItemBylparam (DirectoryNode.cookie | (LoopCounter 1 << 16), OUT PITEMID; ResultDataItem.mask = (uint) rdi.state; ResultDataItem.itemid = (int) pitemid; ResultDataItem.nState = 3; ResultData.SetItem (Ref resultsDataItem); Break; } } } } Catch (Exception EX) { Throw EX; } } } } Open FileActsnapINElement FileArgectSnapINElement = New FileActsnapINElement (); FileActSnapINElement.createdIRectoryNode (this, @ "c: / fileactdemo", rootnode; Build The Solution Execute The Project To See The Addition of New Node Just Created Points of interest There is lot more work to be done to the Framework to include functionalities to display help, to incorporate multiselection option, task pads etc. You can join the team at http://www.ironringsoftware.com/ to incorporate more functionalities to the framework And help the product to be more effect.history Initial Version - 12-NOV-2003