次の方法で共有


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

更新 : 2007 年 11 月

.NET Framework が提供するデリゲート ベースのイベント モデルに慣れていない場合は、「クラスへのイベントの追加」を参照してください。このセクションに適用される固有の詳細については、同じセクションの「イベントの利用」を参照してください。

.NET クライアント (イベント シンク) は、既存の COM サーバー (イベント ソース) で発生したイベントを受け取ることができます。COM 相互運用機能が、マネージ クライアントに含めるメタデータに、必要なデリゲートを生成します。インポートされたデリゲート シグネチャは、シンク イベント インターフェイス、アンダースコア (_)、イベント名、および EventHandler という語で構成されます。つまり、SinkEventInterface_EventNameEventHandler のようになります。

.NET クライアント内でイベントを発生させる COM オブジェクトには、解放される前に 2 つのガベージ コレクタ (GC) コレクションが必要になります。これは、COM オブジェクトとマネージ クライアントとの間で発生する循環参照が原因です。COM オブジェクトを明示的に解放する必要がある場合は、Collect メソッドを 2 回呼び出してください。

既存の COM イベント ソースとの相互運用を可能にするには

  1. COM の型を他のアプリケーションで共有する場合は、COM サーバーのプライマリ相互運用機能アセンブリを取得します。プライマリ相互運用機能アセンブリには、変換されたタイプ ライブラリを表すメタデータが含まれており、発行者によって署名されています。

    66ahbe6y.alert_note(ja-jp,VS.90).gifメモ :

    プライマリ相互運用機能アセンブリを使用できない場合、またはアセンブリをプライベートに使用する場合は、タイプ ライブラリ インポータ (Tlbimp.exe) か、または同等の API を使用して、タイプ ライブラリをインポートできます。

    変換プロセスは、イベントごとにデリゲートを生成しますが、必要なイベントをシンクするだけで十分です。

  2. MSIL 逆アセンブラ (Ildasm.exe) などのメタデータ ブラウザを使用して、イベント デリゲートを識別できます。

  3. マネージ イベント ソースからのイベントを処理する場合と同じ方法で、COM イベント ソースからのイベントを処理します。

使用例

Internet Explorer ウィンドウを開いて、InternetExplorer オブジェクトによって発生したイベントをマネージ コードで実装されたイベント ハンドラに結び付ける方法を次の例に示します。Internet Explorer の型 (イベント デリゲートを含む) の定義は、SHDocVw.dll からメタデータとしてインポートされます。この例では、TitleChange イベントがシンクされます。

Option Explicit
Option Strict

Imports System
Imports System.Runtime.InteropServices
Imports SHDocVw

Namespace InternetExplorer
    Public Class Explorer
        Public Shared Sub Main()
            Dim explorer As New Explorer()
            explorer.Run()
        End Sub
      
        Public Sub Run()
            Dim o As Object = Nothing
            Dim s As String
         
            Try
                ' Starts the browser.
                m_IExplorer = New SHDocVw.InternetExplorer()
            Catch e As Exception
                Console.WriteLine("Exception when creating Internet 
                Explorer object {0}", e)
                Return
            End Try
         
            ' Wires your event handlers to m_IExplorer.
            SetAllEvents()
         
            Try
                ' Goes to the home page.
                m_WebBrowser = CType(m_IExplorer, IWebBrowserApp)
                m_WebBrowser.Visible = True
                m_WebBrowser.GoHome()
            
                ' Starts navigating to different URLs.
                Console.Write("Enter URL (or enter to quit): ")
                s = Console.ReadLine()
                While s <> "" And Not (m_IExplorer Is Nothing) _
                And Not (m_WebBrowser Is Nothing)
                    m_WebBrowser.Navigate(s, o, o, o, o)
                    Console.Write("Enter URL (or enter to quit): ")
                    s = Console.ReadLine()
                End While
                m_WebBrowser.Quit()
            Catch sE As Exception
                If m_IExplorer Is Nothing And m_WebBrowser Is Nothing Then
                    Console.WriteLine("Internet Explorer has gone away")
                Else
                    Console.WriteLine("Exception happens {0}", sE)
                End If
            End Try
        End Sub
      
        ' Uses the AddHandler for adding delegates to events.
        Sub SetAllEvents()
            If Not (m_IExplorer Is Nothing) Then
                ' Title Change event
                ' DWebBrowserEvents2 is the name of the sink event interface.
                ' TitleChange is the name of the event.
                ' DWebBrowserEvents2_TitleChangeEventHandler is the delegate 
                ' name assigned by TlbImp.exe.
                Dim DTitleChangeE As New _
DWebBrowserEvents2_TitleChangeEventHandler(AddressOf OnTitleChange)
                AddHandler m_IExplorer.TitleChange, DTitleChangeE
            End If
        End Sub
      
        '----------------------------------------------------------------
        ' Defines event handlers.
        ' Document title changed
        Shared Sub OnTitleChange(sText As String)
            Console.WriteLine("Title changes to {0}", sText)
        End Sub
      
      
        End Sub
        '----------------------------------------------------------------
        ' The following are class fields.
        Private Shared m_IExplorer As SHDocVw.InternetExplorer = Nothing
        Private Shared m_WebBrowser As IWebBrowserApp = Nothing
    End Class
End Namespace
namespace InternetExplorer
{
    using System;
    using System.Runtime.InteropServices;
    using SHDocVw;

    public class Explorer 
    {
        public static void Main()
        {
            Explorer explorer = new Explorer();
            explorer.Run();
        }
        public void Run()
        {
            Object o = null;
            String s;

            try
            {
                // Starts the browser.
                m_IExplorer = new SHDocVw.InternetExplorer();
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception when creating Internet 
                Explorer object {0}", e);
                return;
            }

            // Wires your event handlers to m_IExplorer.
            SetAllEvents();

            try
            {  
                // Goes to the home page.
                m_WebBrowser = (IWebBrowserApp) m_IExplorer;
                m_WebBrowser.Visible = true;
                m_WebBrowser.GoHome();

                // Starts navigating to different URLs.
                Console.Write("Enter URL (or enter to quit): ");
                s = Console.ReadLine();
                while (s != "" && m_IExplorer != null &&
                    m_WebBrowser != null)
                {
                    m_WebBrowser.Navigate(s, ref o, ref o, ref o,
                          ref o);
                    Console.Write("Enter URL (or enter to quit): ");      
                    s = Console.ReadLine();
                }

                m_WebBrowser.Quit();
            }
            catch(Exception sE)
            {
                if (m_IExplorer == null && m_WebBrowser == null)
                {
                    Console.WriteLine("Internet Explorer has gone away");
                }
                else
                {
                    Console.WriteLine("Exception happens {0}", sE);
                }
            }
        }
        // Uses the += syntax for adding delegates to events.
        void SetAllEvents()
        {
            if (m_IExplorer != null)
            {
                // Title Change event
                // DWebBrowserEvents2 is the name of the sink event
                //interface.
                // TitleChange is the name of the event.
                // DWebBrowserEvents2_TitleChangeEventHandler is the 
                // delegate name assigned by TlbImp.exe.
                DWebBrowserEvents2_TitleChangeEventHandler 
                   DTitleChangeE = new DWebBrowserEvents2_TitleChangeEventHandler(OnTitleChange);
                m_IExplorer.TitleChange += DTitleChangeE;
            }
        }
///////////////////////////////////////////////////////////////////////
        // Define event handlers.
        // Document title changed
        static void OnTitleChange(String Text)
        {
            Console.WriteLine("Title changes to {0}", Text);
        }
   
//////////////////////////////////////////////////////////////////////////
        // The following are class fields.
        static private SHDocVw.InternetExplorer m_IExplorer = null;
        static private IWebBrowserApp m_WebBrowser = null;
    }
}

参照

処理手順

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

概念

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

参照

MSIL 逆アセンブラ (Ildasm.exe)

その他の技術情報

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