/// <summary>basMain
    /// Inicializa a API do Retail
    /// Lança uma exceção se falhar
    /// </summary>
    /// <param name="companyId">Identificador da empresa a Abrir</param>
    public static void Initialize(ApplicationEnum apiKind, string companyId, bool debugMode)
    {
        apiInitialized = false;

        //
        Terminate();
        //
        // Init
        // 1. DataProvider (RTLData16)
        // 2. System (RTLSystem16)
        // 3. DataLayer (RTLData13)
        // 4. Core (RTLCore16)
        rtlDataGlobals   = new RTLData16.GlobalSettings();
        rtlSystemGlobals = new RTLSystem16.GlobalSettings();
        rtlDLGlobals     = new RTLDL16.GlobalSettings();
        rtlCoreGlobals   = new RTLCore16.GlobalSettings();
        rtlPrintGlobals  = new RTLPrint16.GlobalSettings();
        rtlBLGlobals     = new RTLBL16.GlobalSettings();
        //
        switch (apiKind)
        {
        case ApplicationEnum.SageRetail:
            systemStarter = new RTLAPIPRTL16.SystemStarter();
            break;

        case ApplicationEnum.SageGC:
            systemStarter = new SGCOAPIPRTL16.SystemStarter();
            break;
        }
        systemStarter.DebugMode = debugMode;
        if (systemStarter.Initialize(companyId) != 0)
        {
            string initError = systemStarter.InitializationError;
            systemStarter = null;
            throw new Exception(initError);
        }
        // Eventos de erros e avisos vindos da API
        dataManagerEvents = (RTLData16.DataManagerEventsClass)rtlDataGlobals.DataManager.Events;
        dataManagerEvents.__DataManagerEvents_Event_WarningMessage += dataManagerEvents___DataManagerEvents_Event_WarningMessage;
        dataManagerEvents.__DataManagerEvents_Event_WarningError   += dataManagerEvents___DataManagerEvents_Event_WarningError;
        dataManagerEvents.__DataManagerEvents_Event_Message        += DataManagerEvents___DataManagerEvents_Event_Message;

        //
        apiInitialized = true;
        //
        if (APIStarted != null)
        {
            APIStarted(null, null);
        }
    }
 /// <summary>
 /// Termina a ligação à API e liberta todos os recursos
 /// </summary>
 public static void Terminate()
 {
     if (apiInitialized)
     {
         //1. QuickSearch
         if (rtlSystemGlobals != null)
         {
             rtlSystemGlobals.DisposeQuickSearch();
         }
         //3. Business globals
         if (rtlBLGlobals != null)
         {
             rtlBLGlobals.Dispose();
             rtlBLGlobals = null;
         }
         //4. Dispose CORE Global Settings
         if (rtlCoreGlobals != null)
         {
             rtlCoreGlobals.Dispose();
             //System.Runtime.InteropServices.Marshal.ReleaseComObject(rtlCoreGlobals);
             rtlCoreGlobals = null;
         }
         //2. Dispose Printing Manager
         if (rtlPrintGlobals != null)
         {
             rtlPrintGlobals.Dispose();
             rtlPrintGlobals = null;
         }
         //5. DISPOSE DataLayer Global Settings
         if (rtlDLGlobals != null)
         {
             rtlDLGlobals.Dispose();
             //System.Runtime.InteropServices.Marshal.ReleaseComObject(rtlDLGlobals);
             rtlDLGlobals = null;
         }
         //6. Dispose DataProvider
         if (rtlDataGlobals != null)
         {
             rtlDataGlobals.DataManager.CloseConnections();
             rtlDataGlobals.Dispose();
             //System.Runtime.InteropServices.Marshal.ReleaseComObject(rtlDataGlobals);
             rtlDataGlobals = null;
         }
         //7. Dispose System
         if (rtlSystemGlobals != null)
         {
             rtlSystemGlobals.Dispose();
             //System.Runtime.InteropServices.Marshal.ReleaseComObject(rtlSystemGlobals);
             rtlSystemGlobals = null;
         }
         // Dispose System manager
         if (rtlSystemManager != null)
         {
             rtlSystemManager = null;
         }
         //
         apiInitialized = false;
         //
         // Fire event
         if (APIStopped != null)
         {
             APIStopped(null, null);
         }
     }
 }