将简单的 C# DLL 转换为 COM 互操作组件

Turn a simple C# DLL into a COM interop component(将简单的 C# DLL 转换为 COM 互操作组件)

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

问题描述

如何将 C# DLL 制作成可供 VB6 应用程序使用的 COM 互操作 DLL?

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

推荐答案

这是我想在 StackOverflow 中找到但找不到的答案.事实证明,将简单的 C# dll 转换为 COM dll 相当容易.

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.

使用 C# 类项目创建解决方案.该类应该有一个属性/方法的接口和一个事件的接口.将 GUID 属性分配给类和接口,如 MSDN - 示例 COM 类(C# 编程)中所述指南).另请参阅:MSDN - 如何:引发已处理的事件通过 COM 接收器.

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.

在项目属性 > 应用程序选项卡 > 程序集信息按钮 > 选中使程序集 COM-Visible".这使得 COM 类中的所有公共方法都可见.

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

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

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

这就是创建 DLL 所需要做的一切.调用DLL需要先注册.

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

您可以通过以下方式之一注册 DLL:

You can register the DLL one of these ways:

  • 检查项目属性 > 构建选项卡 > 注册 COM 互操作".这将在您构建 DLL 时自动注册它.
  • 使用 RegAsm 手动注册 DLL.这允许您在您选择的目录中注册 DLL,而不是在构建目录中.这是我使用的方法.

  • 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.

  • 不要检查项目属性>构建选项卡>注册COM互操作"
  • 将 DLL 复制到要注册的目录中
  • 使用管理员权限打开命令外壳并键入

  • 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 可以在C:WindowsMicrosoft.NETFrameworkv2.0.50727"中找到,而mydll.dll"是您的 DLL 的名称;tlb 意思是创建一个类型库";codebase 表示将目录位置写入注册表,假设它没有放在 GAC 中".

RegAsm.exe can be found in "C:WindowsMicrosoft.NETFrameworkv2.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 将显示程序集应该是强命名的警告.你可以忽略它.

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

此时,您应该可以在 VB6 中添加对 COM DLL 的引用,使用 Intellisense 查看它,并像普通 COM DLL 一样运行它.

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.

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

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

在 InstallShield 中,将新组件添加到组件列表中.请记住将组件与功能相关联.将组件属性.NET COM Interop"设置为是.

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.

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

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".

将 .tlb 文件添加到组件的文件部分.检查Self-Register"属性.

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

目标 PC 上需要存在正确版本的 .Net Framework.

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

就是这样.

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

本文标题为:将简单的 C# DLL 转换为 COM 互操作组件

基础教程推荐