Click here to Skip to main content
15,895,777 members
Articles / Programming Languages / C#

CIMTool for Windows Management Instrumentation - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
22 Feb 2013CPOL6 min read 37.3K   7.6K   31  
Use WMI to retrieve information about your system and genrate classes for easy WMI development.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AvalonDock.Layout;
using Harlinn.CIMTool.Wpf.Types;
using Harlinn.CIMTool.Wpf.Controls;
using Harlinn.CIMTool.Types;
using Harlinn.CIMTool.CodeGenerator;
using System.Windows.Controls.WpfPropertyGrid;
using Harlinn.CIMTool.Wpf.Forms;

namespace Harlinn.CIMTool.Wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static MainWindow instance;
        public MainWindow()
        {
            InitializeComponent();
            instance = this;
        }

        public static MainWindow Instance
        {
            get
            {
                return instance;
            }
        }

        Types.CIMTool Data
        {
            get
            {
                return (Types.CIMTool)this.DataContext;
            }
        }

        public BaseElement SelectedElement
        {
            get
            {
                return propertyGrid.SelectedObject as BaseElement;
            }
            set
            {
                if (propertyGrid.SelectedObject != value)
                {
                    MetadataRepository.Clear();
                    propertyGrid.SelectedObject = value;
                }
            }
        }


        private void TreeView_SelectedItemChanged_1(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            Data.SelectedItem = e.NewValue as NodeBase;
            propertyGrid.SelectedObject = e.NewValue;
        }

        private void NewQueryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (Data.SelectedItem is ClassNode)
            {
                ClassNode classNode = (ClassNode)Data.SelectedItem;
                LayoutDocument layoutDocument = new LayoutDocument();
                layoutDocument.Title = "Query: " + classNode.Name;
                QueryControl queryControl = new QueryControl();
                queryControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                queryControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                queryControl.Scope = classNode.Parent.ManagementScope;
                queryControl.QueryString = "select * from " + classNode.Name;

                layoutDocument.Content = queryControl;


                layoutDocumentPane.Children.Add(layoutDocument);
            }
        }

        private void GenerateCodeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            

            
            ManagementScopeWrapper theManagementScope = null;
            if (Data.SelectedItem is ClassNode)
            {
                ClassNode classNode = (ClassNode)Data.SelectedItem;
                theManagementScope = classNode.Parent.ManagementScope;
            }
            else if (Data.SelectedItem is ClassesNode)
            {
                ClassesNode classesNode = (ClassesNode)Data.SelectedItem;
                theManagementScope = classesNode.ManagementScope;
            }
            else if (Data.SelectedItem is NamespaceNode)
            {
                NamespaceNode namespaceNode = (NamespaceNode)Data.SelectedItem;
                theManagementScope = namespaceNode.ManagementScope;
            }

            if (theManagementScope != null)
            {

                GeneratorOptions generatorOptions = new GeneratorOptions();
                generatorOptions.RootManagementNamespace = "Harlinn.CIMTool.Generated.Mgmt";
                generatorOptions.ManagementNamespace = theManagementScope.Name;
                generatorOptions.ManagementPrefix = "IIs";
                generatorOptions.RootSerializationNamespace = "Harlinn.CIMTool.Generated.Mgmt." + theManagementScope.Name;
                generatorOptions.SerializationNamespace = "Types";
                generatorOptions.SerializationPrefix = "IIsS";

                GeneratorOptionsWindow generatorOptionsWindow = new GeneratorOptionsWindow();
                generatorOptionsWindow.DataContext = generatorOptions;

                bool? result = generatorOptionsWindow.ShowDialog();

                if (true == result)
                {

                    CodeProvider codeProvider = new CodeProvider(CodeLanguage.CSharp, generatorOptions);
                    using (codeProvider)
                    {
                        //codeProvider.Prefix = "IIS6";
                        //codeProvider.PrefixReplacements.Add("CIM", "Cim");
                        //codeProvider.PrefixReplacements.Add("MSFT", "Microsoft");

                        Namespace nameSpace = new Namespace(codeProvider, theManagementScope);
                        nameSpace.GenerateManagementCode();
                        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                        using (memoryStream)
                        {
                            System.IO.TextWriter textWriter = new System.IO.StreamWriter(memoryStream);
                            using (textWriter)
                            {
                                System.CodeDom.Compiler.CodeGeneratorOptions codeGeneratorOptions = new System.CodeDom.Compiler.CodeGeneratorOptions();
                                codeGeneratorOptions.BracingStyle = "C";
                                codeProvider.CodeDomProvider.GenerateCodeFromNamespace(nameSpace.ManagementCodeNamespace, textWriter, codeGeneratorOptions);
                                textWriter.Flush();

                                LayoutDocument layoutDocument = new LayoutDocument();
                                layoutDocument.Title = "Scope: " + theManagementScope.Name;
                                EditorControl editorControl = new EditorControl();
                                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                                editorControl.LoadFromSream(memoryStream);
                                editorControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                                editorControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

                                layoutDocument.Content = editorControl;

                                layoutDocumentPane.Children.Add(layoutDocument);
                            }
                        }

                    }
                }
            }

        }

        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions