日期:2014-05-18  浏览次数:21349 次

C# Com 和Com+

我想问的问题
1、C# 怎么写Com组件(不是Com+),然后怎么注册呢,怎么在C#中再去调用这个Com组件
2、这是我写的代码
  类库
  [ComVisible(true),
  Guid("50C780E0-74FC-41d3-9BDB-7674DFDCC5A3"))
  ]
  public interface IBanlance
  {
  string Name
  {
  get;
  }
  Stream Read();
  DateTime GetTime();
  }

  [ComVisible(true),
  Guid("93BA6E50-599E-4f58-BBF7-D6BED7935C43")
  ]
  public class Banlance : ServicedComponent, IBanlance {
  public Banlance() { 
   
  }
  public string Name {
  get {
  return "布拉本天平";
  }
  }
  public Stream Read() {
  Stream s = null;
  try
  {
  s = File.OpenRead(@"D:\111.txt");
   
  }
  catch (Exception e) {
  Console.WriteLine(e.Message+"=="+e.StackTrace);
  }
  return s;
  }
  public DateTime GetTime() {
  return DateTime.Now;
  }
  然后产生了个密匙


  在Com+组件里有这个组件,注册表也有这两个Guid


  客户端
  static void Main(string[] args)
  {
  try
  {
   
  new A();
  //在这个地方报错
在 ComAddTest.Program.Main(String[] args) 位置 D:\study\ComAddTest\ComAddTest
\Program.cs:行号 27===无法将类型为“ComAddComponent.Banlance”的对象强制转换为类
型“A”。
  }
  catch (Exception e) {
  Console.WriteLine(e.StackTrace+"==="+e.Message);
  }
   
  Console.ReadLine();
  }
   
  [Guid("50C780E0-74FC-41d3-9BDB-7674DFDCC5A3"),
  InterfaceType(ComInterfaceType.InterfaceIsDual)]
  public interface IBanlance{
  string Name{
  get;
  }
  Stream Read();
  DateTime GetTime();
  }
  [ComImport,Guid("93BA6E50-599E-4f58-BBF7-D6BED7935C43")]
  public class A{
   
   
  }


请各位大侠看看,谢谢
 

------解决方案--------------------
利用 COM Admin 1.0 来对我的dll进行注册到COM+ table. 

C# code

//========================================================================================================== 
        // 
        // 
        //This method is to create a COM+ application through C# coding. 
        //including Security settings. 
        //you also can do it by go through the Component Services program under Administration Tools. 
        private void load() 
        { 
            try 
            { 
                //The ICOMAdminCatalog2 is the COM Admin Library 1.5 which has more functions than the older version. 
                //The ICOMAdminCatalog is the COM Admin Library 1.0 
                COMAdmin.ICOMAdminCatalog2 cac = new COMAdmin.COMAdminCatalogClass(); 
                
                //Connect to the Server COM+ Services. 
                object root = cac.Connect("127.0.0.1"); 
                // 
                //