windows help: 从 VB6 项目调用 C# winforms dll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
help: Call C# winforms dll from VB6 project?
提问by pavanred
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. 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 中开发的模块应该是一个 dll 并且应该包含一些 windows 窗体。我能够从我的 vb6 项目中成功调用 ac# console applicaiton 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. I am calling it by this code -
在我的 VB6 项目中,我想显示我在 C#.net dll 中的表单。我用这个代码调用它 -
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 应用程序调用包含一些 winform 的 C#.net dll 的方式。
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?
我应该Class1
在我的 VB6 代码中实例化吗?如何解决此错误?我的方法正确吗?有没有更好的方法来做到这一点?
TIA.
TIA。
回答by C-Pound Guru
You have to make your class COM-Visible. Here's how I would change your code:
你必须让你的类 COM-Visible。以下是我将如何更改您的代码:
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.
注意[DispId(50)]
. 您想为 COM 可见的方法、属性和事件指定调度 ID。如果你不这样做,编译器会为你做,你可能会在每次编译时破坏兼容性。这个数字并不重要,因为它在编译之间不会改变。
You might want to check out Building COM Objects in C#. It's a pretty good getting started tutorial.
您可能想查看在 C# 中构建 COM 对象。这是一个很好的入门教程。
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.
将 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.
每个接口都需要在接口名称之前设置一个 GUID 属性。要生成唯一的 Guid,请使用 guidgen.exe 实用程序并选择注册表格式。
回答by Brannon
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 Callable Wrapper),并在 VB6 代码中创建该 COM 对象的实例。
This should help you get started:
这应该可以帮助您入门:
回答by Mesh
There some excellent help on msdn here:
在 msdn 上有一些很好的帮助: