简体   繁体   English

帮助:从VB6项目调用C#winforms dll?

[英]help: Call C# winforms dll from VB6 project?

I have a VB6 project(windows application) and I have to redevelop a module in the existing VB6 project in C#.net. 我有一个VB6项目(Windows应用程序),我必须在C#.net的现有VB6项目中重新开发一个模块。

The module that I develop in C#.net should be a dll and should contain some windows forms. 我在C#.net中开发的模块应该是dll,并且应包含一些Windows窗体。 I was able to successfully call ac# console applicaiton dll from my vb6 project but I am facing issues when i try to call a C# class library with winforms from my VB6 project. 我能够从我的vb6项目成功调用ac#控制台应用程序dll,但是当我尝试从我的VB6项目使用winforms调用C#类库时遇到了问题。

This is what I have done for my Proof Of Concept - This is a class file in my C#.net class library project. 这就是我为概念证明所做的-这是C#.net类库项目中的类文件。

namespace TestDll
{
    public interface IClass1
    {
        void DisplayMessage();
    }


    public class Class1:IClass1
    {              
        void IClass1.DisplayMessage()
        { 
            MessageBox.Show ("Displyaing message");
        }

    }
}

I have a form in the same nemspace and I plan to instantiate Class1 and use its object on the page_load event of the C# winform. 我在同一个nemspace中有一个表单,我打算实例化Class1并将其对象用于C#winform的page_load事件。

In my VB6 project I want to display the form I have in my C#.net dll. 在我的VB6项目中,我想显示我在C#.net dll中拥有的表格。 I am calling it by this code - 我用这段代码称呼它-

Private Declare Sub DislayMessage Lib "TestDll.dll" ()

Private Sub Command1_Click() //On button click event of the VB6 windows form
DislayMessage
End Sub

I get an error - "Can't find a DLL entry point in DisplayMessage in TestDll.dll" 我收到一个错误-“在TestDll.dll的DisplayMessage中找不到DLL入口点”

I am not sure how to solve this error. 我不确定如何解决此错误。 I am even skeptical if this is the way a C#.net dll which contains some winforms should be called from a VB6.0 windows applicaiton. 我什至怀疑是否是从VB6.0 Windows应用程序中调用包含某些winforms的C#.net dll的方式。

Should I instantiate Class1 in my VB6 code? 我应该在我的VB6代码中实例化Class1吗? How do I resolve this error? 如何解决此错误? Is my approach correct? 我的方法正确吗? Are there better ways to do this? 有更好的方法可以做到这一点吗?

TIA. TIA。

You have to make your class COM-Visible. 您必须将您的课程设为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");
        }

    }
}

Note the [DispId(50)] . 注意[DispId(50)] You want to specify the dispatch ID for your COM-visible methods, properties, and events. 您要为COM可见的方法,属性和事件指定调度ID。 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. 该数字无关紧要,因为它在编译之间不会改变。

You might want to check out Building COM Objects in C# . 您可能要签出C#中的“构建COM对象” It's a pretty good getting started tutorial. 这是一个很好的入门教程。

Some highlights: 一些重点:

Exposing the VC# objects to the COM world requires the following … 将VC#对象暴露给COM世界需要以下……

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

Every Interface needs a GUID property set before the interface name. 每个接口都需要在接口名称之前设置GUID属性。 To generate the unique Guid , use the guidgen.exe utility and select the Registry Format. 要生成唯一的Guid,请使用guidgen.exe实用工具,然后选择注册表格式。

The only way to do it is to expose your C# class as a COM object (also called a CCW - COM Callable Wrapper), and create an instance of that COM object in your VB6 code. 唯一的方法是将C#类公开为COM对象(也称为CCW-COM可调用包装),并在VB6代码中创建该COM对象的实例。

This should help you get started: 这应该可以帮助您入门:

http://www.bing.com/search?q=C%23+CCW&go=&form=QBRE&qs=n http://www.bing.com/search?q=C%23+CCW&go=&form=QBRE&qs=n

There some excellent help on msdn here: 这里有关于msdn的一些出色帮助:

http://msdn.microsoft.com/en-us/library/6bw51z5z.aspx http://msdn.microsoft.com/en-us/library/6bw51z5z.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM