home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / mmc / CData.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  14.7 KB  |  403 lines

  1. //-------------------------------------------------------------
  2. // CData.cs
  3. //
  4. // This implements the IComponentData, ISnapinHelp2, and IExtendPropertySheet
  5. // MMC interfaces.
  6. //
  7. // Its GUID is {94B9D51F-C874-4da0-BC13-FDA94CBF72DE}
  8. //-------------------------------------------------------------
  9.  
  10. using System;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace Microsoft.SampleMMC
  14. {
  15. [Guid("94B9D51F-C874-4da0-BC13-FDA94CBF72DE")] 
  16. public class CData : IComponentData, ISnapinHelp2, IExtendPropertySheet
  17. {
  18.     private CComponent          m_Component;        // The IComponent object
  19.     private IConsole2           m_Console;          // A reference to the MMC console
  20.     private IConsoleNameSpace2  m_ConsoleNameSpace; // A reference to the MMC console namespace
  21.     private CNode[]             m_NodeCollection;   // The Nodes displayed in the snapin
  22.  
  23.     //-------------------------------------------------
  24.     // CData - Constructor
  25.     //
  26.     // The constructor is responsible for constructing
  27.     // the nodes that will be displayed in the snapin
  28.     //-------------------------------------------------    
  29.     public CData()
  30.     {
  31.         // First, null out all the member variables
  32.         m_Component = null;
  33.         m_Console = null;
  34.         m_ConsoleNameSpace = null;
  35.  
  36.         int i;
  37.         NodeInfo[]   Nodes;
  38.  
  39.         // Let's create the Node Structures
  40.         Nodes = CreateNodes();
  41.  
  42.         int numNodes = Nodes.Length;
  43.  
  44.         m_NodeCollection = new CNode[numNodes];
  45.  
  46.         // Let's take this Node information structures and create Nodes with them
  47.         for(i=0; i<numNodes; i++)
  48.             m_NodeCollection[i] = new CNode(i, Nodes[i]);
  49.  
  50.         // Now add children nodes
  51.         for(i=0; i<numNodes; i++)
  52.          if (Nodes[i].iaChildren != null)
  53.             for(int j=0; j<Nodes[i].iaChildren.Length; j++)
  54.                 m_NodeCollection[i].AddChild(ref m_NodeCollection[Nodes[i].iaChildren[j]]);
  55.  
  56.         // We should be ready to rock now!
  57.      
  58.     }// CData
  59.  
  60.     //-------------------------------------------------
  61.     // Initialize
  62.     //
  63.     // This function is responsible for recieving the
  64.     // MMC Console interface, and I also have it inserting
  65.     // all the images MMC will need to display the snapin
  66.     //-------------------------------------------------    
  67.     public void Initialize(Object pUnknown)
  68.     {
  69.         m_Console          = (IConsole2)pUnknown;
  70.         Marshal.AddRef(Marshal.GetIUnknownForObject(m_Console));
  71.  
  72.         m_ConsoleNameSpace = (IConsoleNameSpace2)pUnknown;
  73.         Marshal.AddRef(Marshal.GetIUnknownForObject(m_ConsoleNameSpace));
  74.  
  75.         // Now we'll add the images we need for the snapin
  76.         try
  77.         {
  78.             IImageList il=null;
  79.             m_Console.QueryScopeImageList(out il);
  80.      
  81.             for(int i=0; i<m_NodeCollection.Length; i++)
  82.             {
  83.                 il.ImageListSetIcon(m_NodeCollection[i].IconHandle, i);
  84.             }
  85.        }
  86.        // For some reason, we tend to throw a 8004005 here (E_FAIL)... doesn't hurt us though
  87.        catch(Exception)
  88.        {
  89.        }
  90.     }// Initialize
  91.  
  92.     //-------------------------------------------------
  93.     // CreateComponent
  94.     //
  95.     // This function creates the component object and
  96.     // passes it back to the MMC
  97.     //-------------------------------------------------    
  98.     public void CreateComponent(out IComponent ppComponent)
  99.     {
  100.         // Make sure we don't already have a component created
  101.         if (m_Component == null)
  102.            m_Component = new CComponent(ref m_NodeCollection);
  103.  
  104.            ppComponent = m_Component;
  105.         
  106.        }// CreateComponent
  107.  
  108.     //-------------------------------------------------
  109.     // Notify
  110.     //
  111.     // This notify function is much less complex than
  112.     // the notify function found in CComponent.
  113.     // Its main responsiblity is to insert data items into
  114.     // the console namespace when requested.
  115.     //-------------------------------------------------    
  116.     public void Notify(IDataObject lpDataObject, uint aevent, int arg, int param)
  117.     {
  118.  
  119.         CDO test= (CDO)lpDataObject;
  120.    
  121.         switch(aevent)
  122.         {
  123.             // If a data item is expanding, we need to tell MMC about it's children.
  124.             // Note, this case doesn't necessarily mean the data item is expanding
  125.             // visually.... MMC is just requesting information about it.
  126.             case MMCN.EXPAND:
  127.                 // See if we're expanding the item (as opposed to collapsing it)
  128.                 if (arg > 0)
  129.                     test.Node.InsertChildren(ref m_ConsoleNameSpace, param);
  130.                 break;
  131.                
  132.             default:
  133.                 // We don't support any other messages
  134.                 throw new ExternalException("CData::Notify", HRESULT.S_FALSE);
  135.         }
  136.     }// Notify
  137.  
  138.     //-------------------------------------------------
  139.     // Destroy
  140.     //
  141.     // This cleans up whatever needs to be cleaned up.
  142.     // Normally, this would be used to release any interfaces
  143.     // we had that traced back to the console. We can do this
  144.     // by removing any references we have to the interfaces...
  145.     //-------------------------------------------------
  146.     public void Destroy()
  147.     {
  148.         Marshal.ReleaseComObject(m_Console);
  149.         Marshal.ReleaseComObject(m_ConsoleNameSpace);
  150.  
  151.         m_Component = null;
  152.         m_NodeCollection = null;
  153.     }// Destroy
  154.  
  155.     //-------------------------------------------------
  156.     // QueryDataObject
  157.     //
  158.     // When MMC wants a data object for a specific cookie,
  159.     // this function will be called.
  160.     //-------------------------------------------------
  161.     public void QueryDataObject(int cookie, uint type, out IDataObject ppDataObject)
  162.     {
  163.         ppDataObject = new CDO(ref m_NodeCollection[cookie]); 
  164.     }// QueryDataObject
  165.  
  166.     //-------------------------------------------------
  167.     // GetDisplayInfo
  168.     //
  169.     // This function is called by MMC whenever it needs to
  170.     // display a node in the scope pane. 
  171.     //-------------------------------------------------
  172.     public void GetDisplayInfo(ref SCOPEDATAITEM sdi)
  173.     {
  174.         // First let's find this node we want info on....
  175.         CNode NodeWeWant = m_NodeCollection[(int)sdi.lParam];
  176.  
  177.         // See if they want the display name
  178.         if ((sdi.mask & SDI.STR) > 0)
  179.             sdi.displayname = Marshal.StringToCoTaskMemUni(NodeWeWant.DisplayName);
  180.  
  181.         // The snapin was set up so the cookie is the same
  182.         // value as the image index
  183.         if ((sdi.mask & SDI.IMAGE) > 0)
  184.             sdi.nImage = NodeWeWant.Cookie;
  185.  
  186.         // We're using the same image for the "open" image as
  187.         // we are the "closed" image
  188.         if ((sdi.mask & SDI.OPENIMAGE) > 0)
  189.             sdi.nOpenImage = NodeWeWant.Cookie;
  190.  
  191.         // We shouldn't need to set this.... but if we need to...
  192.         if ((sdi.mask & SDI.STATE) > 0)
  193.             sdi.nState = 0; 
  194.  
  195.         // If we're inquiring about children....
  196.         if ((sdi.mask & SDI.CHILDREN) > 0)
  197.             sdi.cChildren = NodeWeWant.NumChildren;
  198.  
  199.    }// GetDisplayInfo
  200.  
  201.    
  202.    //-------------------------------------------------
  203.    // CompareObjects
  204.    //
  205.    // This function will compare two data objects. In this
  206.    // snapin, if the cookies are identical for each data object,
  207.    // then the items are the same
  208.    //-------------------------------------------------
  209.    public void CompareObjects(IDataObject lpDataObjectA, IDataObject lpDataObjectB)
  210.    {
  211.         CDO doItem1, doItem2;
  212.         // These data items should be CDO's in disguise.....
  213.         doItem1 = (CDO)lpDataObjectA;
  214.         doItem2 = (CDO)lpDataObjectB;
  215.         
  216.         if (doItem1.Node.Cookie != doItem2.Node.Cookie)
  217.         {
  218.             // These are different objects. We need to return S_FALSE
  219.  
  220.             throw new ExternalException("CData::CompareObjects", HRESULT.S_FALSE);
  221.         }
  222.  
  223.         // else we return S_OK automatically
  224.     }// CompareObjects
  225.  
  226.     //----------------------------------------------------------
  227.     // Functions to implement ISnapinHelp2
  228.     //----------------------------------------------------------
  229.  
  230.     //-------------------------------------------------
  231.     // GetHelpTopic
  232.     //
  233.     // This function passes back the location and filename
  234.     // of a compiled help file
  235.     //-------------------------------------------------
  236.     public void GetHelpTopic(out int lpCompiledHelpFile)
  237.     {
  238.         // If this snapin doesn't have a helpfile, we need to
  239.         // return false
  240.         if (ThisSnapin.sHelpFile.Equals(""))
  241.         {
  242.             ExternalException e = new ExternalException("CData::GetHelpTopic", HRESULT.S_FALSE);
  243.             throw e;
  244.         }
  245.  
  246.         // We do have a help file. Let's pass it back.
  247.         lpCompiledHelpFile = Marshal.StringToCoTaskMemUni(ThisSnapin.sHelpFile);
  248.     }// GetHelpTopic
  249.  
  250.     //-------------------------------------------------
  251.     // GetLinkedTopics
  252.     //
  253.     // We'd use this function if we had topics that were
  254.     // not located in our primary help file. 
  255.     //-------------------------------------------------
  256.     public void GetLinkedTopics(out int lpCompiledHelpFiles)
  257.     {
  258.         // We don't link to other Help files, so we throw false
  259.         throw new ExternalException("CData::GetLinkedTopics", HRESULT.S_FALSE);
  260.     }// GetLinkedTopics
  261.  
  262.     //-------------------------------------------------------
  263.     // Stuff for IExtendPropertySheet
  264.     //-------------------------------------------------------
  265.  
  266.     //-------------------------------------------------
  267.     // CreatePropertyPages
  268.     //
  269.     // MMC calls this function when it wants a property
  270.     // page for a specified node.
  271.     //-------------------------------------------------
  272.     public void CreatePropertyPages(IPropertySheetCallback lpProvider, int handle, IDataObject lpIDataObject)
  273.     {
  274.  
  275.         // This is really a CDO in disguise
  276.         CDO victim = (CDO)lpIDataObject;
  277.        
  278.         // Let's see if this node has property pages
  279.         if (victim.Node.HavePropertyPages)
  280.            victim.Node.CreatePropertyPages(lpProvider, handle);
  281.  
  282.         // We don't have any property pages, let's return false
  283.         else
  284.             throw new ExternalException("CData::CreatePropertyPages", HRESULT.S_FALSE);
  285.         
  286.     }// CreatePropertyPages
  287.  
  288.     //-------------------------------------------------
  289.     // QueryPagesFor
  290.     //
  291.     // MMC calls this function once at the beginning when
  292.     // everything is being initialized, asking if we have any
  293.     // property pages. If we tell it we don't, we can never
  294.     // have any property pages in our snapin. (It will never
  295.     // call CreatePropertyPages)
  296.     //-------------------------------------------------
  297.     public void QueryPagesFor(IDataObject lpDataObject)
  298.     {
  299.  
  300.         // This snapin does have property pages, so we should return S_OK
  301.         // (which will happen automatically). If the snapin didn't have
  302.         // any property pages, then we should return S_FALSE
  303.         // 
  304.         // throw new ExternalException("", HRESULT.S_FALSE);
  305.         //
  306.     }// QueryPagesFor
  307.  
  308.     //-------------------------------------------------------
  309.     // Functions that are not a part of any interface
  310.     //-------------------------------------------------------
  311.  
  312.  
  313.     //-------------------------------------------------
  314.     // CreateNodes
  315.     //
  316.     // This ugly function creates the node info structs
  317.     // that are used to create nodes for this snapin
  318.     //-------------------------------------------------
  319.     private NodeInfo[] CreateNodes()
  320.     {
  321.         NodeInfo[] nodes = new NodeInfo[11];
  322.  
  323.         CData cData = this;
  324.  
  325.         // Set up root node
  326.         nodes[0] = new NodeInfo();
  327.         nodes[0].sGuid      = "123456789012345";
  328.         nodes[0].sName      = "Team Sites";
  329.         nodes[0].sHelpLink  = null;
  330.         nodes[0].oResults   = null;
  331.         nodes[0].sIcon      = "IDI_MAIN";
  332.         nodes[0].ppaPages   = null;
  333.         nodes[0].iaChildren = new int[] {1,2};
  334.  
  335.         // Set up the North
  336.         nodes[1] = new NodeInfo();
  337.         nodes[1].sGuid      = "123456789012345";
  338.         nodes[1].sName      = "North";
  339.         nodes[1].sHelpLink  = "snapsamp.chm::/step_one.htm";
  340.         nodes[1].oResults  = null;
  341.         nodes[1].sIcon      = "IDI_WHITE";
  342.         nodes[1].ppaPages   = null;
  343.         nodes[1].iaChildren = new int[] {3,4,5,6};
  344.  
  345.         // Set up the South
  346.         nodes[2] = new NodeInfo();
  347.         nodes[2].sGuid      = "123456789012345";
  348.         nodes[2].sName      = "South";
  349.         nodes[2].sHelpLink  = "snapsamp.chm::/step_two.htm";
  350.         nodes[2].oResults  = (Object)(new CDivisionResults());
  351.         nodes[2].sIcon      = "IDI_WHITE";
  352.         nodes[2].ppaPages   = null;
  353.         nodes[2].iaChildren = null;
  354.  
  355.         // Set up the Blacks
  356.         nodes[3] = new NodeInfo();
  357.         nodes[3].sGuid      = "123456789012345";
  358.         nodes[3].sName      = "Black";
  359.         nodes[3].sHelpLink  = null;
  360.         nodes[3].oResults  = new String[] {ThisSnapin.sBaseDir + "black1.html", ThisSnapin.sBaseDir + "black2.html"};
  361.         nodes[3].sIcon      = "IDI_BLACK";
  362.         nodes[3].ppaPages   = new IPropSheetPage[1] {new CTeamPropPage()};
  363.         nodes[3].iaChildren = null;
  364.  
  365.         // Set up the Blues
  366.         nodes[4] = new NodeInfo();
  367.         nodes[4].sGuid      = "123456789012345";
  368.         nodes[4].sName      = "Blue";
  369.         nodes[4].sHelpLink  = null;
  370.         nodes[4].oResults  = new String[] {ThisSnapin.sBaseDir + "blue1.html", ThisSnapin.sBaseDir + "blue2.html"};
  371.         nodes[4].sIcon      = "IDI_BLUE";
  372.         nodes[4].ppaPages   = new IPropSheetPage[1] {new CTeamPropPage()};
  373.         nodes[4].iaChildren = null;
  374.  
  375.         // Set up the Cyans
  376.         nodes[5] = new NodeInfo();
  377.         nodes[5].sGuid      = "123456789012345";
  378.         nodes[5].sName      = "Cyan";
  379.         nodes[5].sHelpLink  = null;
  380.         nodes[5].oResults   = new String[] {ThisSnapin.sBaseDir + "cyan1.html", ThisSnapin.sBaseDir + "cyan2.html"};
  381.         nodes[5].sIcon      = "IDI_CYAN";
  382.         nodes[5].ppaPages   = new IPropSheetPage[1] {new CTeamPropPage()};
  383.         nodes[5].iaChildren = null;
  384.  
  385.         // Set up the Greens
  386.         nodes[6] = new NodeInfo();
  387.         nodes[6].sGuid      = "123456789012345";
  388.         nodes[6].sName      = "Green";
  389.         nodes[6].sHelpLink  = null;
  390.         nodes[6].oResults   = new String[] {ThisSnapin.sBaseDir + "green1.html", ThisSnapin.sBaseDir + "green2.html"};
  391.         nodes[6].sIcon      = "IDI_GREEN";
  392.         nodes[6].ppaPages   = new IPropSheetPage[1] {new CTeamPropPage()};
  393.         nodes[6].iaChildren = null;
  394.  
  395.         return nodes;
  396.  
  397.  
  398.     }// CreateNodes
  399.  
  400. }// class CData
  401.  
  402. }// namespace Microsoft.SampleMMC
  403.