1

我有 ac# COM 服务器为事件处理定义了一些委托。这是来自 MSDN 的示例代码:

using System;
using System.Runtime.InteropServices;
namespace Test_COMObject
{
public delegate void ClickDelegate(int x, int y);
public delegate void ResizeDelegate();
public delegate void PulseDelegate();

// Step 1: Defines an event sink interface (ButtonEvents) to be     
// implemented by the COM sink.
[GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface MyButtonEvents
{
    void Click(int x, int y);
    void Resize();
    void Pulse();
}
// Step 2: Connects the event sink interface to a class 
// by passing the namespace and event sink interface
// ("EventSource.ButtonEvents, EventSrc").
[ComSourceInterfaces(typeof(MyButtonEvents))]
public class MyButton
{
    public event ClickDelegate Click;
    public event ResizeDelegate Resize;
    public event PulseDelegate Pulse;

    public MyButton()
    {
    }
    public void CauseClickEvent(int x, int y)
    {
        Click(x, y);
    }
    public void CauseResizeEvent()
    {
        Resize();
    }
    public void CausePulse()
    {
        Pulse();
    }
}
}

但是 msdn 只提供了一个 VB 示例来实现 COM 客户端事件接收器,任何人都可以给我一个示例,非托管 c++ 是如何做到这一点的吗?下面是实现 COM 事件接收器的 vb 示例

' COM client (event sink)
' This Visual Basic 6.0 client creates an instance of the Button class and 
' implements the event sink interface. The WithEvents directive 
' registers the sink interface pointer with the source.
Public WithEvents myButton As Button

Private Sub Class_Initialize()
Dim o As Object
Set o = New Button
Set myButton = o
End Sub
' Events and methods are matched by name and signature.
Private Sub myButton_Click(ByVal x As Long, ByVal y As Long)
MsgBox "Click event"
End Sub

Private Sub myButton_Resize()
MsgBox "Resize event"
End Sub

Private Sub myButton_Pulse()
End Sub
4

1 回答 1

1

语言运行时通常隐藏与从 COM 对象接收事件有关的大量管道。但是,如果您在原始 C++ 中执行此操作,则需要通过查询 IConnectionPointContainer 的接口指针来开始。这使您可以枚举所有传出接口。然后,您可以获取特定接口的 IConnectionPoint。然后调用它的 Advise() 方法来设置接收回调的接收器接口。这会给你一个cookie,你需要存储它。稍后当您想通过 Unadvise 呼叫取消订阅时。

它在 MSDN 库中描述得相当好,从这里开始阅读

于 2012-07-31T06:00:12.937 回答