/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*	File Name: MFCELIZA.CPP
*
*	Purpose: IEliza custom interface in MFC local server
*
*********************************************************************
*/

// MfcEliza.cpp : implementation file
//

#include "stdafx.h"
#include "mfclosv.h"
#include "MfcEliza.h"
#include "winnls.h"

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

UINT g_ObjectCount = 0 ;

/////////////////////////////////////////////////////////////////////////////
// CMfcEliza

IMPLEMENT_DYNCREATE(CMfcEliza, CCmdTarget)

CMfcEliza::CMfcEliza()
{
	g_ObjectCount ++ ;
}

CMfcEliza::~CMfcEliza()
{
	g_ObjectCount -- ;

	if (g_ObjectCount == 0) 
	{
		PostQuitMessage (0) ;
	}
}

/*
Instantiate the COleObject factory
*/

AFX_DATADEF COleObjectFactory CMfcEliza::ms_ElizaFactory  (GUID_MfcEliza, 
		RUNTIME_CLASS(CMfcEliza), 
		FALSE, TEXT("EssenceOfOle.Eliza.MfcCustomLocal")) ;


BEGIN_INTERFACE_MAP (CMfcEliza, CCmdTarget)
	INTERFACE_PART (CMfcEliza, IID_IEliza, Eliza)
END_INTERFACE_MAP( ) 


BEGIN_MESSAGE_MAP(CMfcEliza, CCmdTarget)
	//{{AFX_MSG_MAP(CMfcEliza)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcEliza message handlers

extern void Eliza (char *InStr, char *OutStr) ;

HRESULT CMfcEliza::XEliza::Complain (LPOLESTR pIn, LPOLESTR pOut)
{
	if (*pIn != 0x00)
	{
		char in [512], out [512] ;

		WideCharToMultiByte (CP_ACP, 0, pIn, -1, in, sizeof(in), NULL, NULL) ;
		Eliza (in, out) ;
		MultiByteToWideChar (CP_ACP, 0, out, -1, pOut, 256) ;
	}
	return S_OK ;
}

/*
Implementation of nested class AddRef( )method
*/

ULONG CMfcEliza::XEliza::AddRef( )
{
/*
Set up pThis pointer to access outer object
*/
	METHOD_PROLOGUE (CMfcEliza, Eliza)

/*
Use pThis pointer to delegate to outer object's AddRef( ) method.
*/
	return pThis->ExternalAddRef( ) ;
}

ULONG CMfcEliza::XEliza::Release( ) 
{
	METHOD_PROLOGUE (CMfcEliza, Eliza) ;
	return pThis->ExternalRelease( ) ;
}

long CMfcEliza::XEliza::QueryInterface (REFIID riid, LPVOID *ppv)
{
	METHOD_PROLOGUE (CMfcEliza, Eliza) ;
	return pThis->ExternalQueryInterface (&riid, ppv) ;
}
