> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 接続可能なオブジェクトの使用

ABBYY FlexiCapture SDK のオブジェクトの一部は、いわゆる「接続可能なオブジェクト」です。つまり、これらのオブジェクトは **IConnectionPointContainer** インターフェイスを実装しています。接続可能なオブジェクトは、ABBYY FlexiCapture SDK とそのクライアントとの間の通信をサポートします。

ABBYY FlexiCapture SDK の接続可能なオブジェクトはそれぞれ、2 つの型の接続ポイントを提供します。1 つは **dispatch** インターフェイスを使用するもの、もう 1 つは *IUnknown* から派生したインターフェイスを使用するものです。**dispatch** インターフェイスは Visual Basic などの環境で自動的に使用されるよう設計されており、vtbl ベースのインターフェイスは C++ での使用に適しています。

次の表に、ABBYY FlexiCapture SDK の接続可能なオブジェクトと、対応する Callback インターフェイス (dispinterface) の一覧を示します。

| **オブジェクト** | **Callback インターフェイス (Dispinterface)** |
| - | - |
| [**Project**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/project.md) [**Batch**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/batch.md) | [**IBatchEvents (DIBatchEvents)**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/ibatchevents.md) |
| [**AssemblingErrors**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/assemblingerrors.md) [**Batches**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/batches.md) [**BatchTypes**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/batchtypes.md) [**Blocks**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/blocks.md) [**Documents**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/documents.md) [**FieldInstances**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldinstances.md) [**Fields**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fields.md) [**Pages**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/pages.md) [**RuleErrors**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/ruleerrors.md) [**TableColumns**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/tablecolumns.md) | [**ICollectionEvents (DICollectionEvents)**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/icollectionevents.md) |
| [**ClassificationTrainer**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/classificationtrainer.md) [**FieldsExtractionTrainer**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainer.md) | [**IProcessingEvents (DIProcessingEvents)**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/iprocessingevents.md) |

ABBYY FlexiCapture SDK で発生する特定のイベントの通知を受け取るには、クライアントアプリケーションで特定の型のインターフェイスを実装し、それらのインターフェイスを実装したオブジェクトを、対応する接続可能なオブジェクトに「advise」する必要があります。使用する開発ツールごとの詳細については、以下を参照してください。

> [accordion] Visual Basic .NET
>
> Batch オブジェクトの場合の手順は次のとおりです。
>
> 1. **IBatchEvents\_Event** 型のイベントソースオブジェクトを *WithEvents* で宣言します。
>
>    ```vb theme={null}
>    Public WithEvents eventsSource As FCEngine.IBatchEvents_Event
>    ```
>
> 2. **IBatchEvents** インターフェイスの必要なメソッドを、次のいずれかの形式の Sub で実装します。
>
>    プログラムの実行中に任意のタイミングでイベント処理を開始・停止する場合 (AddHandler 文および RemoveHandler 文を使用) :
>
>    ```vb theme={null}
>    Private Sub eventsSource_OnBatchProgress(ByVal Batch As FCEngine.IBatch, _
>                                             ByVal ProcessedItemsCount As Integer, _
>                                             ByVal RemainingItemsCount As Integer)
>    ...
>    End Sub
>    ```
>
>    または、このプロシージャが特定のイベントを処理することを指定するだけでもかまいません (定義時に Handles キーワードを使用) :
>
>    ```vb theme={null}
>    Private Sub eventsSource_OnBatchProgress(ByVal Batch As FCEngine.IBatch, _
>                                             ByVal ProcessedItemsCount As Integer, _
>                                             ByVal RemainingItemsCount As Integer) Handles eventsSource.OnBatchProgress
>    ...
>    End Sub
>    ```
>
> 3. **Batch** オブジェクトでイベントソースを初期化します。
>
>    ```vb theme={null}
>    Dim project As FCEngine.IProject
>    project = Engine.OpenProject("D:\Sample.fcproj")
>    Dim batch As FCEngine.IBatch
>    batch = project.Batches.AddNew("TestBatch")
>    eventsSource = batch
>    ```
>
> 4. 実装したイベントハンドラーをイベントソースに接続し、**Batch** オブジェクトで処理を実行した後、ハンドラーを切断します。
>
>    ```vb theme={null}
>    AddHandler eventsSource.OnBatchProgress, AddressOf Me.eventsSource_OnBatchProgress
>    batch.Recognize(Nothing, FCEngine.RecognitionModeEnum.RM_ReRecognizeMinimal, Nothing)
>    RemoveHandler eventsSource.OnBatchProgress, AddressOf Me.eventsSource_OnBatchProgress
>    ```
>
>    Handles キーワードを使用してイベント処理メソッドを定義した場合は、**Batch** オブジェクトで処理を実行するだけです。
>
>    ```vb theme={null}
>    batch.Recognize(Nothing, FCEngine.RecognitionModeEnum.RM_ReRecognizeMinimal, Nothing)
>    ```

> [accordion] C++ (COM)
>
> C++ では、必要なインターフェイスを実装して接続ポイントを取得し、そのインターフェイスを実装したオブジェクトを対応する接続可能なオブジェクトに「advise」する必要があります。ここでは Batch オブジェクトを例に説明します。
>
> 1. **IBatchEvents** インターフェイスを実装します。このインターフェイスは **IUnknown** インターフェイスから派生しているため、クライアントオブジェクトは **IUnknown** のメソッドも実装する必要があります。
>
>    ```cpp theme={null}
>    class CProjectEventsListener : public IBatchEvents {
>    public:
>    ...
>        // IUnknown メソッドの簡単な実装を提供します。これらは
>        // COM をサポートする標準クラスを継承して実装することもできます
>        ULONG AddRef();
>        ULONG Release();
>        HRESULT QueryInterface(REFIID riid, void** ppvObject)
>        {
>            if( ppvObject == 0 )
>                return E_INVALIDARG;
>            if( riid == __uuidof(IBatchEvents) ) {
>                *ppvObject = static_cast<IBatchEvents*>( this );
>            } else if( riid == IID_IUnknown ) {
>                *ppvObject = static_cast<IUnknown*>( this );
>            } else {
>                *ppvObject = 0;
>                return E_NOINTERFACE;
>            }
>            AddRef();
>            return S_OK;
>        }
>        // IBatchEvents メソッドの実装を提供します
>        HRESULT OnBatchProgress(IBatch*, int, int);
>        HRESULT OnPageProgress(IPage*, int);
>        HRESULT OnPageRectProcessed(IPage*, int, int, int, int, int);
>        HRESULT OnProcessMessages(VARIANT_BOOL*);
>        HRESULT OnRecognizerTip(IPage*, BSTR);
>        HRESULT OnError(IPage*, BSTR, VARIANT_BOOL*);
>        HRESULT OnWarning(IPage*, BSTR, BSTR, int, int*);
>    };
>    ```
>
> 2. これで、CProjectEventsListener クラスを使用して **Batch** オブジェクトから通知を受け取れるようになります。このオブジェクトを通知ソースに advise します (エラー処理は省略しています) 。
>
>    ```cpp theme={null}
>    // Batch オブジェクトをすでに取得していると仮定します
>    IBatch* batch;
>    // Batch オブジェクトに対して IConnectionPointContainer を問い合わせます
>    IConnectionPointContainer* pContainer=0;
>    batch->QueryInterface(IID_IConnectionPointContainer, (void**)&pContainer);
>    // 接続可能なオブジェクト内の接続ポイントにある IConnectionPoint インターフェイスへのポインターを取得します
>    IConnectionPoint* pPoint=0;
>    pContainer->FindConnectionPoint(__uuidof(IBatchEvents),
>                                    &pPoint);
>    // CProjectEventsListener クラスと接続ポイントの間に接続を確立します
>    CProjectEventsListener listener;
>    IUnknown* listenerUnknown=0;
>    listener.QueryInterface(IID_IUnknown, (void**)&listenerUnknown);
>    // リスナーを通知ソースに advise します
>    DWORD cookie; // IConnectionPoint::Advise メソッドから返される cookie を格納する変数
>    pPoint->Advise(listenerUnknown, &cookie);
>    // バッチを処理します
>    ...
>    // 通知の受信が終わったらリスナーは不要になるため、unadvise します
>    pPoint->Unadvise(cookie);
>    ```

> [accordion] C#
>
> C# での手順は Visual Basic .NET の場合とほぼ同じです。Callback インターフェイスの必要なメソッドを実装し、該当するオブジェクトでイベントソースを初期化してから、実装したイベントハンドラーをイベントソースに接続します。ここでは **Batch** オブジェクトを例に説明します。
>
> 1. **IBatchEvents** インターフェイスの必要なメソッドを実装します。
>
>    ```csharp theme={null}
>    private void eventSource_OnBatchProgress(FCEngine.IBatch Batch, int ProcessedItemsCount, int RemainingItemsCount)
>    {
>    ...
>    }
>    ```
>
> 2. **Batch** オブジェクトでイベントソースを初期化します。
>
>    ```csharp theme={null}
>    FCEngine.IProject project = Engine.OpenProject( "D:\\Sample.fcproj" );
>    FCEngine.IBatch batch = project.Batches.AddNew( "TestBatch" );
>    FCEngine.IBatchEvents_Event eventSource = batch as FCEngine.IBatchEvents_Event;
>    ```
>
> 3. イベントハンドラーをイベントソースに接続し、**Batch** オブジェクトを使用して処理を行った後、ハンドラーを切断します。
>
>    ```csharp theme={null}
>    FCEngine.IBatchEvents_OnBatchProgressEventHandler batchOnProgressHandler =
>    new FCEngine.IBatchEvents_OnBatchProgressEventHandler( eventSource_OnBatchProgress );
>    eventSource.OnBatchProgress += batchOnProgressHandler;
>    ...
>    eventSource.OnBatchProgress -= batchOnProgressHandler;
>    ```

接続可能なオブジェクトの詳細については、COM に関するドキュメントを参照してください。

## 関連項目

[**IBatchEvents**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/ibatchevents.md)

[**ICollectionEvents**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/icollectionevents.md)

[**IProcessingEvents**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/iprocessingevents.md)
