CJ7*_*CJ7 5 .net c# 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(); }
Run Code Online (Sandbox Code Playgroud)
这会有用吗?
我在其他地方读过,在'过程边界'上发送对象可能会导致问题.它是否正确?
一种方法是在 .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
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
然后在 .NET 中有一个带有 IEventHandler 静态集合的注册器:
<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
Run Code Online (Sandbox Code Playgroud)
您的 .NET 代码将调用 FireEvent,如果 VB6 之前已调用 RegisterHandler,则您的 VB6 IEventHandler 将被调用。
| 归档时间: |
|
| 查看次数: |
2119 次 |
| 最近记录: |