> ## 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.

# DeinitializeEngine 関数

> ABBYY FineReader Engine API の DeinitializeEngine 関数 — この関数は ABBYY FineReader Engine の終了処理を行います。

この関数は、ABBYY FineReader Engine の終了処理を行います。この関数は、[InitializeEngine](/ja/fine-reader/engine/api-reference/functions/initializeengine-function) 関数を使用して取得した Engine オブジェクトの終了処理を行うために呼び出す必要があります。終了処理を行う前に、FineReader Engineオブジェクトへのすべての参照を解放しておく必要があります。

<div id="syntax">
  ## 構文
</div>

<div id="c">
  ### C++
</div>

```cpp theme={null}
HRESULT __stdcall DeinitializeEngine();
```

### C\#

```csharp theme={null}
int DeinitializeEngine();
```

<div id="visual-basic-net">
  ### Visual Basic .NET
</div>

```csharp theme={null}
Private Declare Function DeinitializeEngine Lib "FREngine.dll" () As Integer
```

<div id="return-values">
  ## 戻り値
</div>

すべてのオブジェクトが解放されていない場合、この関数は E\_FAIL を返します。その場合は、[IEngine::StartLogging](/ja/fine-reader/engine/api-reference/engine-object-iengine-interface/supplementary-methods/startlogging-method) メソッドを使用して、解放されていないオブジェクトの一覧を取得できます。この関数は、[ABBYY FineReader Engine 関数の標準的な戻り値](/ja/fine-reader/engine/api-reference/return-codes)を返すこともあります。

<div id="remarks">
  ## 備考
</div>

<Warning>
  ABBYY FineReader Engine の初期化および終了処理を、他の動的ライブラリのエントリポイントで行わないでください。また、動的ライブラリ内で実装された静的オブジェクトおよびグローバルオブジェクトのコンストラクターやデストラクター内でも行わないでください。これらは動的ライブラリのエントリポイントで呼び出されるためです。
</Warning>

ABBYY FineReader Engine の初期化および終了処理は、それ以外の場所で行ってください。たとえば、実行可能モジュールの main 関数または WinMain 関数で行います。

<Note>
  Windows では、この制限は Win32 の LoadLibrary 関数と FreeLibrary 関数がリエントラントではないことに起因します。
</Note>

<div id="samples">
  ## サンプル
</div>

<Accordion title="C++ (COM) コード">
  ```cpp theme={null}
  IEngine* FineReaderEngine = 0;
  HMODULE EngineLibraryHandle = 0;
  static HRESULT deinitializeEngine()
  {
   // Engine オブジェクト を解放する
   if( FineReaderEngine != 0 ) {
    FineReaderEngine->Release();
    FineReaderEngine = 0;
   }
   // DeinitializeEngine を呼び出す
   typedef HRESULT (STDAPICALLTYPE* DeinitializeEngineFunc)();
   DeinitializeEngineFunc pDeinitializeEngine =
    (DeinitializeEngineFunc)GetProcAddress( EngineLibraryHandle, "DeinitializeEngine" );
   if( pDeinitializeEngine == 0 || FAILED( pDeinitializeEngine() ) ) {
    setLastErrorInfo( L"FineReader Engine をアンロードできません。" );
    return E_UNEXPECTED;
   }
   return S_OK;
  }
  ```
</Accordion>

<Accordion title="C# コード">
  ```csharp theme={null}
  public class EngineLoader : IDisposable
  {
      // FineReader Engine をアンロードする
      public void Dispose()
      {
          if (engine == null)
          {
              // Engine はロードされていない
              return;
          }
          engine = null;
          // FreeLibrary を呼び出す前にすべての object を削除する
          GC.Collect();
          GC.WaitForPendingFinalizers();
          GC.Collect();
          int hresult = deinitializeEngine();
   
          hresult = dllCanUnloadNow();
          if (hresult == 0)
          {
              FreeLibrary(dllHandle);
          }
          dllHandle = IntPtr.Zero;
          initializeEngine = null;
          deinitializeEngine = null;
          dllCanUnloadNow = null;
          // クリーンアップ後に例外をスローする
          Marshal.ThrowExceptionForHR(hresult);
      }
      // Kernel32.dll の関数
      [DllImport("kernel32.dll")]
      private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);
      private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
      [DllImport("kernel32.dll")]
      private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
      [DllImport("kernel32.dll")]
      private static extern bool FreeLibrary(IntPtr hModule);
      // FREngine.dll の関数
      [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
      private delegate int InitializeEngine( string customerProjectId, string LicensePath, string LicensePassword, , , , ref FREngine.IEngine engine);
      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      private delegate int DeinitializeEngine();
      [UnmanagedFunctionPointer(CallingConvention.StdCall)]
      private delegate int DllCanUnloadNow();
      // private 変数
      private FREngine.IEngine engine = null;
      // FREngine.dll へのハンドル
      private IntPtr dllHandle = IntPtr.Zero;
      private InitializeEngine initializeEngine = null;
      private DeinitializeEngine deinitializeEngine = null;
      private DllCanUnloadNow dllCanUnloadNow = null;
  }
  ```
</Accordion>

この関数は、Windows [EnginesPool](/ja/fine-reader/engine/guided-tour/samples#enginespool) サンプルを除くすべての[コード サンプル](/ja/fine-reader/engine/guided-tour/samples)で使用されます。

<div id="see-also">
  ## 関連項目
</div>

[InitializeEngine](/ja/fine-reader/engine/api-reference/functions/initializeengine-function)

[Engine オブジェクトをロードするさまざまな方法](/ja/fine-reader/engine/guided-tour/advanced-techniques/programming-aspects/different-ways-to-load-engine)
