首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# Com 跟Com+

2011-12-14 
C# Com 和Com+我想问的问题1、C# 怎么写Com组件(不是Com+),然后怎么注册呢,怎么在C#中再去调用这个Com组件2

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");                 //                 //Get the Applications List in the Component Services.                 COMAdmin.ICatalogCollection appList = (COMAdmin.COMAdminCatalogCollection)cac.GetCollection("Applications");                 //Get a new Application from the Add() Method.                 COMAdmin.ICatalogObject caco = (COMAdmin.ICatalogObject)appList.Add();                 //Set the properties of the Application.                 caco.set_Value("Name", "My_Functions");                 caco.set_Value("Description", "The Function DLL contrains two Components, one is for user level and the other one is for Administrator level");                 //save changes to the applications list.                 appList.SaveChanges();                 //                 //Install the Component.                 //I don't know why, Microsoft says that MyFunctions.tlb should be the MyFunctions.dll file.                 //but it doesn't work for me, I have to put MyFunctions.tlb and MyFunctions.dll in the same folder, then                 //install the MyFunctions.tlb file.                 cac.InstallComponent("My_Functions", "MyFunctions.tlb", "", "");                 //                 //                 //                 //get the Roles collection from the new COM+ Application.                 COMAdmin.ICatalogCollection roleList = (COMAdmin.COMAdminCatalogCollection)appList.GetCollection("Roles", caco.Key);                 //                 //set up two new roles.                 COMAdmin.ICatalogObject user = (COMAdmin.ICatalogObject)roleList.Add();                 COMAdmin.ICatalogObject admin = (COMAdmin.ICatalogObject)roleList.Add();                 //set properties of these two roles.                 admin.set_Value("Name", "Admin");                 user.set_Value("Name", "User");                 //                 //save changes to the roles collection.                 roleList.SaveChanges();                 //Get the Component Collection of the new COM+ application object.                 COMAdmin.ICatalogCollection comList = (COMAdmin.COMAdminCatalogCollection)appList.GetCollection("Components", caco.Key);                 //populate the collection.                 //this is a must action.                 //to get all the objects inside the Component Collection.                 comList.Populate();                 //                 //loop through the collection to set some properties.                 foreach (COMAdmin.ICatalogObject tmpAdmin in comList)                 {                     if (tmpAdmin.Name.ToString().ToLower() == "my_functions")                     {                         tmpAdmin.set_Value("ComponentAccessChecksEnabled", true);                         comList.SaveChanges();                         //                         COMAdmin.ICatalogCollection adminList = (COMAdmin.COMAdminCatalogCollection)comList.GetCollection("RolesForComponent", tmpAdmin.Key);                         COMAdmin.ICatalogObject userObject = (COMAdmin.ICatalogObject)adminList.Add();                         userObject.set_Value("Name", "User");                         adminList.SaveChanges();                         //                         break;                     }                 }                 //                 //                 //set up some properties.                 caco.set_Value("ApplicationAccessChecksEnabled", true);                 caco.set_Value("AccessChecksLevel", COMAdmin.COMAdminAccessChecksLevelOptions.COMAdminAccessChecksApplicationComponentLevel);                 appList.SaveChanges();                                 //Export this COM+ application to the MSI excutable file.                 //optional.                 cac.ExportApplication("My_Functions", @"D:\temp\MyFunctions.msi", 4);                 //cac.InstallComponent("My_Functions", "MyFunctions.tlb", "", "");                             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }         } 


[解决办法]

探讨
注册只需要使用
regsvr32这个命令即可,使用代码手段一般是由于应用的特殊需要的。

[解决办法]
探讨
楼上的那你的意思是
.net创建的COM组件不能在.net中用吗?
只能在C++之类的用?
那Com+呢,建立了COm+后又怎么使用呢

[解决办法]
探讨
引用:
首先,如果你要在C#使用使用你的.NET COM.那么直接引用你那个NET 程序集即可,不能引用COM。
如果你要确定你的NET COM 是否有效,那么在浏览器地址栏输入:

JScript codenew ActiveXObject("Lib.Class")


这样又起到什么作用呢,难道只是在js中用吗

热点排行