다음을 통해 공유


방법: COM 싱크에서 처리하는 이벤트 발생

업데이트: 2007년 11월

.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 소스에서 발생하는 이벤트 처리

개념

.NET Framework 구성 요소를 COM에 노출

참조

ComSourceInterfacesAttribute

기타 리소스

관리되는 이벤트와 관리되지 않는 이벤트