打开一个简单的C#DLL到COM互操作组件组件、操作、简单、COM

2023-09-02 21:09:58 作者:沐梦.

我如何做一个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?

推荐答案

这是我想找到的计算器,但不能回答。它原来是很容易把一个简单的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#类项目的解决方案。类应该有一个接口,用于将属性/方法和用于事件的接​​口。例如COM类(C#编程 - 如 MSDN分配GUID属性的类和接口指南)。另请参见: - 如何:引发事件处理的由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可见。这使得在COM类的所有公共方法可见。

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

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

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 Interop 的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: WINDOWS Microsoft.NET 框架 V2.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.

在这一点上,你应该能够添加一个引用到COM DLL在VB6,看到它的智能感知,并运行它就像一个普通的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互操作为是。

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文件到组件的文件部分。 选中自注册的属性。

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

正确版本的.NET Framework的需要存在于目标计算机上。

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

就是这样。