65.9K
CodeProject 正在变化。 阅读更多。
Home

C# Com

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.78/5 (35投票s)

2007年5月25日

CPOL

5分钟阅读

viewsIcon

327314

downloadIcon

6183

使用Com+或Com Interop在VB6中访问C# .NET DLL

引言

本示例将帮助您了解如何在VB 6.0代码中实现和使用C# DLL。由于C#是一种面向对象的语言,我们可以利用其面向对象的特性在C# DLL中创建合适的类。我们可以使用COM Interop或遵循COM Plus方法来引用旧的VB 6.0应用程序中的此类DLL。这就像将我们的业务逻辑委托给这个DLL一样。在本文中,我尝试展示了两种在VB 6.0中引用C# DLL的不同方法。代码已附上,您可以直接参考以理解以下说明,或尝试根据说明创建自己的代码。

创建COM Interop

通过使用COM Interop,我们可以创建一个DLL,该DLL可以是私有的或共享的。可以在VB 6.0应用程序中引用此DLL。VB 6.0应用程序引用此DLL的类型库,即扩展名为 .tlb 的文件,该文件是使用VS工具实用程序创建的。为了使COM对象的客户端能够访问该对象,客户端需要对其进行描述,如何定位它以及如何调用其方法和属性。对于“真实”的非托管COM类,此描述以类型库的形式提供。类型库是对COM类支持的GUID,类和接口(方法,属性和参数)的二进制描述。

.NET程序集不包含与类型库兼容格式的信息。因此,程序员有必要运行两个.NET提供的实用程序之一,以将类的程序集描述提取到类型库文件中。一个实用程序是 tlbexp.exe ,即.NET类型库导出器。此命令行实用程序以要转换为类型库的程序集DLL的名称作为输入。程序员还可以指定要创建的类型库的名称。

tlbexp ComInteropExample.DLL /out:ComInteropExample.tlb 

创建类型库后,COM客户端可以引用它以获取COM客户端绑定到COM类接口并运行COM类实例所需的信息。用于从程序集创建类型库的另一个命令行实用程序是 regasm.exe ,即.NET程序集注册实用程序。除了创建类型库之外,此实用程序还会创建使程序集在COM对象中对客户端可见所必需的Windows注册表项,如下所示:

regasm ComInteropExample.DLL /tlb: ComInteropExample.tlb

请注意,.NET类库DLL的项目属性中还有一个名为“Register for COM Interop”的属性。将此属性设置为 True 指示IDE在每次生成时自动注册程序集以进行COM Interop,因此您无需手动执行此步骤。

在Visual Studio 2005项目中创建COM Interop DLL的说明

  1. 在VS 2005中创建一个新的类库项目,项目名称:ComInteropExample
  2. 在VS 2005中打开 AssemblyInfo.cs ,该文件位于项目的 Properties 文件夹中。将Com Visible设置为“True:
    [assembly: ComVisible(true)] 
  3. 转到项目属性 -> 生成。将“Register for Com Interop”选项选中为“Selected”。
  4. 转到类文件,例如 ComInteropClass.cs 并添加
    namespace using System.Runtime.InteropServices
    请注意,无需在应用程序引用中添加对 InteropServices DLL的引用。
  5. 对于接口 iInterface
    define GUID as [Guid("EC87B398-B775-4e6f-BE2C-997D4594CFAA")] 
    [InterfaceType(ComInterfaceType.InterfaceIsDispatch)]
    请注意,要创建GUID,请转到工具 -> 创建GUID -> 将GUID格式设置为注册表格式,复制GUID并使用语法将其添加到您的代码中。将接口方法写为
    [DispId(1)] int PerformAddition(int a, int b); 
    [DispId(2)] int PerformDeletion(int a, int b); 
    
    [Guid("EC87B398-B775-4e6f-BE2C-997D4594CFAA")] 
    [InterfaceType(ComInterfaceType.InterfaceIsDispatch)]
     
    public interface iInterface 
    { 
        [DispId(1)] int PerformAddition(int a, int b); 
        [DispId(2)] int PerformDeletion(int a, int b); 
    }
  6. 对于类 ComInteropClass ,添加语句
    above class [Guid("5674D47E-6B2A-456e-85C4-CB7AA6AIF24A")] 
    
    [ClassInterface(ClassInterfaceType.None)] 
    [ProgId("ComInteropClass")] 
    
    public class ComInteropClass:iInterface
    
    [Guid("0C216A19-E1B7-4b05-86D3-4C516BDDC041")] 
    [ClassInterface(ClassInterfaceType.None)] 
    [ProgId("ComInteropClass")] 
    
    //this name is used in VB code for late binding Dim the
    //Object As Object Set theObject = CreateObject("ComInteropClass")
     
    public class ComInteropClass:iInterface 
    { 
        #region iInterface Members public int PerformAddition(int a, int b) 
        { 
            // throw new Exception(
                "The method or operation is not implemented."); 
            try 
        { 
            return a + b; 
        } 
    
            catch 
            { 
                return 0; 
            } 
        }
        public int PerformDeletion(int a, int b) 
        { 
            //throw new Exception(
                "The method or operation is not implemented."); 
            try 
            { 
                return a - b; 
            } 
            catch 
            { 
                return 0; 
            } 
         } 
         #endregion 
    } 
  7. 使用VS 2005的SDK命令提示符注册程序集。转到项目的 release 文件夹
    regasm ComInteropExample.DLL /tlb: ComInteropExample.tlb

在VB中引用此TLB。VB代码随ZIP文件提供,请参考。它可以用于后期绑定。如果未分配强名称密钥,则程序集将是私有的,因此请将程序集复制到您要使用的文件夹中。要使程序集公开,请使用VS 2005的SN工具为程序集分配强名称密钥。

创建COM+

[assembly: ApplicationName("ComPlusExample")] 
[assembly:Description("ComPlus Assmebly")] 
[assembly:ApplicationActivation(ActivationOption.Server)] 
[assembly:ApplicationAccessControl(false)]
  1. 在Visual Studio 2005中创建一个新的C# .NET类库项目。
  2. 在VS2005中打开 AssemblyInfo.cs 文件。设置以下选项
    [assembly: ComVisible(true)] 
  3. 转到 References 文件夹 -> 右键单击 -> 添加引用(在.NET选项卡中)。添加
    reference : System.EnterpriseServices
  4. 打开一个类文件(ComPlusClass.cs )并引用Enterprise Service,如
    using System.EnterpriseServices 
  5. 在命名空间下方添加以下语句
  6. 创建一个接口,例如 iInterface
    public interface iInterface 
    { 
        int PerformAddition(int a,int b); 
        int PerformSubtraction(int a,int b); 
    }
  7. 创建实现接口 ServicedComponent iInterface 的类。
  8. 要跟踪此类COM+中的事件,请添加以下语句
    [EventTrackingEnabled(true)] 
    [Description("Serviced Component")] 
    [EventTrackingEnabled(true)] 
    [Description("Interface Serviced Component")]
    
        Collapse [EventTrackingEnabled(true)] 
                 [Description("Serviced Component")] 
                 [EventTrackingEnabled(true)] 
                 [Description("Interface Serviced Component")] 
    
    public class ComPlusClass:ServicedComponent,iInterface 
    {
        #region iInterface Members
    
        public int PerformAddition(int a, int b) 
        { 
             // throw new Exception(
                 "The method or operation is not implemented."); 
             try 
             { 
                 return a + b; 
             } 
                 catch 
             { 
                 return 0; 
             } 
        }
    
        public int PerformSubtraction(int a, int b) 
        { 
            //throw new Exception(
                "The method or operation is not implemented."); 
            try 
            { 
                return a - b; 
            } 
                catch 
            { 
                return 0; 
            }
            #endregion 
        }
    }
  9. 构建应用程序。使用VS 2005命令提示符为应用程序分配强名称密钥。转到开始 -> 程序文件 -> Visual Studio 2005 -> Visual Studio工具 -> SDK命令提示符。
    sn -k ComPlusClass.snk 
    将此强名称密钥添加到应用程序属性 -> 签名 -> 强名称密钥
  10. 注册程序集
    • 使用VS工具注册程序集,例如
      regasm ComPlusExample.DLL 
    • 使用该工具创建类型库
      tlbexp ComPlusExample.DLL 
    • 在COM+中注册它,例如
      regsvcs ComPlusExample.DLL 
  11. 要查看此已注册的COM+,请转到控制面板 -> 管理工具 -> COM+服务。在COM+服务 -> 计算机 -> 我的计算机 -> COM+应用程序中,右键单击它。您可以为此创建MSI。

摘要

Release 文件夹中引用程序集的TLB,以在VB6应用程序中使用。如果您有任何问题,请告诉我。我已尽力使本文易于阅读,并将根据您的宝贵建议对其进行改进。谢谢。

历史

  • 2007年5月25日 - 发布原始版本
© . All rights reserved.