zoukankan      html  css  js  c++  java
  • COMAdmin 管理COM+ C# 抄自 CODEGURU 很多未知,整理一下

    抄自

    http://www.codeguru.com/cpp/com-tech/complus/article.php/c3943/COM-Automation-Using-COMAdminCatalog-in-NET-C.htm

    但是发现很多未知引用,只是整理了一下作参考

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.Specialized;
    using COMAdmin;

    namespace updatebh
    {
        class Comadminclass
        {

            private COMAdminCatalog objAdmin;
            private object objRoot;
            private COMAdminCatalogCollection objCollection ;
            private NameValueCollection collection;
            public delegate void DeleteEventHandler();
            public delegate void ErrorMessageHandler(string s, Exception e);
     
     
     //Events
     /// <summary>Raised when the object is successfully deleted on
     /// the remote host.</summary>
     public event DeleteEventHandler Delete;
     /// <summary>Raised when the object throws an exception.</summary>
     public event ErrorMessageHandler Error;
     
     //Method(s) to invoke the event
     protected virtual void OnDelete()
     {
     
        if(Delete != null)
        Delete();
     }
     
     //#region Get COM + Applications
     // Get COM + Applications
     
        internal COMAdminCatalogCollection GetCollection()
        {
            try
            {
                objAdmin = new COMAdmin.COMAdminCatalog();
                objRoot = objAdmin.Connect("localhost");
                objCollection = (COMAdmin.COMAdminCatalogCollection)objAdmin.GetCollection("Applications");
            }
            catch (Exception ex)
            {

                System.Windows.Forms.MessageBox.Show("Error : " + ex);
            }
         return objCollection;
        }

        public NameValueCollection GetCOMApplications()
        {
            collection = new NameValueCollection();
            try
            {
                objCollection = GetCollection();
                objCollection.Populate();

                foreach (COMAdmin.COMAdminCatalogObject objAppNames
                in objCollection)
                {
                    COMAdmin.ICatalogCollection objComponents =
                    (COMAdmin.ICatalogCollection)objCollection.
                    GetCollection("Components", objAppNames.Key);
                    objComponents.Populate();
                    foreach (COMAdmin.COMAdminCatalogObject Components
                    in objComponents)
                    {
                        collection.Add(Components.Name.ToString(),
                        objAppNames.Name.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Error += new ErrorMessageHandler(Message);
                Error("GetCOMAPPlications", e);
                Dispose();
            }
            return collection;
        }


        internal void Message(string message, Exception e)
        {
            System.Windows.Forms.MessageBox.Show("Error occurred " +
            message + "error description : " + e, "Nexsure COM + ", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }

        public void DeleteCOMApplication(string appName)
        {
            try
            {
                long l_Count = 0;
                ICatalogCollection pCollection = GetCollection();
                ICatalogObject pCatalog;
                pCollection.Populate();
                l_Count = pCollection.Count;
                if (l_Count == 0)
                    return;

                for (int i = 0; i < l_Count; i++)
                {
                    pCatalog = (ICatalogObject)pCollection.get_Item(i);
                    if (appName == (string)pCollection.get_Value("Name"))
                    {
                        pCollection.Remove(i);
                        pCollection.SaveChanges();
                        OnDelete();
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Error += new ErrorMessageHandler(Message);
                Error("Unable to delete the COM+ Application:", e);
                Dispose();
            }
        }

        public void CreateCOMApplication()
        {
            bool Exists = false;
            try
            {
                Exists = ApplicationExists(); // Method to check whether
                // the application exists
                // in the Catalog
                if (!Exists)
                {
                    objCollection = GetCollection();
                    COMAdmin.COMAdminCatalogObject objObject = (COMAdmin.COMAdminCatalogObject)objCollection.Add();
                    objObject.set_Value("Name", props.COM_Name);
                    objObject.set_Value("Description", props.COM_Description);
                    objObject.set_Value("Activation", props.COM_Type);
                    objObject.set_Value("Identity", props.COM_User);
                    objCollection.SaveChanges();
                    OnCreate();
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("An application with the same name already exixts in the Registry!", "Nexsure COM + Help", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                Error += new ErrorMessageHandler(Message);
                Error("Error creating COM+ Application :", ex);
                Dispose();
            }
        }

      public bool StartCOMApplication()
        {
              bool Startup = false;
          try
            {
              objCollection = GetCollection(); // Method returns
               // COM+ Applications
                objAdmin.StartApplication(applicationName);
                System.Windows.Forms.MessageBox.Show("Application was successfully started.");
                Startup = true;
              }
         catch
            {
                 System.Windows.Forms.MessageBox.Show("Unable to start the application.");
            }
     return Startup;
      }

     

     public bool ShutDownCOMApplication()
     {
          bool ShuttingDown = false;
          try
          {
              objCollection = GetCollection();
              objAdmin.ShutdownApplication(applicationName);
              System.Windows.Forms.MessageBox.Show("Application was successfully shut down.");
              ShuttingDown = true;
          }
          catch
          {
              System.Windows.Forms.MessageBox.Show("Unable to shut down the application.");
          }
          return ShuttingDown;
     }






        }
    }

  • 相关阅读:
    SQL 基础技能提升
    科技领域的assert和deassert的含义
    PDF调出本来存在的书签面板
    什么是事务( Transaction )?
    verilog 和systemverilog的Timing Check Tasks
    PT静态时序分析的三种模式
    useful systemverilog system tasks
    Accellera举措可能导致Verilog标准分化
    验证方法学的发展历程及比较
    PrimeTime 时序分析流程和方法(ZZ)
  • 原文地址:https://www.cnblogs.com/redmondfan/p/2986720.html
Copyright © 2011-2022 走看看