将一个简单的C#DLL转换为COM互操作组件 [英] Turn a simple C# DLL into a COM interop component

查看:1743
本文介绍了将一个简单的C#DLL转换为COM互操作组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

如何使C#DLL成为可以被VB6应用程序使用的COM interop DLL?是我想在StackOverflow找到答案,但不能。



创建C#dll



使用C#类项目创建解决方案。类应该具有属性/方法的接口和事件的接口。按类别和接口分配GUID属性,如 MSDN - 示例COM类(C#编程指南)。另请参阅: MSDN - 如何:提高事件由COM接收器处理



在项目属性>应用程序选项卡>装配信息按钮>选中使装配COM-可见。这将使类COM中的所有公共方法可见。



在项目属性>构建选项卡中将平台目标设置为x86。



这就是创建DLL所需要做的。要调用DLL,您需要注册它。



在开发机器上注册DLL



请使用以下方法之一注册DLL:




  • 选中项目属性> Build选项卡>注册COM Interop。

  • 使用RegAsm手动注册DLL。这允许您在所选的目录中注册DLL,而不是在构建目录中。这是我使用的方法。




    • 不要检查项目属性> Build选项卡>

    • 将DLL复制到要注册它的目录

    • 使用管理员权限和类型打开命令shell

        RegAsm.exe -tlb -codebase mydll.dll 

      RegAsm.exe可以在C:\Windows \Microsoft.NET\Framework\v2.0.50727找到,而mydll.dll是您的DLL的名称; tlb 表示创建类型库;
      codebase 表示将目录位置写入注册表,假设它未放在GAC中。



      RegAsm将显示一个警告,指示程序集应命名为strong。你可以忽略它。



      此时,您应该能够在VB6中添加对COM DLL的引用,使用Intellisense查看它,并像常规COM DLL一样运行。 / p>





使用InstallShield安装DLL



如果您使用InstallShield与应用程序的其余部分一起安装DLL,请执行以下操作。



在InstallShield中,组件到组件列表。请记住将组件与某个功能关联。
将组件属性.NET COM Interop设置为是。



将.dll文件添加到组件的Files部分。
不要检查自注册属性。
右键单击.dll文件并选择设置密钥文件。



将.tlb文件添加到组件的文件部分。
检查自注册属性。



.Net框架的正确版本需要存在于目标PC上。



就是这样。


How do I make a C# DLL into a COM interop DLL that can be consumed by a VB6 application?

解决方案

This is the answer I wanted to find in StackOverflow but couldn't. It turns out to be fairly easy to turn a simple C# dll into a COM dll.

To create the C# dll

Create a solution with a C# class project. The class should have an interface for the properties/methods and an interface for the events. Assign GUID attributes to the class and interfaces as described in MSDN - Example COM Class (C# Programming Guide). Also see: MSDN - How to: Raise Events Handled by a COM Sink.

In Project Properties > Application tab > Assembly Information button > check "Make assembly COM-Visible". This makes all public methods in the class COM visible.

In Project Properties > Build tab > Set "Platform target" to x86.

That's all you need to do to create the DLL. To call the DLL, you need to register it.

Registering the DLL on your development machine

You can register the DLL one of these ways:

  • Check Project Properties > Build Tab > "Register for COM Interop". This will automatically register the DLL when you build it.
  • Manually register the DLL with RegAsm. This allows you to register the DLL in the directory of your choice, rather than in the build directory. This is the method I used.

    • Do not check Project Properties > Build Tab > "Register for COM Interop"
    • Copy the DLL to the directory where you want to register it
    • Open a command shell with administrator rights and type

      RegAsm.exe -tlb -codebase mydll.dll
      

      RegAsm.exe can be found in "C:\Windows\Microsoft.NET\Framework\v2.0.50727", while "mydll.dll" is the name of your DLL; tlb means "create a type library"; codebase means "write the directory location to the Registry, assuming it is not being placed in the GAC".

      RegAsm will display a warning that the assembly should be strong-named. You can ignore it.

      At this point, you should be able to add a reference to the COM DLL in VB6, see it with Intellisense, and run it just like a regular COM DLL.

Installing the DLL with InstallShield

If you are using InstallShield to install the DLL along with the rest of your application, do the following.

In InstallShield, add a new Component to the Components list. Remember to associate the Component with a Feature. Set component property ".NET COM Interop" to Yes.

Add the .dll file to the Files section of the Component. Do not check the "Self-Register" property. Right-click on the .dll file and select "Set Key File".

Add the .tlb file to the Files section of the Component. Check the "Self-Register" property.

The correct version of the .Net Framework needs to exist on the target PC.

That's it.

这篇关于将一个简单的C#DLL转换为COM互操作组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆