/**********************************************/
/*                                            */    
/* iReboot.cpp                                */
/*                                            */
/* reboot management                          */
/*                                            */
/* scol v 4                                   */
/*                                            */
/**********************************************/


//
// Modifications History
//
//$LB (25/04/2002) : iRemoveNeedReboot function : open the key with WRITE rights, instead of doing it with READ rights
//                   (allow the process to delete the value...)


#include <windows.h>








/******************************************************************************/
/*                                                                            */
/*                C O N T E N T                                               */
/*                                                                            */
/*                                                                            */
/*     I N T E R N A L      B O D Y                                           */
/*                                                                            */
/*                                                                            */
/*     E X T E R N A L      B O D Y                                           */
/*                                                                            */
/*                                                                            */
/* BOOL iIsRebootNeeded   ()                                                  */
/*                                                                            */
/* BOOL iRemoveNeedReboot ()                                                  */
/*                                                                            */
/* BOOL iNeedReboot       ()                                                  */
/*                                                                            */
/* BOOL iReboot           ()                                                  */
/*                                                                            */
/******************************************************************************/










/******************************************************************************/
/*                                                                            */
/*     T Y P E S    A N D    D A T A                                          */
/*                                                                            */
/******************************************************************************/

#define CRYONETWORKS_KEYPATH "Software\\Cryo-Networks\\"
#define SCOLREBOOT_KEYVALUE  "ScolReboot"











/******************************************************************************/
/*                                                                            */
/*     I N T E R N A L      B O D Y                                           */
/*                                                                            */
/******************************************************************************/













/******************************************************************************/
/*                                                                            */
/*     E X T E R N A L      B O D Y                                           */
/*                                                                            */
/******************************************************************************/









/**********************************************************/
/*                                                        */
/* BOOL iIsRebootNeeded()                                 */
/*                                                        */
/* scol v 4                                               */
/*                                                        */
/* returns 0 if the computer doesn't need to reboot.      */
/* otherwize, it returns nonzero.                         */
/**********************************************************/

BOOL iIsRebootNeeded()
{
HKEY cryoKey;
byte data = 0;
DWORD type = REG_BINARY;
DWORD size = sizeof(byte);;




	//open the key
	if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, CRYONETWORKS_KEYPATH, 0, KEY_READ, &cryoKey) != ERROR_SUCCESS) 
  		return 0;

	//and read the value
	if (RegQueryValueEx (cryoKey, SCOLREBOOT_KEYVALUE, NULL, &type, &data, &size) != ERROR_SUCCESS)
	{
		RegCloseKey (cryoKey);
		return 0;
	}


	return 1;
}







/**********************************************************/
/*                                                        */
/* BOOL iRemoveNeedReboot()                               */
/*                                                        */
/* scol v 4                                               */
/*                                                        */
/* remove "ScolReboot" value from the CryoNEtworks        */
/* registry key.                                          */
/*                                                        */
/**********************************************************/

BOOL iRemoveNeedReboot()
{

if (iIsRebootNeeded())
{
HKEY cryoKey;
byte data = 1;
LONG res;


	//open the key
	if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, CRYONETWORKS_KEYPATH, 0, KEY_WRITE, &cryoKey) != ERROR_SUCCESS) 
  		return 0;


	//remove value that indicates Scol need to reboot
	if ((res = RegDeleteValue (cryoKey, SCOLREBOOT_KEYVALUE)) != ERROR_SUCCESS)
	{
		RegCloseKey (cryoKey);
		return 0;
	}


	RegCloseKey (cryoKey);
}
return 1;
}






/**********************************************************/
/*                                                        */
/* BOOL iNeedReboot ()                                    */
/*                                                        */
/* scol v 4                                               */
/*                                                        */
/* write a value in registry Cryo-Networks registry key   */
/* in order to indicate that we'll need to reboot the     */
/* computer.                                              */
/*                                                        */
/**********************************************************/
/*
BOOL iperms_IsUserAuthorized()
{
HKEY tmpKey;
byte data = 0;

	//open or create the Cryo-Networks software local key
	if ((RegCreateKeyEx (HKEY_LOCAL_MACHINE, CRYONETWORKS_KEYPATH, 0,
			NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &tmpKey, NULL)) != ERROR_SUCCESS)
		return 0;


	//write value 
	if ((RegSetValueEx (tmpKey, "tmp", 0, (DWORD)REG_BINARY, &data, sizeof(byte))) != ERROR_SUCCESS)
	{
		RegCloseKey (tmpKey);
		return 0;
	}

	//remove value 
	if ((RegDeleteValue (tmpKey, "tmp")) != ERROR_SUCCESS)
	{
		RegCloseKey (tmpKey);
		return 0;
	}


	RegCloseKey (tmpKey);
	return 1;
}
*/
BOOL iNeedReboot ()
{
if (!iIsRebootNeeded())
{
HKEY cryoKey;
byte data = 0;



	//open or create the Cryo-Networks software local key
	if ((RegCreateKeyEx (HKEY_LOCAL_MACHINE, CRYONETWORKS_KEYPATH, 0,
			NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &cryoKey, NULL)) != ERROR_SUCCESS)
		return 0;


	//write value that indicates Scol need to reboot
	if ((RegSetValueEx (cryoKey, SCOLREBOOT_KEYVALUE, 0, (DWORD)REG_BINARY, &data, sizeof(byte))) != ERROR_SUCCESS)
	{
		RegCloseKey (cryoKey);
		return 0;
	}


	RegCloseKey (cryoKey);

}
return 1;
}







/**********************************************************/
/*                                                        */
/* BOOL iReboot ()                                        */
/*                                                        */
/* scol v 4                                               */
/*                                                        */
/* make the computer reboot                               */
/**********************************************************/

BOOL iReboot ()
{
	ExitWindowsEx(EWX_REBOOT,0);
	return 1;
}
