转载: .Net下与传统COM和ActiveX的交互

转载自:Top的color Net
.Net

下与传统 COMActiveX 的交互(原理与实践)

 

概要:本文主要说明在.netC#)中是如何也传统的COMActiveX进行交互的。其中包括:

1、             如何在.net中使用传统的COMActiveX

2、             反之,如何让其它应用程序以传统的COMActiveX方式访问.net对象?也就是如何将.net对象以COMActiveX的形式暴露给操作系统。

3、             一些开发概要和示例,以及开发示例代码。代码只做演示与测试,不涉及细节问题。

 

0部份:COMActiveX的简介

我不是COM高手,也不是ActiveX能人,对它们只是了解皮毛,所以为了防止读者对文章中的一些说明出现理解的偏差,有必要先说明一下本人对COMActiveX的理解。简单明了,COMActiveX都是不同的应用程序可以公用的组件。COM没有界面,而ActiveX是有界面的。ActiveX是特殊的COM。因此,COM所具有的功能,ActiveX都有,而ActiveX有更强大的功能及用户交界界面。本文不涉及COMActiveX的实现细节问题,主要是介绍把.net对象经COM的形式暴露给OS及其它应用程序的原理以及一些实现细节,而并非COM的开发细节。

COM是以组件的形式发布到目标机器上的,它可以被多个不同的应用程序公用。它是由操作系统的一个COM运行环境管理的(个人理解)。类似于.net运行环境,COM的运行环境负责整个COM的运行,从产生到结束。由于COMMSwindows上较早的一个软件开发模式,而且COM是基于二进制的组件,所以目前基本上所有的windows平台上都可以使用COM,也就是不用担心你开发的COM不能在其它机器上使用了。(再次说明,完全是个人理解)

既然是操作系统管理COM的,当你开发一个COM时,必须让OS知道你开发的是什么COM,如何使用等一些必须的信息。相关的有关于COMGUID,注册等一些 常见的知识。这里就不多说了。

我们所关心的问题之一是:当我的机上可以正常的使用COM时,如何让.net平台来使用传统的COM,当然也包括ActiveX.

 

 

1部份:如何在.net(VS2003)下引用传统的COMActiveX

这是一个很简单的问题,想必很多读者都使用过,而我也不准备在这块上说很多。你可以通过引用,然后找到COM的注册文件,直接添加一个引用,然后像使用.net的对象一样使用经过引用后的COM。如果它是一个ActiveX,你还可以把它添加到工具栏上。当然,你的目标对象必须是正确的COM或者ActiveX

那么经过引用后,它是如何工作的呢?其实我想讨论的,也就是传统的COM是如何在.net下工作的。看下面的图示(载自MSDN

 

RCW.JPG
解释一下:

1、              首先就是把COM封装成程序集中的元数据,也就是.net可以使用的数据。VS2003给我们提供的工具是Tlibmp.exe,当你在引用COM时,VS2003就是用这个工具帮助我们把COM封装成了一个.net下可以用的元数据。也就是我们引用后,项目目录里会生成一个DLL文件,而它,就是.net可以使用的元数据。

2、              而在运行时,RCW会为我们处理一些细节问题,然后通过Interop来调用,你就像使用.net对象一样的使用COM

我要说明的是:经过Tlbimp.exe封装后的元数据,只是在.net项目开发时使用的,运行时,真正的COM还是要在目标机器上安装注册。下面的一篇文章很有参考价值,读者可以深入的理解一下,.net是如何使用COM的。

http://www.microsoft.com/china/msdn/archives/library/dndotnet/html/bridge.asp 

  下面就是我们所关心的很二个问题,如何以COM的形式来访问.net对象?

 

2部份,如何让其它应用程序以传统的COM方法来访问.net对象(原理)

请注意这里的措词,.net是不能开发传统的COM的。因此,一些关于.net开发COM以及ActiveX的说明都是不完全正确的。它的本质是把.net的对象,通过一些封装,然后暴露给操作系统,让其实的应用程序可以像传统的COM方式那样来访问.net对象,而访问该对象的同时,因为该对象只能在.net下生成,所以运行环境还是要.net支持的。

先看一下MSDN上的一个图示。

 

CCW.JPG
解释一下:

1.             托管代码经编译成中间语言,想必大家都明白。而让其它应用程序也可以访问你所编译的DLL文件,却有很多不同的方法。这里,就是一种,以COM的形式暴露给OS

2.             这是技术核心,通过Tlbxp.exe工具,把DLL文件导出成组件(COM组件)类型库,也就是:把中间语言可以使用的DLL文件,通过工具,导出成COM可以使用的数据。

3.             注册,还有一个工具regasm.exe,可以把DLLOS(注册表)以COM的形式注册对象。这样OS里的COM运行环境就知道系统里有一个对象,可以用GUID来标识并生成和使用。

4.             以COM的方式实例化.net对象,先是应用程序向COM运行环境请求,要以生成一个对象(GUID标识)。COM运行环境通过注册信息找到COM类型数据库,然后通过.netCCW(COM Callable Wrapper)COM可调用包装)来访问.net下的DLL文件,然后生成.net对象,再返回给COM运行环境。而COM运行环境再以COM的形式把.net对象返回给应用程序。然后应用程序就像访问COM一样的来访问.net对象。

因此,.net不能开发正真的COM,只是对.net对象进行一个封装,然后以COM的形式暴露给OS,让OS里的其它应用程序也可以像访问COM那样来访问.net对象。而本身的.net对象还是要.net环境来支持的。

好了,知道了它的原理后,我们就明白可以做什么事了。下面,我们就是如何来做事了。

 

 

3部份,最简单的方法来暴露.net对象(VS2003)

这个方法很简单,就是做一个Library项目,然后把编译选项设定为Interop交互为true.

 

projectproperty.JPG
编译过后,系统会自动的为你在OS中注册一个COM,当然,有点类似ActiveX,就要看你的对象有没有界面了。

这里引用博客园里的另一文章给大家看看,应该有收获。

红马天下(http://www.cnblogs.com/homer/ )的这几篇文章:用C#编写ActiveX控件

http://www.cnblogs.com/homer/archive/2005/01/04/86473.html

http://www.cnblogs.com/homer/archive/2005/01/08/88780.html

http://www.cnblogs.com/homer/archive/2005/01/26/97822.html

上面的几篇文章里介绍了一些操作细节,我这里就不重复了。上面的工作大部份都是系统自动给我们完成的,下面我们来讨论一些细节问题。

 

4部份:以传统的COM方式向OS暴露.net对象(VS2003下的操作及实现)

1、             还是new一个Library项目。

2、             添加一个COM对象(要安装插件,后面有下载)

newcom.JPG

3、
             看看这个插件为我们做了什么(代码):

None.gif using  System;
None.gif using  System.Runtime.InteropServices;
None.gif
None.gif namespace  Webb.PublicCOM.TestCOM01
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    Events raised by your COM class#region Events raised by your COM class
InBlock.gif    //
InBlock.gif    // TODO: Insert delegates for events raised by your class here.
InBlock.gif    //
InBlock.gif    //public delegate void Event1Handler(int i, int j);
InBlock.gif
InBlock.gif    [    Guid("6C656B6C-F101-4CC8-A7C4-B2296A0A715C"),
InBlock.gif        InterfaceType(ComInterfaceType.InterfaceIsIDispatch) ]
InBlock.gif    public interface IWebbTestCOM02Events
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        //
InBlock.gif        // TODO: Insert event handlers for events your class raises here.
InBlock.gif        //
InBlock.gif        //[DispId(1)] void Event1(int i, int j);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    #endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    Interface published by your COM class#region Interface published by your COM class
InBlock.gif    [    Guid("BE268E05-CF9C-43B4-986E-720971A924A2"),
InBlock.gif        InterfaceType(ComInterfaceType.InterfaceIsDual) ]
InBlock.gif    public interface IWebbTestCOM02
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        //
InBlock.gif        // TODO: Insert functions your class publishes here.
InBlock.gif        //
InBlock.gif        //[DispId(1)] int Function1(string s1, string s2);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    #endregion

InBlock.gif
InBlock.gif    [    Guid("B303E55E-F40A-4E8C-B94B-BE781E01274C"),
InBlock.gif        ProgId("Webb.PublicCOM.TestCOM01.WebbTestCOM02"),
InBlock.gif        ClassInterface(ClassInterfaceType.None),
InBlock.gif        ComSourceInterfaces(typeof(IWebbTestCOM02Events)) ]
InBlock.gif    public class WebbTestCOM02 : IWebbTestCOM02
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        //
InBlock.gif        // TODO: Insert events raised by your class here.
InBlock.gif        //
InBlock.gif        //public event Event1Handler Event1;
InBlock.gif
InBlock.gif        // A COM class must have a public parameterless constructor.
InBlock.gif        // Otherwise the class might not be registered for COM and
InBlock.gif        // cannot be created by CreateObject.
InBlock.gif        public WebbTestCOM02() : base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        //
InBlock.gif        // TODO: Implement the methods of your class interface here.
InBlock.gif        //
InBlock.gif        //public int Function1(string s1, string s2)
InBlock.gif        //{
InBlock.gif        //    return 0;
InBlock.gif        //}
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

4、             删除一些注释的代码,编译项目,你就可以得到一个已经向OS暴露过的.net对象了。当然,你还可以添加一些函数或者事件。编译过后,有一个DLL文件,还有一个tlb文件,它就是用工具导出后,向COM暴露的组件库数据。C++可以用该文件通过COM方式来访问.net对象,注意,这与C++写托管代码完全不同。源代码有一个用C++通过COM来访问.net对象的实例。

5、             用其它应该程序来访问上面的对象。
我们的目标很明确,就是以COM的方式来访问前面的.net对象,首先你可以在COM对象里找到我们编译过和注册过的对象:
selectCOM.JPG

如果你是在.net下引用该COM,你会得到一个错误:

selectCOMerror.JPG
这里说明两个问题:

                        i.              我们在vs.net2003下添加COM引用时,其实就是我第1部份里说的原理,而这里我们所引用的对象本身已经是.net对象,所以再引用时,是会出错的。这里是引用,不是运行时创建。

                       ii.              我们可以直接对DLL文件引用,这与一般的.net程序集引用是有一点区别的:

 

comview.JPG
可以看到,我们引用后的对象,与一般程序集的引用不一样,反而也COM的引用很像。也就说,我们这里的引用是通过了RCW的。

 

接下来,你就可以自由的把你的.net对象写的很完善了。大家应该可以注意到,我们写这个项目的时候,就是要遵守一些开发原则。因为COM的特殊性,我们的对象必须定义和实现一些接口,而在每种开发语言里,它的实现方法是不一样的。本文暂时不讨论代码实现细节,示例代码里有一个例子,可以参考一下。

 

5部份:以ActiveX方式向OS暴露.net对象(VS2003下的操作及实现)

接上面的思路,我们应该可以想到,以COM方式向OS暴露.net对象,就是让我们的对象实现一些接口,定义一些必须信息。然后利用工具向OS注册这些信息就行了。下面讨论的就是,我们手动的修改和添加一些信息,让我们的.net对象实现更多的接口,然后以ActiveX的形式暴露给OS。首先说明,ActiveX是有页面的,因此我们可以从一个UserControl上开发。

在原来的项目中,添加一个TestActiveX01对象,从UserControl继承。然后给我们的对象添加一些特殊功能,让它以ActiveX的形式暴露给OS

None.gif using  System;
None.gif using  System.Collections;
None.gif using  System.ComponentModel;
None.gif using  System.Drawing;
None.gif using  System.Data;
None.gif using  System.Windows.Forms;
None.gif
None.gif //  Add in these    using clauses for this example
None.gif using  System.Runtime.InteropServices;
None.gif using  System.Text;
None.gif using  System.Reflection;
None.gif using  Microsoft.Win32;
None.gif
None.gif
None.gif namespace  Webb.PublicCOM.TestCOM01
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    /// Summary description for TestActiveX03.
ExpandedSubBlockEnd.gif    /// </summary>

InBlock.gif    [ProgId("Webb.PublicCOM.TestActiveX03")]
InBlock.gif    [ClassInterface(ClassInterfaceType.AutoDual)]
InBlock.gif    public class TestActiveX03 : System.Windows.Forms.UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        private System.Windows.Forms.PictureBox pictureBox1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
InBlock.gif        /// Required designer variable.
ExpandedSubBlockEnd.gif        /// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        public TestActiveX03()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            // This call is required by the Windows.Forms Form Designer.
InBlock.gif            InitializeComponent();
InBlock.gif            // TODO: Add any initialization after the InitializeComponent call
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
InBlock.gif        /// Clean up any resources being used.
ExpandedSubBlockEnd.gif        /// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                if(components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Component Designer generated code#region Component Designer generated code
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
InBlock.gif        /// Required method for Designer support - do not modify 
InBlock.gif        /// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        /// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TestActiveX03));
InBlock.gif            this.pictureBox1 = new System.Windows.Forms.PictureBox();
InBlock.gif            this.SuspendLayout();
InBlock.gif            // 
InBlock.gif            // pictureBox1
InBlock.gif            // 
InBlock.gif            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
InBlock.gif            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
InBlock.gif            this.pictureBox1.Name = "pictureBox1";
InBlock.gif            this.pictureBox1.Size = new System.Drawing.Size(128, 40);
InBlock.gif            this.pictureBox1.TabIndex = 0;
InBlock.gif            this.pictureBox1.TabStop = false;
InBlock.gif            // 
InBlock.gif            // TestActiveX03
InBlock.gif            // 
InBlock.gif            this.Controls.Add(this.pictureBox1);
InBlock.gif            this.Name = "TestActiveX03";
InBlock.gif            this.Size = new System.Drawing.Size(200, 152);
InBlock.gif            this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////    <summary>
InBlock.gif        ///    Register the class as a    control    and    set    it's CodeBase entry
InBlock.gif        ///    </summary>
ExpandedSubBlockEnd.gif        ///    <param name="key">The registry key of the control</param>

InBlock.gif        [ComRegisterFunction()]
InBlock.gif        public static void RegisterClass ( string key )
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
InBlock.gif            StringBuilder    sb = new StringBuilder ( key ) ;
InBlock.gif            sb.Replace(@"HKEY_CLASSES_ROOT\","") ;
InBlock.gif            // Open the CLSID\{guid} key for write access
InBlock.gif            RegistryKey k    = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);
InBlock.gif            // And create    the    'Control' key -    this allows    it to show up in
InBlock.gif            // the ActiveX control container
InBlock.gif            RegistryKey ctrl = k.CreateSubKey    ( "Control"    ) ;
InBlock.gif            ctrl.Close ( ) ;
InBlock.gif            // Next create the CodeBase entry    - needed if    not    string named and GACced.
InBlock.gif            RegistryKey inprocServer32 = k.OpenSubKey    ( "InprocServer32" , true )    ;
InBlock.gif            inprocServer32.SetValue (    "CodeBase" , Assembly.GetExecutingAssembly().CodeBase )    ;
InBlock.gif            inprocServer32.Close ( ) ;
InBlock.gif            // Finally close the main    key
InBlock.gif            k.Close (    ) ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////    <summary>
InBlock.gif        ///    Called to unregister the control
InBlock.gif        ///    </summary>
ExpandedSubBlockEnd.gif        ///    <param name="key">Tke registry key</param>

InBlock.gif        [ComUnregisterFunction()]
InBlock.gif        public static void UnregisterClass ( string    key    )
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            StringBuilder    sb = new StringBuilder ( key ) ;
InBlock.gif            sb.Replace(@"HKEY_CLASSES_ROOT\","") ;
InBlock.gif            // Open    HKCR\CLSID\{guid} for write    access
InBlock.gif            RegistryKey    k =    Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);
InBlock.gif            // Delete the 'Control'    key, but don't throw an    exception if it    does not exist
InBlock.gif            k.DeleteSubKey ( "Control" , false ) ;
InBlock.gif            // Next    open up    InprocServer32
InBlock.gif            RegistryKey    inprocServer32 = k.OpenSubKey (    "InprocServer32" , true    ) ;
InBlock.gif            // And delete the CodeBase key,    again not throwing if missing
InBlock.gif            k.DeleteSubKey ( "CodeBase"    , false    ) ;
InBlock.gif            // Finally close the main key
InBlock.gif            k.Close    ( )    ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

编译,然而用Active Control Test Container 来测试和查看我们的ActiveX:

 

selectActiveX.JPG
可以看到,经过特殊方法实现后的对象可以在ActiveX中查看的到,而且可以添加到容器中:

ActiveXView.JPG
接下来的工作就是一些功能的实现细节问题了,本文暂时不讨论这些内容。

最后一个问题就是在其它语言中通过COM来访问我们的.net对象,我们在C++下经过测试并实现了一些细节问题,事件等。但源代码不在示例中。

 

总结:.net开发COM或者ActiveX,其实是把写好的.net对象(当然是准备暴露给OS的),实现一些特殊的接口,通过工具把元数据导出成COM可用的组件库数据,然后注册给OS。运行时,通过CCW来动态的管理与维护.net对象。而对于用户来说,他就像访问COM一样访问.net对象。

MSDN参考:

http://msdn2.microsoft.com/zh-cn/library/8bwh56xe(VS.80).aspx

http://msdn2.microsoft.com/zh-cn/library/e753eftz(vs.80).aspx

 

下载文档:

1、             VS2003COM开发插件。

http://www.cnblogs.com/Files/WuCountry/CSComSetup.zip

2、             示例项目代码。
http://www.cnblogs.com/Files/WuCountry/Webb.PublicCOM.zip

3、             本文doc文档。
http://www.cnblogs.com/Files/WuCountry/TempDoc.zip

转载于:https://www.cnblogs.com/platinum/archive/2007/06/22/792475.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值