如何将NET程序集发布为COM组件


 

在.NET中提供有一种很有用的机制,它可以把 .Net 托管DLL注册成COM,以供其他平台调用。“史称” Com Callable Wrapper

以下是MSDN给出的解释

When a COM client calls a .NET object, the common language runtime creates the managed object and a COM callable wrapper (CCW) for the object. Unable to reference a .NET object directly, COM clients use the CCW as a proxy for the managed object.

The runtime creates exactly one CCW for a managed object, regardless of the number of COM clients requesting its services. As the following illustration shows, multiple COM clients can hold a reference to the CCW that exposes the INew interface. The CCW, in turn, holds a single reference to the managed object that implements the interface and is garbage collected. Both COM and .NET clients can make requests on the same managed object simultaneously.

Accessing .NET objects through COM callable wrapper

 

 

 

接下来让我们通过下面的实例一窥究竟。

一:如何编译一个NET程序集 并注册为COM


  • 1> 在vs2005中建立一个普通C# 的 class library 工程, 并在写入代码
Net2Com
namespace Net2Com
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    
public class TestClass
    {
        
public string SayHello(string name)
        {
            
return "Hello " + name;
        }

    }
}

 

  • 2> Project-->"Properties"-->"Build"-->勾上"Register for COM interop"
  • 3> 在AssemblyInfo.cs 文件中修改[assembly: ComVisible(true)]
  • ComVisible
    // Setting ComVisible to false makes the types in this assembly not visible 
    // to COM components.  If you need to access a type in this assembly from 
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(true)]

    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("0fe8e515-872d-49a8-859b-6d802ed2b60c")]

    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version 
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Revision and Build Numbers 
    // by using the '*' as shown below:
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

     

  •  

  • 4> 编译项目,VS将会自动完成dll注册.

那么VS都将哪些信息注册到注册表了呢。不妨让我们用“Net2Com”这个关键字搜索下

1: HKEY_CLASSES_ROOT\CLSID{E24B3E60-......}

 

2:HEKY_CLASSED_ROOT\Net2Com.TestClass\CLSID 

 注意下红框框里的GUID, 就是第一张图中的节点值。

 

3: HEKY_CLASSED_ROOT\TypeLib\{0FE8E515-......}

 

 

二:如何使用该程序集

到这里,我们已经将该程序集注册成COM了,接下来就是如何使用的问题。 

1:NET 调用 

首先,让我们从VS.NET 中引用该COM。这就像平时我们添加引用一样。只不过是在COM这个选项卡里,具体如下图

 

2:VB 调用 

或许很多人会说,NET调用COM化的NET 程序集,岂不是多次一举?确实,我也觉得有点画蛇添足,那么为了让我的辛苦劳动有所价值,这次我们将从VB中引用它

 首先我们新建一个VB的项目,然后在Projecy->reference 中引用该程序集

 

接下来添加如下代码

 

Option Explicit
Dim c As Net2Com.TestClass

Private Sub Command1_Click()
Set c = New Net2Com.TestClass
  
MsgBox c.SayHello("Jeremy")
End Sub

 

运行结果如下

 

 

 

三:总结

在企业架构中,不可能只存在一种技术。VB, .net,JAVA, C++ 都各自耕耘在自己的一亩三分地上。那么如何解决各平台之间资源整合信息共享呢?

假设,我有一个很成熟的算法,并在NET中实现, 另外一个VB的项目想使用该算法,如果直接做.NET 翻译成VB,不仅耗时耗力,可能还因为使用了一些。NET的特性而无法100%移植,这个时候,将.NET程序公开成COM,就是一个很好的选择。 

posted on 2010-10-01 20:13  Jeremy@sungard  阅读(878)  评论(2编辑  收藏  举报