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

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

问题描述

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



我在C#.net中开发的模块应该是一个dll,应该包含一些窗体。我能够从我的vb6项目中成功地调用ac#console应用程序,但是当我尝试使用VB6项目中的winform调用C#类库时,我面临的问题。



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

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


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

}
}

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



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

 私有声明Sub DislayMessage LibTestDll.dll()

Private Sub Command1_Click()//在VB6窗口的按钮点击事件
DislayMessage
End Sub

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



我不知道如何解决这个错误。我甚至怀疑,如果这是一个C#.net dll包含一些winforms应该从VB6.0 Windows应用程序调用。



我应该在我的VB6代码中实例化 Class1 如何解决这个错误?
我的方法是否正确?有更好的方法吗?



TIA。

解决方案

可见。以下是我如何更改代码:

 命名空间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可见的方法,属性和事件指定dispatch ID。如果没有,编译器将为您做,您可能会在每次编译时最终破坏兼容性。这个数字并不重要,因为它不会在编译之间发生变化。



您可能需要查看在C#中构建COM对象。这是一个非常好的入门教程。



一些亮点:


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

  *该类必须是public 
*属性,方法和事件必须是公开的。
*属性和方法必须在类接口上声明。
*事件必须在事件接口中声明。

每个界面都需要在接口名称之前设置一个GUID属性
。要
生成唯一的Guid,请使用
guidgen.exe实用程序,然后选择
注册表格式。



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

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.

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");
        }

    }
}

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.

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

I get an error - "Can't find a DLL entry point in DisplayMessage in TestDll.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.

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.

解决方案

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");
        }

    }
}

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.

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

Some highlights:

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.

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天全站免登陆