/*
*********************************************************************
*
*	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:CELIZA.CPP
*
*	Purpose: Implementation of IEliza custom interface
*
*********************************************************************
*/

#include <windows.h>

#include "celiza.h"

extern "C" void Eliza (TCHAR *InStr, TCHAR *OutStr) ;

CEliza::CEliza (void)
{
    m_RefCount = 1 ;
    return;
}


CEliza::~CEliza(void)
{
    return;
}



STDMETHODIMP CEliza::QueryInterface(REFIID riid, LPVOID *ppv)
{
    if (riid == IID_IUnknown || riid == IID_IEliza)
	{
        *ppv = (IEliza *) this;
		AddRef( ) ;
		return NOERROR;
	}
	else if (riid == IID_ISupportErrorInfo)
	{
        *ppv = (ISupportErrorInfo *) this;
		AddRef( ) ;
		return NOERROR;
	}
	else
	{
    	*ppv = NULL ;
    	return E_NOINTERFACE ;
	}
        
}


STDMETHODIMP_(ULONG) CEliza::AddRef(void)
{
    return ++m_RefCount;
}


STDMETHODIMP_(ULONG) CEliza::Release(void)
{
	m_RefCount -- ;

    if (m_RefCount == 0)
	{
        delete this;
		return 0 ;
	}

    return m_RefCount ;
}







/*
 * CEliza::Complain
 *
 * Purpose:
 *    Receive complaint from client.  Prepare Eliza pseudo-shrink reply
 *    based on its content.
 *
 * Parameters:
 *    LPSTR pIn -- pointer to input string
 *    LPSTR pOut -- pointer to buffer for output string
 *
 * Return Value:
 *  HRESULT         NOERROR always.
 */

STDMETHODIMP CEliza::Complain (LPOLESTR pIn, LPOLESTR *pOut)
{
	
/*
Check to see if caller is complaining about his mother. That's not 
allowed, so create, populate, and set an error info object.
*/
	
	if (wcsstr (pIn, L"mother") != NULL)
	{
/*
Create generic COM error object.
*/

		ICreateErrorInfo *pCreateErrorInfo ;
		CreateErrorInfo (&pCreateErrorInfo) ;

/*
Populate the pieces we care about.
*/
		pCreateErrorInfo->SetDescription (L"No complaining about your mother") ;
		pCreateErrorInfo->SetSource (L"Essence of COM ElizaIPS.DLL") ;

/*
Query for the IErrorInfo interface needed to set it into COM's
logical thread.
*/

		IErrorInfo *pErrorInfo ;
		pCreateErrorInfo->QueryInterface (IID_IErrorInfo, (void**)&pErrorInfo) ;

/*
Set it into COM's error context.
*/
		SetErrorInfo (0, pErrorInfo) ;

/*
Clean up.
*/
		pCreateErrorInfo->Release( ) ;
		pErrorInfo->Release ( ) ;
/*
Return error code.
*/
		return E_FAIL ;

	}

/*
Otherwise, proceed as usual.

Allocate space for 256 wide characters
*/

	WCHAR *pStr = (WCHAR *) CoTaskMemAlloc (512) ;

	Eliza (pIn, pStr) ;
	*pOut = pStr ;
	return NOERROR ;
}


/*
Caller is checking to see if we provide error information on the specified
interface. We only do this on the IEliza interface. If that's the one he's
asking for, then say yes, otherwise say no.
*/

STDMETHODIMP CEliza::InterfaceSupportsErrorInfo (REFIID riid)
{
	if (riid == IID_IEliza)
	{
		return S_OK ;
	}
	else
	{
		return S_FALSE ;
	}
}

