在暴露给 COM 的类库中引发事件 [英] Raising events in a class library exposed to COM

查看:15
本文介绍了在暴露给 COM 的类库中引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为服务编写一个包装器,该服务将由现有的 VB6 项目使用.除了一个重要方面外,我已经让大部分基本框架工作了:我可以在 VB6 项目中引用包装器,并且子程序/函数调用等按预期工作,但事件没有.这些事件在 VB6 应用程序中可见,但它们从不触发.

I'm trying to write a wrapper to a service, which will be used by an existing VB6 project. I've got most of the basic framework working, except for one important aspect: I can reference the wrapper in a VB6 project and subs/function calls etc. work as expected, but events do not. The events are visible in the VB6 app, but they never fire.

VB.NET 代码:

Public Event Action_Response(ByVal Status as String)
Public Function TestEvent()
    RaiseEvent Action_Response("Test Done")
    Return "Done"
End Function

VB6 代码:

Dim WithEvents my_Wrapper as Wrapper_Class
Private Sub cmdTest_Click()
    Set my_Wrapper = New Wrapper_Class
    Debug.Print my_Wrapper.TestEvent() 
End Sub

Private Sub my_Wrapper_Action_Response(ByVal Status As String)
    Debug.Print Status
    Set my_Wrapper = Nothing
End Sub

因此,cmdTest 按钮代码按预期打印完成",但 Action_Response 事件不会触发.我还需要做些什么来触发事件吗?

So, the cmdTest button code prints 'Done' as expected, but the Action_Response event doesn't fire. Is there something else do I need to do to get the event to fire?

推荐答案

评论写的太多了,我来回答一下吧....

Its too much to write in a comment, so I'll make it an answer....

首先,确定要向 COM 公开的 .net 类.我会选择一个名为 CORE 的课程.

First, identify the .net class you want to expose to COM. I'll pick a class called CORE.

创建一个描述 CORE 对象将获取(即生成)的事件的接口.

Create an interface that describes the EVENTS that the CORE object will source (ie generate).

<ComVisible(True)> _
<Guid("some guid here...use guidgen, I'll call it GUID1")> _
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface ICoreEvents
    <System.Runtime.InteropServices.DispId(1)> _
    Sub FileLoaded(ByVal Message As String)
End Interface

接下来,为您的类的 COM 公开属性和方法创建一个接口.

Next, Create an interface for the COM exposed properties and methods of your class.

<ComVisible(True)> _
<Guid("another GUID, I'll call it guid2")> _
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface ICore
    ReadOnly Property Property1() As Boolean
    ReadOnly Property AnotherProperty() As ISettings
    ReadOnly Property Name() As String
    ReadOnly Property Phone() As String
End Interface

现在,创建您的实际 .net 类

Now, create your actual .net class

<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
<ComDefaultInterface(GetType(ICore))> _
<ComSourceInterfaces(GetType(ICoreEvents))> _
<Guid("a third GUID, I'll call it GUID3")> _
Public Class Core
    Implements ICore

    <System.Runtime.InteropServices.ComVisible(False)> _
    Public Delegate Sub OnFileLoaded(ByVal Message As String)
    Public Event FileLoaded As OnFileLoaded
End Class

现在,当您需要引发 FileLoaded 事件时,只需从您的班级中引发 RAISEEVENT FILELOADED(Message)..NET 会将事件转发到 COM,因为您已经连接了 COMSourceInterfaces 属性.

Now, when you need to raise the FileLoaded event, just RAISEEVENT FILELOADED(Message) from within your class. .NET will forward the event out to COM because you've hooked up the COMSourceInterfaces attribute.

该属性是其中大部分内容的简写,但不幸的是,您无法完全控制您需要执行某些操作(例如在您的 com 接口上保持版本兼容性).

The attribute is shorthand for much of of this, but unfortunately doesn't give you quite the control that you need to do certain things (for instance retain version compatibility on your com interfaces).

这篇关于在暴露给 COM 的类库中引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆