我想问的问题
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{
            
           
        }
请各位大侠看看,谢谢
    

解决方案 »

  1.   

    呵呵,和使用其他类没有区别,唯一的不同就是要用 regsvc32.exe(好像是这么写的)注册到操作系统的com+里面去
      

  2.   

    利用 COM Admin 1.0 来对我的dll进行注册到COM+ table. 
    //========================================================================================================== 
            // 
            // 
            //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); 
                } 
            }
      

  3.   

    能否发一份资料给小弟
    [email protected]
      

  4.   

    我想问下我创建了Com+ 也产生了客户端安装程序
    在组件服务中也能看见
    但是下一步我怎么用呢?在本地上和远程上怎么用呢有人说创建一个程序把这个Dll文件引进来,这个当然能用了,那这样创建Com+的用处是什么呢
    有人说向远程那样使用就可,但是远程它开放了个端口的啊,客户端连接那边的 协议+ip+端口的啊
      

  5.   

    还有C#创建的Com是拿给C++,vb用的吗?我再用C#来用行吗?如果行,又怎么用呢
      

  6.   

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

  7.   


    当然可以,在工程里面添加引用,在COM选项卡里面找到你的ProgID
    不过,都是C#的,直接引用可能更好,没必要走一次COM的Wrap=============================================
    安装你在顶楼写的代码,给程序加上签名以后(sn.exe生成的snk文件,不是数字签名)
    先把你的dll加到gac里面
    gacutil /i 你的dll路径然后注册到系统(写入注册表)
    regasm 你的DLL路径
      

  8.   


    regsvr32在这里不好用 :D
      

  9.   

    还有,你的COM里面最好用
    [ProgId("MyLib.MyClass")]
    这样的属性指定一个名字,方便调用在客户端里面,添加引用以后,会出现一个 AxMyLib的命名空间AxMyLib.MyClass o=new AxMyLib.MyClass();这样创建对象最后,由于是经过包装的COM组件,你最好手动释放资源
      

  10.   

    楼上的说的,我试试
    但是还有个问题我想问下我创建了Com+ 也产生了客户端安装程序 
    在组件服务中也能看见 
    在远程上怎么用呢 怎么释放COM的资源的,非常感谢
      

  11.   

    用regasm 注册产生的是个.tlb文件
    在Com引用中也能看见,但是引用的话会报错,说ActiveX 类型库是从.net程序集中导出的 ,无法将其作为引用增加而用另外一个命令 regsvcs 命令就根本看不见 
      

  12.   

    .net写的COM,不在注重释放资源(如果你本身没有使用一些资源的话),直接设置为null就行了.
    COM+客户端也需要引用dll的,COM+是适用分布式环境的.如果你的服务端和客户端都是.net,那就用remoting,web service,wcf...等等
      

  13.   

    首先,如果你要在C#使用使用你的.NET COM.那么直接引用你那个NET 程序集即可,不能引用COM。
    如果你要确定你的NET COM 是否有效,那么在浏览器地址栏输入:
    new ActiveXObject("Lib.Class")
      

  14.   

    是输入:
    JavaScript: new ActiveXObject("Lib.Class");
      

  15.   

    如果COM组件不能创建就会报错
      

  16.   

    c#能写com组件.写是能写的.
    但是没必要..
      

  17.   


    这个好像是不行的.
    c#写com,只要写一个Class Library,生成dll后,
    regasm  xxx.dll   注册是用regasm但是用c#来写com实在是没有必要-_-!
      

  18.   

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

  19.   

    当然可以用.
    com是肯定可以在.net中用的.我说的没必要不是说不能用.
      

  20.   


    请问怎么用呢?我引入这组件就报错
    说ActiveX 类型库是从.net程序集中导出的 ,无法将其作为引用增加 
      

  21.   

    能说下怎么用吗?
    ========================
    com组件添加引用即可.
    com+的
    Regsvcs注册后
    引用类
    使用..被Regsvcs注册的dll必须先进行强名称,也就是说com+组件必须要强名称后才可以详细的你可以查一下msdn,查一下Regsvcs比如
    下面的命令将 myTest.dll 中包含的所有公共类添加到 myTargetApp(一个现有的 COM+ 1.0 应用程序)中,同时产生 myTest.tlb 类型库。regsvcs /appname:myTargetApp myTest.dll
    下面的命令将 myTest.dll 中包含的所有公共类添加到 myTargetApp(一个现有的 COM+ 1.0 应用程序)中,同时产生 newTest.tlb 类型库。regsvcs /appname:myTargetApp /tlb:newTest.tlb myTest.dll
    对于com+这块的内容挺多的.也比较难.
    你多查一些资料吧.他的编写,部署之类的...
    多查查msdn
      

  22.   


    用来测试你的NET COM是否可以被创建啊
      

  23.   

    呵呵..想在C#中引用NET COM?? 我看你怎么引用
    我在23楼说的很清楚,不能引用NET COM
      

  24.   

    现在有这样个问题
    加了强名,也使COM可见了,生成了TLB,也注册了,也添加进缓存了,
    然后我新建WINFORM工程,添加引用,在COM里找到我的COM(testcom)引用的时候报错: 未能添加对“testCom"的引用。ActiveX类型库"....(省略了我TLB的地址)"是从。NET程序集导出的,无法将其作为引用添加。请改为添加对.net程序集的引用。 请问怎么解决呢
    谢谢了
      

  25.   

    你怎么这么不开窍啊?都说在C#中不能引用NET COM,直接引用你那个DLL就可以了
      

  26.   


    老大,那怎么用这个Com呢,引用Com+也发生这样的事情
    我想用Com+做个服务器端,客户端去连接它,它和客户端之间进行通讯