如题之前一直没听过,但最近听一些人说能写,不知这里有没有人用C#写过呢?

解决方案 »

  1.   

    你的类需要继承自ServicedComponent这个基类(在程序集System.EnterpriseServices里,你需要Add Reference)然后给你的dll做一个强命名,然后注册到GAC里,然后就可以注册到COM管理器里,剩下的设置就和以前写的COM组件的设置操作都一样的,使用也一样了更具体的你可以去Google,网上很多文章介绍
      

  2.   

    //-------------------------------------------------------------
    //COM 的例子
    //-------------------------------------------------------------
    [GuidAttribute("85E45398-B4BA-440e-9343-1E665E2AFCD8"),ComVisible(true)]
    public interface IFormDlg
    {
      [DispId(1)]bool DoModel();
      [DispId(2)]void CloseDialog();
      [DispId(3)]bool DoModeless();
      [DispId(4)]bool IsShown{get;}
    }[ComVisible(true), GuidAttribute("D55CE299-16A3-4dd7-8530-406708FD9908"),ProgId("SureyorUIClassLibrary.ThreeButtonsDlg")]
    public class COMFormDlg:IFormDlg
    {
      private bool _isShown = false;
      private Thread _dlgThread;
      private static object _locker;
      //............
      //...........
      public COMFormDlg(){}
      public COMFormDlg(string title, string msg)
      {
        //...................
      }
      public bool IsShown{get{return _isShown;}}
      //
      [ComRegisterFunctionAttribute]
      public static void RegisterFunction(Type t)
      {
        //代码.....
      }
      [ComUnregisterFunctionAttribute]
      public static void UnregisterFunction(Type t)
      {
       //代码......
      }
      bool DoModel(){return true;}
      void CloseDialog(){throw new Exception("lsdkfsld");}
      bool DoModeless(){return true;}
    }
      

  3.   

    //-------------------------------------------------------------
    //COM+ 的例子 和COM是不同的
    //-------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.EnterpriseServices;
    using System.Runtime.InteropServices;namespace MyFunctions
    {    
        
          
        [InterfaceType(ComInterfaceType.InterfaceIsDual)]
        [ComVisible(true),GuidAttribute("169ABC84-9616-4af8-8068-7117A3BD4035")] 
        public interface myInterFace
        {
             void RunCalc();
             void RunWindowsExplorer();
             int GetRandom(int min,int max);
        }    [ProgId("My_Functions")]
        [ComVisible(true),GuidAttribute("5D7FED5B-20EC-4169-B0BC-EC3C2AB5D973")]
        public class Functions:ServicedComponent, myInterFace
        {        public  void RunWindowsExplorer()
            {
                try
                {
                    System.Diagnostics.Process.Start("Explorer.exe");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }        public static bool RunCalc()
            {
                try
                {
                    System.Diagnostics.Process.Start("calc.exe");
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                //return false;
            }
            public int GetRandom(int min,int max)
            {
              return int.MaxValue;
              
            }
        }
    }