/*
*********************************************************************
*
*	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: Implmentation of IEliza interface for local server
*
*********************************************************************
*/

#include <windows.h>

#include "celiza.h"

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

extern GUID GUID_ElizaObject ;
extern GUID GUID_DualElizaTypeLib ;
extern GUID GUID_DualElizaInterface ;

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

/*
Get the type info of this object.
*/
	ITypeLib* pTypeLib;
	HRESULT hr ;

	hr = LoadRegTypeLib(GUID_DualElizaTypeLib, 1, 0, 0, &pTypeLib);

	if (hr != S_OK)
	{
		MessageBox (NULL, L"Couldn't load type lib", L"", 0) ;
	}

	hr = pTypeLib->GetTypeInfoOfGuid(GUID_DualElizaInterface, &m_pTypeInfo);
	if (hr != S_OK)
	{
		MessageBox (NULL, L"Couldn't load type info", L"", 0) ;
	}

	pTypeLib->Release();

    return;
}


CEliza::~CEliza(void)
{
	MessageBox (NULL, L"Eliza dtor", L"", 0) ;
	if (m_pTypeInfo)
	{
		m_pTypeInfo->Release( ) ;
	}
	SendMessage (m_hWnd, WM_CLOSE, 0, 0) ;

    return;
}



STDMETHODIMP CEliza::QueryInterface(REFIID riid, LPVOID *ppv)
{
    if (riid == IID_IUnknown || riid == IID_IDualEliza
		|| riid == IID_IDispatch)
	{
        *ppv = (LPVOID)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 ;
}


STDMETHODIMP CEliza::Complain (BSTR *pIn, BSTR *pOut)
{
	if (*pIn != 0x00)
	{
		MessageBox (NULL, *pIn, L"This complaint", 0) ;

 		TCHAR out [256] ;
		wsprintf (out, TEXT("Client: %s%c%c"), *pIn, 0x0D, 0x0A) ;
		SendMessage (m_hEdit, EM_SETSEL, (WPARAM) -1, (LPARAM)-1) ;
		SendMessage (m_hEdit, EM_REPLACESEL, 0, (LPARAM) out) ;

		TCHAR stringOut [256] ;
		stringOut [0] = 0x00 ;

		Eliza (*pIn, stringOut) ;
		SysReAllocString (pOut, stringOut) ;

		MessageBox (NULL, stringOut, L"This response", 0) ;

		SendMessage (m_hEdit, EM_SETSEL, (WPARAM) -1, (LPARAM)-1) ;
		SendMessage (m_hEdit, EM_REPLACESEL, 0, (LPARAM) stringOut) ;
	}

	return NOERROR ;
}

STDMETHODIMP CEliza::GetTypeInfoCount(UINT* pctinfo)
{	
	*pctinfo = 1; 
	return S_OK;
}

STDMETHODIMP CEliza::GetTypeInfo (UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
{
	*pptinfo = m_pTypeInfo ;
	m_pTypeInfo->AddRef( ) ;
	return S_OK ;
}

STDMETHODIMP CEliza::GetIDsOfNames (REFIID riid, LPOLESTR* rgszNames, UINT cNames,
		LCID lcid, DISPID* rgdispid)
{
	return m_pTypeInfo->GetIDsOfNames(rgszNames, cNames, rgdispid);
}

STDMETHODIMP CEliza::Invoke(DISPID dispidMember, REFIID riid,
		LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
		EXCEPINFO* pexcepinfo, UINT* puArgErr)
{
	HRESULT hr = m_pTypeInfo->Invoke(this, dispidMember, wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr);
	return hr ;
}
