通过互操作将VB6对象传递给.NET对象?

时间:2011-12-19 14:54:11

标签: c# .net vb6 com-interop

我有一个VB6应用程序,通过互操作显示.NET DLL表单。

我希望.NET DLL中的一个事件能够显示VB6应用程序中的表单。

我的想法是让VB6应用程序将表单的引用传递给.NET DLL。例如:

[VB6]
Dim objNetDllObject As New NetDllObject
objNetDllObject.PassVb6Form(MyForm)
objNetDllObject.ShowForm

[C#]
object Vb6Form; 
private void PassVb6Form(object form) { Vb6Form = form; }
private void button1_Click(object sender, EventArgs e) { Vb6Form.Show(); }

这会有用吗?

我在其他地方读过,在'过程边界'上发送对象可能会导致问题。这是对的吗?

2 个答案:

答案 0 :(得分:4)

一种方法是在.NET中定义COM接口:

<System.Runtime.InteropServices.GuidAttribute("0896D946-8A8B-4E7D-9D0D-BB29A52B5D08"), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface IEventHandler
    Sub OnEvent(ByRef sender As Object, ByRef e As Object)
End Interface

在VB6中实现此接口

Implements MyInterop.IEventHandler

Private Sub IEventHandler_OnEvent(ByRef sender As Variant, ByRef e As Variant)
    Dim id
    id = e.Entity.Id
    ' As long as e is COM Visible (not necessarily COM registered, this will work)
End Sub

然后在.NET中有一个注册器,带有IEventHandlers的静态集合:

<ComClass(ComRegistrar.ClassId, ComRegistrar.InterfaceId, ComRegistrar.EventsId>
Public Class ComRegistrar

   Private Shared ReadOnly _eventHandlers As New Dictionary(Of String, List(Of IEventHandler))


   ' This is called by .NET code to fire events to VB6
   Public Shared Sub FireEvent(ByVal eventName As String, ByVal sender As Object, ByVal e As Object)
        For Each eventHandler In _eventHandlers(eventName)
                eventHandler.OnEvent(sender, e)
        Next
   End Sub

   Public Sub RegisterHandler(ByVal eventName As String, ByVal handler As IEventHandler)
        Dim handlers as List(Of IEventHandler)
        If Not _eventHandlers.TryGetValue(eventName, handlers)
             handlers = New List(Of IEventHandler)
             _eventHandlers(eventName) = handlers
        End If
        handlers.Add(handler)
   End Sub

End Class

你的.NET代码会调用FireEvent,如果VB6以前调用了RegisterHandler,你的VB6 IEventHandler就会被调用。

答案 1 :(得分:0)

您还可以使用ComSourceInterfacesAttribute,这可以使您的代码更简单(或者至少从VB6方面更自然)。您有以下代码:

C#库:

namespace WindowsFormsControlLibrary1
{
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IMyFormEvents
    {
        [DispId(1)]
        void MyEvent();
    }

    public delegate void MyEventEventHandler();

    [ComSourceInterfaces(typeof(IMyFormEvents))]
    public partial class MyForm : Form
    {
        public event MyEventEventHandler MyEvent;

        public MyForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.MyEvent != null)
            {
                this.MyEvent();
            }
        }
    }
}

VB6客户端:

Option Explicit

Private WithEvents f As WindowsFormsControlLibrary1.MyForm

Private Sub Command1_Click()
    Set f = New WindowsFormsControlLibrary1.MyForm
    Call f.Show
End Sub

Private Sub f_MyEvent()
    MsgBox "Event was raised from .NET"
    'Open another VB6 form or do whatever is needed
End Sub

如果您打算在VB6应用程序中使用大量.NET UI,我强烈建议使用Interop Forms Toolkit来简化操作。 (它有助于生成像这些样本的代码,以及处理其他一些常见的场景)。