/*****************************************************************************
* Copyright (c) 1990-98 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
*
*****************************************************************************/


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

#include "stdafx.h"
#include <afxdllx.h>
#include "ServiceAsyn.h"
#include "PortSerial.h"
#include "PortStdCom.h"

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


HINSTANCE g_hInstance = NULL;

static AFX_EXTENSION_MODULE DigiAsynDLL = { 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("DIGIASYN.DLL: Initializing!\n");

      g_hInstance = hInstance;
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule(DigiAsynDLL, hInstance))
			return 0;

		// 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(DigiAsynDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE("DIGIASYN.DLL: Terminating!\n");
		// Terminate the library before destructors are called
		AfxTermExtensionModule(DigiAsynDLL);
	}
	return 1;   // ok
}

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

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

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

      CConfigObject * pNewObj = NULL;

           if (strName == DXB_REGVAL_LINE_SERVICE_ASYN) pNewObj = new CServiceAsyn(hKeyRoot, strRegPath, pParent);
      else if (strName == DXB_REGVAL_PORT_TYPE_SERIAL)  pNewObj = new CPortSerial(hKeyRoot, strRegPath, pParent);
      else if (strName == DXB_REGVAL_PORT_TYPE_STD_COM)  pNewObj = new CPortStdCom(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("DIGIASYN.DLL: DeleteObj()\n");

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

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

   return;
}
