////////////////////////////////////////////////////////////////////////////////
//
// ConverterTst.cpp -     LibConverter
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h>
#include "ConverterTst.h"
#include "AtlBase.h"


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


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE ConverterTstDLL = { NULL, NULL };
HINSTANCE g_hInstance = NULL;

IApplicationPtr newKompasAPI( NULL );               

void OnProcessDetach();                            //  


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

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

		AfxInitExtensionModule(ConverterTstDLL, hInstance);

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


//-------------------------------------------------------------------------------
//     API7 Kompas
// ---
void GetNewKompasAPI() 
{
	if ( !( IApplication *)newKompasAPI ) 
  {
		CString filename;
	
    if( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) 
    {
			filename.ReleaseBuffer( 255 );
			
      CString 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 )
					newKompasAPI = IDispatchPtr( pCreateKompasApplication(), false/*AddRef*/ ); //   Application
				FreeLibrary( hAppAuto );  
			}
		}
	}
}


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


void TestConverter(); //        

//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  switch ( comm )
  {
    case 1: //  
    {
      //        
      TestConverter();
      break;
    }
  }
}


//-------------------------------------------------------------------------------
//  
// ---
void OnProcessDetach()
{
  newKompasAPI = NULL;
}

//     
#define CONVERTERS_PATH _T("SOFTWARE\\ASCON\\KOMPAS-3D\\Converters\\MyLibConverter")
#define MAX_BUFFER       256 


//-------------------------------------------------------------------------------
//    LibConverter
// ---
void TestConverter()
{
  int err = 0;
  //    API7 Kompas
  GetNewKompasAPI();
  //  
  if ( newKompasAPI )
  {
    CRegKey key;
    // ,    
    if ( key.Open( HKEY_LOCAL_MACHINE, CONVERTERS_PATH, KEY_READ ) == ERROR_SUCCESS )
    {
      TCHAR path[MAX_BUFFER];
      DWORD cbPath = MAX_BUFFER; 
      //    
      if ( key.QueryValue( path, _T("Path"), &cbPath ) == ERROR_SUCCESS )
      {
        //   
        IConverterPtr LibConverter( newKompasAPI->GetConverter( _bstr_t(path) ) );
        if ( LibConverter )
        {
          //    
          ILibConverterParamPtr myConvParam( LibConverter->ConverterParameters( 0/*command*/) );
          if ( myConvParam )
          {
            //  "  ?"
            int okStyle = YesNoT( (LPTSTR)(LPCTSTR)LoadStr(IDS_STYLE_DOC) );
            //       
            myConvParam->PutSaveLineStyle( !!okStyle );
          }
          //   
          int res = LibConverter->Convert( _T("c:\\1.cdw"), _T("c:\\1.mycdw"), libConv_Exp_cdw, FALSE/*showParam*/ );
          if ( !res )
            err = IDS_ERR_CONVERT; // "  "
          else
            LibMessage( IDS_CONVERT_SUCCESS ); // "  "
        }
        else
          err = IDS_NO_GET_CONVERTER_INTERFACE; // "    ."
      }
      else
        err = IDS_NO_GET_CONVERTER; // "  "
    }
    else
      err = IDS_NO_GET_CONVERTER; // "  "
  }
  else
    err = IDS_NO_GET_NEW_API; // "   API7"
  if ( err )
    //  
    LibMessage( err );
}


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

//------------------------------------------------------------------------------
//  
// ---
int LibMessage( LPCTSTR str, int flags ) 
{
  int res = 0;

  if ( str && str[0] ) {                  //  
    int enabse = ::IsEnableTaskAccess();  //  
    if ( enabse )                         //     
      ::EnableTaskAccess(0);              //  

    //                                      
    res = MessageBox( (HWND) GetHWindow(), str, LoadStr(LIBRARYID()), flags );

    if ( enabse )                         //      
      ::EnableTaskAccess(1);              //    
  }

  return res;
}


//------------------------------------------------------------------------------
//  
// ---
int LibMessage( int strId, int flags ) 
{
  return ::LibMessage( LoadStr(strId), flags );
}

