/*
**************************************************************************

	Filename: DLLMAIN.C


	Rolling Thunder Computing, Inc.
	25 Hearbtreak Rd
	Ipswich MA 01938

  e-mail 73540.3430@compuserve.com
*****************************************************************************
*/


/*
*****************************************************************************
System header files
*****************************************************************************
*/

#include	<windows.h>

/*
*****************************************************************************
Application header files
*****************************************************************************
*/


#include "cclassf.h"
#include "ctimedta.h"


/*
*****************************************************************************
Function prototype files
*****************************************************************************
*/


/*
*****************************************************************************
Global Variables defined here
*****************************************************************************
*/

UINT cfTimeData ;
CClassFactory cf ;

static char *pGuidString = "{351D1CE0-DCAA-11cf-B85F-00608C876FB3}" ;
static const GUID GUID_TimeData = 
{ 0x351d1ce0, 0xdcaa, 0x11cf, { 0xb8, 0x5f, 0x0, 0x60, 0x8c, 0x87, 0x6f, 0xb3 } };


/*
*****************************************************************************
EXTERNS of global variables defined in other files
*****************************************************************************
*/


int ObjectCount = 0, LockCount = 0 ;
SYSTEMTIME lt ;
CTimeData *lpFirstObj = NULL ;
HINSTANCE hDllInst ;

void CALLBACK TimerProc (HWND hWnd, UINT umsg, UINT id, DWORD time)
{
	GetLocalTime (&lt) ;

	if (lpFirstObj)
	{
		lpFirstObj->SendDataChangeToClient( )  ;
	}

}


__declspec (dllexport) BOOL WINAPI DllMain (HINSTANCE hInst, DWORD why, LPVOID reserved)
{
	static UINT TimerID ;

	if (why == DLL_PROCESS_ATTACH)
	{				
		hDllInst = hInst ;

/*
Set timer for regular notifications
*/
		TimerID = SetTimer (NULL, 0, 1000, (TIMERPROC) TimerProc) ;


/*
Register private clipboard format
*/

		cfTimeData = RegisterClipboardFormat ("TimeDataObject") ;
	}
	else if (why == DLL_PROCESS_DETACH)
	{
		KillTimer (NULL, TimerID) ;
	}


	return TRUE ;
}



STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID FAR* ppv)
{

/*
Check to make sure that COM is asking for the CLSID that we support.
Maybe someone got the registry entry wrong.
*/

	if (rclsid !=  GUID_TimeData)
	{
		return E_FAIL ;
	}

/*
Take our hard-wired class factory and call QueryInterface on it, on
behalf of the COM that called this function.
*/

	return cf.QueryInterface (riid, ppv) ;

}					 

STDAPI DllCanUnloadNow(void)
{
	if (ObjectCount == 0 && LockCount == 0) 
	{
		return S_OK ;
	}
	else
	{
		return S_FALSE ;
	}

	return 0 ;
}

STDAPI DllRegisterServer( )
{
	HKEY hClsidKey, hMyKey, hNameKey ;
	char FileName [256] ;
	int length ; LONG retval ;

	length = GetModuleFileName (hDllInst, FileName, sizeof(FileName)) ;

	RegOpenKey (HKEY_CLASSES_ROOT, "CLSID", &hClsidKey) ;
	RegCreateKey (hClsidKey, pGuidString, &hMyKey) ;
	retval = RegSetValue (hMyKey, "InProcServer32", REG_SZ, FileName, length) ;

	RegCreateKey (HKEY_CLASSES_ROOT, "EssenceOfOLE.Data5", &hNameKey) ;
	RegSetValue (hNameKey, "Clsid", REG_SZ, pGuidString, lstrlen(pGuidString)) ;

	RegCloseKey (hMyKey) ;
	RegCloseKey (hClsidKey) ;
	RegCloseKey (hNameKey) ;

	if (retval == ERROR_SUCCESS)
	{
		return S_OK ;
	}
	else
	{
		return E_UNEXPECTED ;
	}
}

STDAPI DllUnregisterServer( )
{
	HKEY hClsidKey, hMyKey, hNameKey ;
	LONG retval ;

	RegOpenKey (HKEY_CLASSES_ROOT, "CLSID", &hClsidKey) ;
	RegOpenKey (hClsidKey, pGuidString, &hMyKey) ;

	retval = RegDeleteKey (hMyKey, "InProcServer32") ;
	RegDeleteKey (hClsidKey, pGuidString) ;

	RegOpenKey (HKEY_CLASSES_ROOT, "EssenceOfOLE.Data5", &hNameKey) ;
	RegDeleteKey (hNameKey, "Clsid") ;
	retval = RegDeleteKey (HKEY_CLASSES_ROOT, "EssenceOfOLE.Data5") ;

	RegCloseKey (hMyKey) ;
	RegCloseKey (hClsidKey) ;
	RegCloseKey (hNameKey) ;
	
	
	if (retval == ERROR_SUCCESS)
	{
		return S_OK ;
	}
	else
	{
		return E_UNEXPECTED ;
	}
}
