/*
*********************************************************************
*
*	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: WINMAIN.CPP
*
*	Purpose: Windows entry point for IEliza custom interface local server
*
*********************************************************************
*/

#include <windows.h>
#include "iclassf.h"
#include "pipesv.h"

HINSTANCE hInst ;
int ObjectCount = 0, LockCount = 0 ;
HWND hMainWnd ;

/*
Class ID for the Eliza object
*/

// {E8D73100-C4F9-11ce-AD55-00608C86B89C}
static const GUID GUID_ElizaObject = 
{ 0xe8d73100, 0xc4f9, 0x11ce, { 0xad, 0x55, 0x0, 0x60, 0x8c, 0x86, 0xb8, 0x9c } };


int WINAPI WinMain(HANDLE  hInstance, HANDLE  hPrevInstance, LPSTR   lpCmdLine,
    int     nShowCmd)
{
    MSG msg;
   	WNDCLASS wc ;

	hInst = hInstance ;

/*
Initialize COM
*/

	CoInitialize (NULL) ;
/*
Check for OLE server startup.  If found, register class factory.
*/


	DWORD dwRegister ;

	if (strstr (lpCmdLine, "Embedding"))
	{
		LPCLASSFACTORY pCF = new CClassFactory ( ) ;

		CoRegisterClassObject(
			GUID_ElizaObject, 
			pCF, 
			CLSCTX_LOCAL_SERVER,
			REGCLS_MULTIPLEUSE,
			&dwRegister) ;
	}



  /*
  register window classes
  */

    wc.style            = CS_OWNDC;
    wc.lpfnWndProc      = (WNDPROC) MainWndProc;
    wc.cbClsExtra       = 0 ;
    wc.cbWndExtra       = 0 ;
    wc.hInstance        = hInst;
    wc.hIcon            = NULL ;//LoadIcon(hInst,MAKEINTRESOURCE(APPICON));
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = GetStockObject (WHITE_BRUSH) ;
    wc.lpszMenuName     = TEXT("MainMenu");
    wc.lpszClassName    = TEXT("PipesvClass");

    if (!RegisterClass(&wc))
        return FALSE;

	wc.style            = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = (WNDPROC) ChannelWndProc;
    wc.cbClsExtra       = 0 ;
    wc.cbWndExtra       = 4 ;
    wc.hInstance        = hInst;
    wc.hIcon            = NULL ;
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = GetStockObject (WHITE_BRUSH);
    wc.lpszMenuName     = NULL ;
    wc.lpszClassName    = TEXT("ChannelClass");

    if (!RegisterClass(&wc))
        return FALSE;


    hMainWnd = CreateWindow (TEXT("PipesvClass"), TEXT("IEliza Custom Interface Server"),
            WS_OVERLAPPED | WS_CAPTION | WS_BORDER | WS_THICKFRAME |
            WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN |
            WS_VISIBLE | WS_SYSMENU,
            CW_USEDEFAULT, CW_USEDEFAULT, 500, 350,
            NULL, NULL, hInst, NULL);

    if (hMainWnd == NULL)
        return FALSE;

	  
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


/*
App going down, shut down class factory
*/

	CoRevokeClassObject (dwRegister) ;
	CoUninitialize ( ) ; 

    return 1;
    
}

