求助:从VB6项目中调用C#的WinForms的dll? [英] help: Call C# winforms dll from VB6 project?

查看:123
本文介绍了求助:从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,并应包含一些窗口的形式。我能成功调用C#控制台应用程序了DLL从我的VB6的项目,但是当我尝试调用一个C#类库与我的VB6项目的WinForms我面对的问题。

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一种形式,我打算实例 1级并使用其对象的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

$ b $的按钮单击事件b

我得到一个错误 - 在TestDll.dll在找不到DisplayMessage一个DLL入口点

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

我不知道如何解决这个错误。我甚至怀疑,如果这是一个包含一些的WinForms一个C#.NET的DLL应该从窗户VB6.0应用程序了被称为的方式。

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.

我应该实例 1级在我的VB6的代码?我该如何解决这个问题?
是我的做法正确吗?有没有更好的方式来做到这一点?

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-可见。以下是我会改变你的代码:

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.

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

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