home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / ASPFusion_Basic_Suite / examples / c++ / advregistry / AdvRegistry.cpp next >
Encoding:
C/C++ Source or Header  |  2002-01-26  |  9.7 KB  |  241 lines

  1. /************************************************************************************************************
  2. Methods:
  3. 1. Delete()        delete the specified branch from registry
  4. 2. Get()        get the specified value from specified branch
  5. 3. GetAll()        get list of specified branch
  6. 4. Set()        set the value to specified branch
  7.  
  8. Properties:
  9. 1. PutEntry()        the registry value to be processed
  10. 2. GetErrorReason() reports any errors that occur during the request
  11. 3. GetIsError()        returns 1 if any error occur during the request otherwise 0
  12. 4. PutSort()        sorts query column data (case-insensitive). sorts on entry, type, and value columns as text.
  13.                     valid values are
  14.                     Entry_ASC (default)
  15.                     Entry_DESC 
  16.                     Type_ASC 
  17.                     Type_DESC 
  18.                     Value_ASC 
  19.                     Value_DESC
  20. 5. PutType()        the type of data you want to process. valid values are 
  21.                     Any 
  22.                     String 
  23.                     DWORD 
  24.                     Key
  25. 6. PutValue()        the data value to be set
  26. ************************************************************************************************************/
  27.  
  28. /***********************************/
  29. /*include all required header files*/
  30. /***********************************/
  31. #include <stdio.h>
  32. #include <conio.h>
  33.  
  34. /***********************************************************************/
  35. /*import AdvRegistry COM library here to get its methods and properties*/
  36. /***********************************************************************/
  37. #import "C:\AdvRegistry.dll" no_namespace
  38.  
  39. /*************************/
  40. /*prototype for functions*/
  41. /*************************/
  42. void Delete();//Delete function
  43. void Get();//Get function
  44. void GetAll();//GetAll function
  45. void Set();//Set function
  46.  
  47. /**************/
  48. /*main program*/
  49. /**************/
  50. void main(void)
  51. {
  52.     char Ch;
  53.     do
  54.     {
  55.         printf("\r\nDelete    [D]");
  56.         printf("\r\nGet    [G]");
  57.         printf("\r\nGetAll    [A]");
  58.         printf("\r\nSet    [S]");
  59.         printf("\r\nQuit    [Q]");
  60.         printf("\r\n\r\nEnter Choice = ");
  61.         scanf("%c", &Ch);
  62.         getchar();
  63.         Ch = toupper(Ch);
  64.         switch (Ch)
  65.         {
  66.             case 'D':Delete();break;//call Delete function
  67.             case 'G':Get();break;//call Get function
  68.             case 'A':GetAll();break;//call GetAll function
  69.             case 'S':Set();break;//call Set function
  70.             case 'Q':printf("\r\nGood Bye\r\n");break;
  71.             default: printf("\r\nError: Please enter valid choice\r\n");
  72.         }
  73.     }
  74.     while (Ch != 'Q');
  75. }
  76.  
  77. /******************************************************************/
  78. /*Delete function: The function is used to delete registry entries*/
  79. /******************************************************************/
  80. void Delete()
  81. {
  82.     char m_Branch[256], m_Entry[256];//define charecter buffer of 256 to read branch name and entry name
  83.     printf("\r\nEnter Branch Name = ");
  84.     gets(m_Branch);//read branch name
  85.     printf("\r\nEnter Entry Name = ");
  86.     gets(m_Entry);//read entry name
  87.     try//for COM library call, try...catch is used to handle exceptions
  88.     {
  89.         IRegistryPtr obj;//take smart pointer object obj for COM library
  90.         CoInitialize(NULL);//Initializes the COM library on the current thread
  91.         obj = IRegistryPtr("AdvRegistry.Registry");
  92.         obj->PutEntry(m_Entry);//set entry property
  93.         HRESULT hr = obj->Delete(m_Branch);//call Delete method of COM library
  94.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  95.         {
  96.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  97.             return;
  98.         }
  99.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  100.         //GetErrorReason() property of COM will contain error message, for which this error occured
  101.         if (obj->GetIsError() == 1)
  102.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  103.         else
  104.             printf("\r\nSuccessfully Deleted\r\n");//display successfull message
  105.     }
  106.     catch (_com_error &error)//catch exception here
  107.     {
  108.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  109.     }
  110. }
  111.  
  112. /*****************************************************************/
  113. /*Get function: The function is used to get registry entry values*/
  114. /*****************************************************************/
  115. void Get()
  116. {
  117.     char m_Branch[256], m_Entry[256], m_Type[256];//define charecter buffer of 256 to read branch name, entry name and type
  118.     printf("\r\nEnter Branch Name = ");
  119.     gets(m_Branch);//read branch name
  120.     printf("\r\nEnter Entry Name = ");
  121.     gets(m_Entry);//read entry name
  122.     printf("\r\nEnter Type = ");
  123.     gets(m_Type);//read type name
  124.     try//for COM library call, try...catch is used to handle exceptions
  125.     {
  126.         IRegistryPtr obj;//take smart pointer object obj for COM library
  127.         CoInitialize(NULL);//Initializes the COM library on the current thread
  128.         obj = IRegistryPtr("AdvRegistry.Registry");
  129.         obj->PutEntry(m_Entry);//set entry property
  130.         obj->PutType(m_Type);//set type property
  131.         IDispatch* p = obj->Get(m_Branch);//call Get method of COM library
  132.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  133.         //GetErrorReason() property of COM will contain error message, for which this error occured
  134.         if (obj->GetIsError() == 1)
  135.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  136.         else
  137.         {
  138.             printf("\r\nSuccessfully Get\r\n");//display successfull message
  139.             IGetPtr getobj;
  140.             p->QueryInterface(IID_IDispatch,(void**)&getobj);//call query interface
  141.             printf("\r\nValue = %s\r\n", (char*)getobj->GetValue());
  142.         }
  143.     }
  144.     catch (_com_error &error)//catch exception here
  145.     {
  146.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  147.     }
  148. }
  149.  
  150. /*********************************************************************/
  151. /*GetAll function: The function is used to get registry entry listing*/
  152. /*********************************************************************/
  153. void GetAll()
  154. {
  155.     char m_Branch[256], m_Sort[256], m_Type[256];//define charecter buffer of 256 to read branch name, sort criteria and type
  156.     printf("\r\nEnter Branch Name = ");
  157.     gets(m_Branch);//read branch name
  158.     printf("\r\nEnter Type = ");
  159.     gets(m_Type);//read type name
  160.     printf("\r\nEnter Sort Criteria = ");
  161.     gets(m_Sort);//read sort citeria name
  162.     try//for COM library call, try...catch is used to handle exceptions
  163.     {
  164.         IRegistryPtr obj;//take smart pointer object obj for COM library
  165.         CoInitialize(NULL);//Initializes the COM library on the current thread
  166.         obj = IRegistryPtr("AdvRegistry.Registry");
  167.         obj->PutType(m_Type);//set type property
  168.         obj->PutSort(m_Sort);//set sort criteria property
  169.         _variant_t pVal = obj->GetAll(m_Branch);//call GetAll method of COM library
  170.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  171.         //GetErrorReason() property of COM will contain error message, for which this error occured
  172.         if (obj->GetIsError() == 1)
  173.         {
  174.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  175.         }
  176.         else
  177.         {
  178.             printf("\r\nSuccessfully GetAll\r\n");//display successfull message
  179.             IDispatch* p = pVal.pdispVal;//IDispatch object is assigned VARIANT object
  180.             IGetAllPtr getallobj;//take GetAll interface object
  181.             p->QueryInterface(IID_IDispatch,(void**)&getallobj);//call query interface
  182.             long count = getallobj->GetCount();//get number of nodes
  183.             for (int j = 1; j <= count; j++)//loop over each node
  184.             {
  185.                 _variant_t retVal = getallobj->GetItem(j);//get node item
  186.                 IDispatch* pp = retVal.pdispVal;//IDispatch object is assigned VARIANT object
  187.                 IGetAllEntriesPtr getallentriesobj;//take GetAllEntries interface object
  188.                 pp->QueryInterface(IID_IDispatch,(void**)&getallentriesobj);//call query interface
  189.                 printf("\r\n%s | %s | %s\r\n", (char*)getallentriesobj->GetEntry(),
  190.                     (char*)getallentriesobj->GetType(),(char*)getallentriesobj->GetValue());//format data
  191.             }
  192.             printf("\r\n");
  193.         }
  194.     }
  195.     catch (_com_error &error)//catch exception here
  196.     {
  197.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  198.     }
  199. }
  200.  
  201. /****************************************************************/
  202. /*Set function: The function is used to set registry entry value*/
  203. /****************************************************************/
  204. void Set()
  205. {
  206.     char m_Branch[256], m_Entry[256], m_Type[256], m_Value[256];//define charecter buffer of 256 to read branch name, entry name, type and value
  207.     printf("\r\nEnter Branch Name = ");
  208.     gets(m_Branch);//read branch name
  209.     printf("\r\nEnter Entry Name = ");
  210.     gets(m_Entry);//read entry name
  211.     printf("\r\nEnter Type = ");
  212.     gets(m_Type);//read type name
  213.     printf("\r\nEnter Value = ");
  214.     gets(m_Value);//read sort citeria name
  215.     try//for COM library call, try...catch is used to handle exceptions
  216.     {
  217.         IRegistryPtr obj;//take smart pointer object obj for COM library
  218.         CoInitialize(NULL);//Initializes the COM library on the current thread
  219.         obj = IRegistryPtr("AdvRegistry.Registry");
  220.         obj->PutEntry(m_Entry);//set entry property
  221.         obj->PutType(m_Type);//set type property
  222.         obj->PutValue(m_Value);//set value property
  223.         HRESULT hr = obj->Set(m_Branch);//call Set method of COM library
  224.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  225.         {
  226.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  227.             return;
  228.         }
  229.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  230.         //GetErrorReason() property of COM will contain error message, for which this error occured
  231.         if (obj->GetIsError() == 1)
  232.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  233.         else
  234.             printf("\r\nSuccessfully Set\r\n");//display successfull message
  235.     }
  236.     catch (_com_error &error)//catch exception here
  237.     {
  238.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  239.     }
  240. }
  241.