Skip to main content
This function deinitializes ABBYY FineReader Engine. The function must be called for deinitialization of the Engine object obtained using the InitializeEngine function. Prior to the deinitialization, you must release all references to FineReader Engine objects.

Syntax

C++

HRESULT __stdcall DeinitializeEngine();

C#

int DeinitializeEngine();

Visual Basic .NET

Private Declare Function DeinitializeEngine Lib "FREngine.dll" () As Integer

Return values

This function returns E_FAIL if not all objects are released. In that case, you can get the list of not released objects using the IEngine::StartLogging method. The function may return the standard return values of ABBYY FineReader Engine functions.

Remarks

Do not initialize and deinitialize ABBYY FineReader Engine at the entry points of other dynamic libraries and also in constructors and destructors of static and global objects implemented in dynamic libraries, because they are called at the dynamic library entry points.
A user should initialize and deinitialize ABBYY FineReader Engine elsewhere. For example, in the main or WinMain function of an executable module.
: In Windows, this restriction is due to the fact that the Win32 LoadLibrary and FreeLibrary functions are not re-entrant.

Samples

IEngine* FineReaderEngine = 0;
HMODULE EngineLibraryHandle = 0;
static HRESULT deinitializeEngine()
{
 // Release Engine object
 if( FineReaderEngine != 0 ) {
  FineReaderEngine->Release();
  FineReaderEngine = 0;
 }
 // Calling DeinitializeEngine
 typedef HRESULT (STDAPICALLTYPE* DeinitializeEngineFunc)();
 DeinitializeEngineFunc pDeinitializeEngine =
  (DeinitializeEngineFunc)GetProcAddress( EngineLibraryHandle, "DeinitializeEngine" );
 if( pDeinitializeEngine == 0 || FAILED( pDeinitializeEngine() ) ) {
  setLastErrorInfo( L"Can't unload FineReader Engine." );
  return E_UNEXPECTED;
 }
 return S_OK;
}
public class EngineLoader : IDisposable
{
    // Unload FineReader Engine
    public void Dispose()
    {
        if (engine == null)
        {
            // Engine was not loaded
            return;
        }
        engine = null;
        // Deleting all objects before FreeLibrary call
        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;
        // throwing exception after cleaning up
        Marshal.ThrowExceptionForHR(hresult);
    }
    // Kernel32.dll functions
    [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 functions
    [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 variables
    private FREngine.IEngine engine = null;
    // Handle to FREngine.dll
    private IntPtr dllHandle = IntPtr.Zero;
    private InitializeEngine initializeEngine = null;
    private DeinitializeEngine deinitializeEngine = null;
    private DllCanUnloadNow dllCanUnloadNow = null;
}
The function is used in all code samples except the Windows EnginesPool sample.

See also

InitializeEngine Different Ways to Load the Engine Object