/*****************************************************************************
* Copyright (c) 1990-99 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/


// DigiFWrk.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <afxdllx.h>
#include "Adapter.h"
#include "ProtocolNone.h"
#include "ServiceNone.h"
#include "Resource.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


HINSTANCE g_hInstance = NULL;

static AFX_EXTENSION_MODULE FrameWrkDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	// Remove this if you use lpReserved
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE("DIGIFWRK.DLL Initializing!\n");

		g_hInstance = hInstance;
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule(FrameWrkDLL, hInstance))
			return 0;
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE("DIGIFWRK.DLL Terminating!\n");
		// Terminate the library before destructors are called
		AfxTermExtensionModule(FrameWrkDLL);
	}
	return 1;   // ok
}

void FW_EXPORTED_API AttachToFrameWork(void)
{
	TRACE("DIGIFWRK.DLL Attaching!\n");

   try
   {
      /* Removed, it is unnecessary to check against this
      static BOOL fAttachedToFrameWork = FALSE;
      AffirmThrow(!fAttachedToFrameWork);
      */

      // Insert this DLL into the resource chain
      // NOTE: If this Extension DLL is being implicitly linked to by
      //  an MFC Regular DLL (such as an ActiveX Control)
      //  instead of an MFC application, then you will want to
      //  remove this line from DllMain and put it in a separate
      //  function exported from this Extension DLL.  The Regular DLL
      //  that uses this Extension DLL should then explicitly call that
      //  function to initialize this Extension DLL.  Otherwise,
      //  the CDynLinkLibrary object will not be attached to the
      //  Regular DLL's resource chain, and serious problems will
      //  result.
      new CDynLinkLibrary(FrameWrkDLL);
      /* Removed
      fAttachedToFrameWork = TRUE;
      */
   }
   catch (CAffirmException ae)
   {
   }
}

BOOL APIENTRY NewObj(CConfigObject const ** const ppObj, CString const & strName, HKEY const hKeyRoot, CString const & strRegPath, CConfigObject const * const pParent)
{
   BOOL fSuccess = FALSE;

   TRACE("DIGIFWRK.DLL: NewObj(%s)\n", (LPCTSTR)strName);

   try
   {
      AffirmThrow(ppObj != NULL);
#if defined(_DEBUG)
      if (*ppObj != NULL) TRACE("DIGIFWRK.DLL: *ppObj != NULL\n");
#endif

      CConfigObject * pNewObj = NULL;

      // **** IMPORTANT ****
      // add an entry to this table any time a new adapter is defined
      // 
      static TCHAR const * const apszAdapterTable[] =
      {
         DXB_REGVAL_OPTION_ENUM_CLYDE,
         DXB_REGVAL_OPTION_ENUM_SYNC570,
         DXB_REGVAL_OPTION_ENUM_ACCELEPORT_2000,
      };

      // see if strName is an adapter first
      for(int i = 0; i < ElementCount(apszAdapterTable); i++)
      {
         if (strName == apszAdapterTable[i])
         {
            pNewObj = new CAdapter(hKeyRoot, strRegPath, pParent);
            break;
         }
      }

      if (pNewObj == NULL)
      {
         // guess strName wasn't an adapter, see if it's something else
              if (strName == DXB_REGVAL_PROTOCOL_ENUM_NONE) pNewObj = new CProtocolNone(hKeyRoot, strRegPath, pParent);
         else if (strName == DXB_REGVAL_LINE_SERVICE_NONE)  pNewObj = new CServiceNone(hKeyRoot, strRegPath, pParent);
         else AffirmThrow(FALSE);
      }

      AffirmThrow(pNewObj != NULL);
      *ppObj  = pNewObj;
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

void APIENTRY DeleteObj(CConfigObject const ** const ppObj)
{
   TRACE("DIGIFWRK.DLL: DeleteObj()\n");

   try
   {
      AffirmThrow(ppObj != NULL);
      AffirmThrow(*ppObj != NULL);

      delete *ppObj;
      *ppObj = NULL;
   }
   catch (CAffirmException ae)
   {
   }

   return;
}
