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

	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"


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


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

CClassFactory cf ;

static char *pGuidString = "{C7744F30-0B49-11d0-8BF8-00608C876FB3}" ;
static const GUID GUID_ScriptEngine = { 0xc7744f30, 0xb49, 0x11d0, { 0x8b, 0xf8, 0x0, 0x60, 0x8c, 0x87, 0x6f, 0xb3 } };

int ObjectCount ;

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


HINSTANCE hDllInst ;

__declspec (dllexport) BOOL WINAPI DllMain (HINSTANCE hInst, DWORD why, LPVOID reserved)
{

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

	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_ScriptEngine)
	{
		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) 
	{
		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, NULL, REG_SZ, "ThunderScript", 13) ;
	retval = RegSetValue (hMyKey, "InProcServer32", REG_SZ, FileName, length) ;
	retval = RegSetValue (hMyKey, "OLEScript", REG_SZ, "", 0) ;

	RegCreateKey (HKEY_CLASSES_ROOT, "ThunderScript", &hNameKey) ;
	RegSetValue (hNameKey, "CLSID", REG_SZ, pGuidString, lstrlen(pGuidString)) ;
	RegSetValue (hNameKey, "OLEScript", REG_SZ, "", 0) ;

	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 ;
	}
}
