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

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

问题描述

我想写一个服务的包装器,这将被现有的VB6项目使用。我有大部分的基本框架工作,除了一个重要方面:我可以引用包装器在VB6项目和subs /函数调用等工作按预期,但事件不。这些事件在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对象将源代码(即生成)的EVENTS的接口。

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天全站免登陆