且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

求助:从VB6项目中调用C#的WinForms的dll?

更新时间:2022-11-08 18:40:39

您必须让你的类COM-可见。以下是我会改变你的代码:

You have to make your class COM-Visible. Here's how I would change your code:

namespace TestDll
{
    [Guid("FB8AB9B9-6986-4130-BD74-4439776D1A3D")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    public interface IClass1
    {
        [DispId(50)]
        void DisplayMessage();
    }


   [Guid("74201338-6927-421d-A095-3BE4FD1EF0B4")]
   [ClassInterface(ClassInterfaceType.None)]
   [ComVisible(true)]
   [ProgId("TestDll.Class1")]
    public class Class1:IClass1
    {              
        void IClass1.DisplayMessage()
        { 
            MessageBox.Show ("Displyaing message");
        }

    }
}

请注意了 [DISPID(50)] 。要指定您的COM可见方法,属性和事件的调度ID。如果你不这样做,编译器会为你做,你可能最终你每次编译时破坏兼容性。 ,人数不那么重要了,因为它不编译之间切换

Note the [DispId(50)]. You want to specify the dispatch ID for your COM-visible methods, properties, and events. If you don't, the compiler will do it for you and you may end up breaking compatibility every time you compile. The number doesn't matter so much as it doesn't change between compiles.

您可能要检查出的建筑COM在C#对象。 ,这是一个相当不错的入门教程

You might want to check out Building COM Objects in C#. It's a pretty good getting started tutorial.

一些亮点:

揭VC#对象的COM
世界需要以下...

Exposing the VC# objects to the COM world requires the following …

* The class must be public
* Properties, methods, and events must be public.
* Properties and methods must be declared on the class interface.
* Events must be declared in the event interface.



每个接口所需要的接口名称前一个GUID属性
设置。至
生成唯一的GUID,使用
Guidgen.exe实用工具,并选择
注册表格式。

Every Interface needs a GUID property set before the interface name. To generate the unique Guid , use the guidgen.exe utility and select the Registry Format.