
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include <afx.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include <shlobj.h>
#include <afxdisp.h>

#include <afx.h>
#include <afxdb.h>
#include <direct.h>
#include <iostream>
#include <vector>
#include <io.h>
#include <sys/stat.h>
#include <SHLOBJ.H>



char *location         = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Sun Management Center";
char * HKEYS           = "HKEY_LOCAL_MACHINE";



using namespace std;


HKEY getKey(char *str){
if (!str)
return NULL;
if (strncmp("HKEY_CLASSES_ROOT",str,17)==0)
return HKEY_CLASSES_ROOT;
if (strncmp("HKEY_CURRENT_USER",str,17)==0)   
return HKEY_CURRENT_USER;
if (strncmp("HKEY_LOCAL_MACHINE",str,18)==0)
return HKEY_LOCAL_MACHINE;
if (strncmp("HKEY_USERS",str,10)==0)
return HKEY_USERS;
  return NULL;
}

/* This Function add key to the registry
 * 1 Parm) Registry key
 * 2 Parm) location where do you want to store the value
 * 3 Parm) name of the key value
 * 4 Parm) value of the key
 */

bool addRegistery(char * key, char * location, char * name, char * value)
{
  HKEY kRoot,kNew;
  DWORD disposition, bufsiz,type;
  long rval;

   kRoot=getKey(key);
   if (!kRoot)
   {
    return false;
   }
         rval=RegCreateKeyEx(kRoot,location,NULL,NULL,REG_OPTION_NON_VOLATILE,
         KEY_ALL_ACCESS ,(LPSECURITY_ATTRIBUTES)NULL,&kNew,&disposition);
   
   if (name)
   {
     if (strlen(name)==0) 
	 {
      name=NULL;
	 }
   }

  bufsiz=strlen(value);
  type=REG_SZ;
  rval=RegSetValueEx(kNew,(LPTSTR)name,0,type, (LPBYTE)value,bufsiz);
  RegCloseKey(kRoot);
  return true;
}

  


bool removeRegistery(char * key, char *location, char * name)
{
   HKEY kRoot, kNew;
   long rval;
   char *str;
   str = new char[strlen(location) + strlen(name) + 5];
   kRoot = getKey(key);

   if(!kRoot)
   { 
	return false;
   }
   
      rval = RegOpenKeyEx(kRoot,location,0,KEY_ALL_ACCESS, &kNew);   

   if (rval!=ERROR_SUCCESS)
   {
     return false;
   }
  
    rval = RegDeleteValue(kNew, name);  
    RegCloseKey(kRoot);
	if (rval!=ERROR_SUCCESS)
   {
		
		return false;
	}
    RegCloseKey(kNew);
	return true;
}

char * queryKey(char * key, char *location, char *name)
{
	LPDWORD type,bufsiz;
	char *keyname;
	HKEY kRoot,kNew;
	long rval;
	keyname = new char[257];

	kRoot=getKey(key);
	if (!kRoot)
	{ 
	  return "";
	}

	rval = RegOpenKeyEx(kRoot,location,0,KEY_QUERY_VALUE,&kNew);

	if (rval!=ERROR_SUCCESS)
	{
      return "";
	}

	 bufsiz= (LPDWORD)256;
	 
	 if (name)
	 {
        if (strlen(name)==0)
		{	
          name=NULL;
		}
	 }
 
			rval=RegQueryValueEx(kNew,(LPTSTR)name,(LPDWORD)NULL,
            (LPDWORD)&type,(LPBYTE)keyname,(LPDWORD)&bufsiz);

    RegCloseKey(kRoot);
    RegCloseKey(kNew);
  
	if (rval ==ERROR_SUCCESS)
	{
      return keyname;
	} 
    else
	{
      return "";
	} 
}

char * getEnv()
{
  char *name = getenv("Temp");
  return name;
}

void main(int argc,char **argv)
{
	
	   
	   if(strcmp(argv[1], "Query") == 0)
	   {
          char *str = queryKey(HKEYS, location, argv[2]);
	      cout << str << endl;
       } 
	   else if(strcmp(argv[1], "add") == 0)
	   {
	     if(addRegistery(HKEYS, location, argv[2], argv[3]))
		 {
			 cout << "True" << endl;
		 }
		 else
		 {
			 cout << "False" << endl;
		 }

       }
	   else if(strcmp(argv[1], "delete") == 0)
	   {
     	   if(removeRegistery(HKEYS, location, argv[2]))
		   {
			   cout << "True" << endl;
		   }
		   else 
		   {
			   cout << "False" << endl;
		   }
	   }
}

