方法: COM シンクによって処理されるイベントを発生させる

.NET Framework が提供するデリゲート ベースのイベント モデルに慣れていない場合は、「イベントの処理と発生」を参照してください。

.NET Framework には、イベントの送信元 (ソース) をイベントの受信側 (シンク) に接続するためのデリゲート ベースのイベント システムが用意されています。 シンクが COM のクライアントの場合は、コネクション ポイントをシミュレートするための追加の要素をソースに含める必要があります。 この修正により、COM のクライアントは、IConnectionPoint::Advise メソッドを呼び出すことにより、従来の方法でイベント シンク インターフェイスを登録できます。Visual Basic ではコネクション ポイントの詳細は隠ぺいされているため、これらのメソッドを直接呼び出す必要はありません。

COM のイベント シンクとの相互運用を可能にするには

  1. マネージ コードに、イベント シンク インターフェイスを定義します。 このインターフェイスには、マネージ クラスによって供給されるイベントのサブセットを含めることができます。 インターフェイスのメソッド名は、イベント名と同じにする必要があります。

  2. ComSourceInterfacesAttribute を適用して、イベント シンク インターフェイスをマネージ クラスに接続します。

  3. マネージ クラスが含まれているアセンブリをタイプ ライブラリにエクスポートします。 Tlbexp.exe (タイプ ライブラリ エクスポーター) または同等の API を使用してアセンブリをエクスポートします。

  4. COM にイベント シンク インターフェイスを実装します。

  5. イベントをシンクする COM クライアントの場合は、そのタイプ ライブラリ内のイベント ソースによって定義されているイベント シンク インターフェイスを実装します。 次に、コネクション ポイント機構を使用して、シンク インターフェイスをイベント ソースに接続します。

使用例

イベント ソースとしてのマネージ サーバーと、イベント シンクとしての COM クライアントの例を次に示します。 マネージ サーバーは、ButtonEvents をイベント シンク インターフェイスとして宣言し、そのインターフェイスを Button クラスに接続します。 アンマネージ クライアントは、Button クラスのインスタンスを作成し、イベント シンク インターフェイスを実装します。

' Managed server (event source)
Option Explicit
Option Strict

Imports System
Imports System.Runtime.InteropServices

Namespace EventSource
    Public Delegate Sub ClickDelegate(x As Integer, y As Integer)
    Public Delegate Sub ResizeDelegate()
    Public Delegate Sub 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 ButtonEvents
        Sub Click(x As Integer, y As Integer)
        Sub Resize()
        Sub Pulse()
    End Interface
   
    ' Step 2: Connects the event sink interface to a class 
    ' by passing the namespace and event sink interface
    ' ("EventSource.ButtonEvents, EventSrc").
    <ComSourceInterfaces(GetType(ButtonEvents))> _
    Public Class Button
        Public Event Click As ClickDelegate
        Public Event Resize As ResizeDelegate
        Public Event Pulse As PulseDelegate
      
      
        Public Sub CauseClickEvent(x As Integer, y As Integer)
            RaiseEvent Click(x, y)
        End Sub
      
        Public Sub CauseResizeEvent()
            RaiseEvent Resize()
        End Sub
      
        Public Sub CausePulse()
            RaiseEvent Pulse()
        End Sub
    End Class
End Namespace
using System;
using System.Runtime.InteropServices;
namespace EventSource
{
    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 ButtonEvents
    {
        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(ButtonEvents))]
    public class Button
    {
        public event ClickDelegate Click;
        public event ResizeDelegate Resize;
        public event PulseDelegate Pulse;
      
        public Button()
        {
        }
        public void CauseClickEvent(int x, int y)
        { 
            Click(x, y);
        }
        public void CauseResizeEvent()
        { 
            Resize();
        }
        public void CausePulse()
        {
            Pulse();
        }
    }
}
' 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

参照

処理手順

方法: COM ソースによって発生したイベントを処理する

関連項目

ComSourceInterfacesAttribute

概念

COM への .NET Framework コンポーネントの公開

その他の技術情報

マネージ イベントとアンマネージ イベント