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はCOMクライアントイベントシンクを実装するためのVBサンプルのみを提供します。これを行うためのアンマネージ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 に答える