help: 从 VB6 项目调用 C# winforms dll? [英] help: Call C# winforms dll from VB6 project?

查看:30
本文介绍了help: 从 VB6 项目调用 C# winforms dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VB6 项目(Windows 应用程序),我必须在 C#.net 中的现有 VB6 项目中重新开发一个模块.

I have a VB6 project(windows application) and I have to redevelop a module in the existing VB6 project in C#.net.

我用 C#.net 开发的模块应该是一个 dll 并且应该包含一些 windows 窗体.我能够从我的 vb6 项目中成功调用 c# 控制台应用程序 dll,但是当我尝试从我的 VB6 项目中调用带有 winforms 的 C# 类库时遇到问题.

The module that I develop in C#.net should be a dll and should contain some windows forms. I was able to successfully call a c# 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.

这是我为我的概念证明所做的 - 这是我的 C#.net 类库项目中的一个类文件.

This is what I have done for my Proof Of Concept - This is a class file in my C#.net class library project.

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


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

    }
}

我在同一个 nemspace 中有一个表单,我计划实例化 Class1 并在 C# winform 的 page_load 事件上使用它的对象.

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.

在我的 VB6 项目中,我想显示我在 C#.net dll 中的表单.我用这段代码调用它 -

In my VB6 project I want to display the form I have in my 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

我收到一个错误 - 在 TestDll.dll 的 DisplayMessage 中找不到 DLL 入口点"

I get an error - "Can't find a DLL entry point in DisplayMessage in TestDll.dll"

我不确定如何解决此错误.我什至怀疑这是不是应该从 VB6.0 Windows 应用程序调用包含一些 winforms 的 C#.net 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 代码中实例化 Class1 吗?如何解决此错误?我的方法正确吗?有没有更好的方法来做到这一点?

Should I instantiate Class1 in my VB6 code? How do I resolve this error? Is my approach correct? Are there better ways to do this?

TIA.

推荐答案

你必须让你的类 COM-Visible.以下是我将如何更改您的代码:

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.

您可能想查看 在 C# 中构建 COM 对象.这是一个很好的入门教程.

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.

这篇关于help: 从 VB6 项目调用 C# winforms dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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