/*
*********************************************************************
*
*	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: AutoEliza.CPP
*
*	Purpose: IEliza expressed via an MFC dispinterface for comparison purposes
*
*********************************************************************
*/
// AutoEliza.cpp : implementation file
//

#include "stdafx.h"
#include "elizaau.h"
#include "AutoEliza.h"
#include "winnls.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAutoEliza

IMPLEMENT_DYNCREATE(CAutoEliza, CCmdTarget)

CAutoEliza::CAutoEliza()
{
	EnableAutomation();
	
	// To keep the application running as long as an OLE automation 
	//	object is active, the constructor calls AfxOleLockApp.
	
	AfxOleLockApp();
}

CAutoEliza::~CAutoEliza()
{
	// To terminate the application when all objects created with
	// 	with OLE automation, the destructor calls AfxOleUnlockApp.
	
	AfxOleUnlockApp();
}


void CAutoEliza::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CCmdTarget::OnFinalRelease();
}


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

BEGIN_DISPATCH_MAP(CAutoEliza, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CAutoEliza)
	DISP_FUNCTION(CAutoEliza, "Complain", Complain, VT_EMPTY, VTS_PBSTR VTS_PBSTR)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IAutoEliza to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {95ADE9C6-B7B5-11CF-8A66-00AA00A58097}
static const IID IID_IAutoEliza =
{ 0x95ade9c6, 0xb7b5, 0x11cf, { 0x8a, 0x66, 0x0, 0xaa, 0x0, 0xa5, 0x80, 0x97 } };

BEGIN_INTERFACE_MAP(CAutoEliza, CCmdTarget)
	INTERFACE_PART(CAutoEliza, IID_IAutoEliza, Dispatch)
END_INTERFACE_MAP()

// {95ADE9C7-B7B5-11CF-8A66-00AA00A58097}
IMPLEMENT_OLECREATE(CAutoEliza, "EssenceOfOle.AutoElizaInproc", 0x95ade9c7, 0xb7b5, 0x11cf, 0x8a, 0x66, 0x0, 0xaa, 0x0, 0xa5, 0x80, 0x97)

/////////////////////////////////////////////////////////////////////////////
// CAutoEliza message handlers

void Eliza (char *, char *) ;


void CAutoEliza::Complain(BSTR *pIn, BSTR *pOut) 
{
	if (**pIn != 0x00)
	{
		char in [256], out [256] ;
		WCHAR wOut [256] ;

		WideCharToMultiByte (CP_ACP, 0, *pIn, -1, in, sizeof(in), NULL, NULL) ;
		Eliza (in, out) ;
		MultiByteToWideChar (CP_ACP, 0, out, -1, wOut, sizeof(wOut)) ;
		*pOut = SysAllocString (wOut) ;
	}
}
