【发布时间】:2009-07-09 19:16:13
【问题描述】:
我正在将处理 COM 对象(可能用 VB6 编写)的事件的 vb.net 应用程序从框架 1.1 升级到 WPF 2.0/3.5
代码:(为简洁起见简化了对象名称)
public class MyClass
Private WithEvents serviceMonitor As COMOBJECT.ServiceMonitor
Public Sub New()
serviceMonitor = New COMOBJECT.ServiceMonitor()
serviceMonitor.Connect([some ip address])
End Sub
Private Sub ServiceMonitor_ServiceConnectionUp(ByVal MonitorId As Integer, ByVal UserArg As Integer) _
Handles serviceMonitor.ServiceConnectionUp
Debug.WriteLine("connection up!")
End Sub
' other similar handlers omitted
End Class
应用程序将按预期获得回调,但在几秒钟内我收到访问冲突。基本的回调代码与 .net 1.1 版本类似,但运行良好。
根据我对错误的研究,它是由垃圾收集器移动东西引起的。由于我没有向 DLL 传递任何要操作的对象,我猜测回调是问题所在。其他人已经通过 <UnmanagedFunctionPointer(CallingConvention.Cdecl)> 和/或 Marshal.GetFunctionPointerForDelegate 的委托解决了这个问题。
不幸的是,我发现的所有示例都是 DLL 具有某种 SetCallback(IntPtr) 方法的情况。我正在使用 WithEvents 和 Handles 关键字。这是我的尝试(请注意,我删除了 Handles 关键字以便可以使用 AddHandler:
<UnmanagedFunctionPointer(CallingConvention.Cdecl)> _
Delegate Sub ServiceMonitor_ServiceConnectionUpDelegate(ByVal MonitorId As Integer, ByVal UserArg As Integer)
public class MyClass
Private WithEvents serviceMonitor As COMOBJECT.ServiceMonitor
Public Sub New()
serviceMonitor = New COMOBJECT.ServiceMonitor()
del = New ServiceMonitor_ServiceConnectionUpDelegate(AddressOf ServiceMonitor_ServiceConnectionUp)
AddHandler serviceMonitor.ServiceConnectionUp, del ' <--- Error here
serviceMonitor.Connect([some ip address])
End Sub
Private Sub ServiceMonitor_ServiceConnectionUp(ByVal MonitorId As Integer, ByVal UserArg As Integer)
Debug.WriteLine("connection up!")
End Sub
' other similar handlers omitted
End Class
我在 AddHandler 行上遇到的错误是:“Value of type MyClass.ServiceMonitor_ServiceConnectionUpDelegate cannot be converted to COMOBJECT._IServiceMonitorEvents_ServiceConnectionUpEventHandler”
当我将鼠标悬停在提到的事件处理程序上时,它具有以下签名: 委托子_IServiceMonitorEvents_ServiceConnectionUpEventHandler(ByVal MonitorId As Integer, ByVal UserArg As Integer)
签名是相同的,所以我不确定是什么问题。
问题 1:如何以这种方式将委托与 AddHandler 一起使用?
问题二:Marshal.GetFunctionPointerForDelegate()需要参与吗?它返回一个 IntPtr 但 AddHandler 需要一个委托。
提前致谢。
【问题讨论】: