////////////////////////////////////////////////////////////////////////////////
//
// MacroObj.cpp -   Visual C++
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h>
#include "MacroObj.h"
#include "events\CBaseEvent.h"
#include "events\ABaseEvent.h"

#include "PropManager\cPropMen.h"
#include "events\ApplicationEvent.h"
#include "Orifice.h"

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



//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE MacroObjDLL = { NULL, NULL };
HINSTANCE g_hInstance = NULL;
IApplicationPtr appl( NULL );               


void OnProcessDetach();                                      //  


//-------------------------------------------------------------------------------
//   
//    DLL
// ---
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
  g_hInstance = hInstance;

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("MACROOBJ.AWX Initializing!\n");

		AfxInitExtensionModule(MacroObjDLL, hInstance);

		new CDynLinkLibrary(MacroObjDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("MACROOBJ.AWX Terminating!\n");
    OnProcessDetach();
		AfxTermExtensionModule(MacroObjDLL);
	}
	return 1;   // ok
}


//-------------------------------------------------------------------------------
//     API
// ---
void GetNewKompasAPI() 
{
	if ( !( IApplication * )appl ) 
  {
		CString filename;
	
    if( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) 
    {
			filename.ReleaseBuffer( 255 );
			CString libname;

      libname = LoadStr( IDR_API7 );    // kAPI7.dll
			filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind(_T('\\')) + 1)), 
												libname );

			HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI7.dll
		
      if(  hAppAuto ) 
      {
				//      KompasApplication  
        typedef LPDISPATCH ( WINAPI * FCreateKompasApplication )(); 
				 
        FCreateKompasApplication pCreateKompasApplication = 
					(FCreateKompasApplication)GetProcAddress( hAppAuto, "CreateKompasApplication" );	
				
        if ( pCreateKompasApplication )
					appl = IDispatchPtr ( pCreateKompasApplication(), false /*AddRef*/); //   Application
				FreeLibrary( hAppAuto );  
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
unsigned int WINAPI LIBRARYID()
{
  return IDR_LIBID;
}

extern long choiceID;
//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  switch ( comm )
  {
     case 1: 
    {
      GetNewKompasAPI();
      
      PropertyManagerObject::macroObj = new Orifice();
      if ( PropertyManagerObject::macroObj->InitProcessParam( ) )
      {
        PropertyManagerObject::macroObj->ChoiceFaces();
        PropertyManagerObject::macroObj->EndProcess(); 
      }  


      break;
    }
  }
}


//-------------------------------------------------------------------------------
//  
// ---
void OnProcessDetach()
{
  //   
  if ( PropertyManagerObject::macroObj )
    PropertyManagerObject::macroObj->EndProcess();

  ABaseEvent::TerminateEvents();
  cBaseEvent::TerminateEvents();
  ABaseEvent::DestroyList();
  cBaseEvent::DestroyList();
  appl = NULL;
}

 
//-----------------------------------------------------------------------------
//      
// ---
int WINAPI LibInterfaceNotifyEntry( IDispatch * application ) 
{
  if ( !(bool)appl )
    appl = application; //   
  return 0;
}


//-------------------------------------------------------------------------------
//   API
// ---
int WINAPI LibIsOnApplication7() 
{
  return 1;
}


//------------------------------------------------------------------------------
//    
// ---
CString LoadStr( int strID ) 
{
	TCHAR temp[_MAX_PATH];
	LoadString( MacroObjDLL.hModule, strID, temp, _MAX_PATH ); 
	return temp;
}


