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

  1. /************************************************************************************************************
  2. Methods:
  3. 1. Add()        add a system DSN
  4. 2. Configure()    configure an existing system DSN
  5. 3. Remove()        remove an existing system DSN
  6.  
  7. Properties:
  8. 1. GetErrorReason() reports any errors that occur during the request
  9. 2. GetIsError()        returns 1 if any error occur during the request otherwise 0
  10. ************************************************************************************************************/
  11.  
  12. /***********************************/
  13. /*include all required header files*/
  14. /***********************************/
  15. #include <stdio.h>
  16. #include <conio.h>
  17.  
  18. /************************************************************************/
  19. /*import AdvDirectory COM library here to get its methods and properties*/
  20. /************************************************************************/
  21. #import "C:\AdvDSN.dll" no_namespace
  22.  
  23. /*************************/
  24. /*prototype for functions*/
  25. /*************************/
  26. void Add();//Add function
  27. void Configure();//Configure function
  28. void Remove();//Remove function
  29.  
  30. /**************/
  31. /*main program*/
  32. /**************/
  33. void main(void)
  34. {
  35.     char Ch;
  36.     do
  37.     {
  38.         printf("\r\nAdd        [A]");
  39.         printf("\r\nConfigure    [C]");
  40.         printf("\r\nRemove        [R]");
  41.         printf("\r\nQuit        [Q]");
  42.         printf("\r\n\r\nEnter Choice = ");
  43.         scanf("%c", &Ch);
  44.         getchar();
  45.         Ch = toupper(Ch);
  46.         switch (Ch)
  47.         {
  48.             case 'A':Add();break;//call Add function
  49.             case 'C':Configure();break;//call Configure function
  50.             case 'R':Remove();break;//call Remove function
  51.             case 'Q':printf("\r\nGood Bye\r\n");break;
  52.             default: printf("\r\nError: Please enter valid choice\r\n");
  53.         }
  54.     }
  55.     while (Ch != 'Q');
  56. }
  57.  
  58. /********************************************************/
  59. /*Add function: The function is used to add a system DSN*/
  60. /********************************************************/
  61. void Add()
  62. {
  63.     char m_Driver[256], m_DSN[256];//define charecter buffer of 256 to read driver and dsn connection string
  64.     printf("\r\nEnter Driver Name = ");
  65.     gets(m_Driver);//read driver name
  66.     printf("\r\nEnter DSN Connection String = ");
  67.     gets(m_DSN);//read DSN connection string name
  68.     try//for COM library call, try...catch is used to handle exceptions
  69.     {
  70.         IDSNPtr obj;//take smart pointer object obj for COM library
  71.         CoInitialize(NULL);//Initializes the COM library on the current thread
  72.         obj = IDSNPtr("AdvDSN.DSN");
  73.         HRESULT hr = obj->Add(m_Driver, m_DSN);//call Add method of COM library
  74.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  75.         {
  76.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  77.             return;
  78.         }
  79.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  80.         //GetErrorReason() property of COM will contain error message, for which this error occured
  81.         if (obj->GetIsError() == 1)
  82.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  83.         else
  84.             printf("\r\nSuccessfully Added\r\n");//display successfull message
  85.     }
  86.     catch (_com_error &error)//catch exception here
  87.     {
  88.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  89.     }
  90. }
  91.  
  92. /***************************************************************************/
  93. /*Configure function: The function is used to configure exisintg system DSN*/
  94. /***************************************************************************/
  95. void Configure()
  96. {
  97.     char m_Driver[256], m_DSN[256];//define charecter buffer of 256 to read driver and dsn connection string
  98.     printf("\r\nEnter Driver Name = ");
  99.     gets(m_Driver);//read driver name
  100.     printf("\r\nEnter DSN Connection String = ");
  101.     gets(m_DSN);//read DSN connection string name
  102.     try//for COM library call, try...catch is used to handle exceptions
  103.     {
  104.         IDSNPtr obj;//take smart pointer object obj for COM library
  105.         CoInitialize(NULL);//Initializes the COM library on the current thread
  106.         obj = IDSNPtr("AdvDSN.DSN");
  107.         HRESULT hr = obj->Configure(m_Driver, m_DSN);//call Configure method of COM library
  108.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  109.         {
  110.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  111.             return;
  112.         }
  113.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  114.         //GetErrorReason() property of COM will contain error message, for which this error occured
  115.         if (obj->GetIsError() == 1)
  116.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  117.         else
  118.             printf("\r\nSuccessfully Configured\r\n");//display successfull message
  119.     }
  120.     catch (_com_error &error)//catch exception here
  121.     {
  122.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  123.     }
  124. }
  125.  
  126. /*********************************************************************/
  127. /*Remove function: The function is used to remove existing system DSN*/
  128. /*********************************************************************/
  129. void Remove()
  130. {
  131.     char m_Driver[256], m_DSN[256];//define charecter buffer of 256 to read driver and dsn connection string
  132.     printf("\r\nEnter Driver Name = ");
  133.     gets(m_Driver);//read driver name
  134.     printf("\r\nEnter DSN Connection String = ");
  135.     gets(m_DSN);//read DSN connection string name
  136.     try//for COM library call, try...catch is used to handle exceptions
  137.     {
  138.         IDSNPtr obj;//take smart pointer object obj for COM library
  139.         CoInitialize(NULL);//Initializes the COM library on the current thread
  140.         obj = IDSNPtr("AdvDSN.DSN");
  141.         HRESULT hr = obj->Remove(m_Driver, m_DSN);//call Remove method of COM library
  142.         if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
  143.         {
  144.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  145.             return;
  146.         }
  147.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  148.         //GetErrorReason() property of COM will contain error message, for which this error occured
  149.         if (obj->GetIsError() == 1)
  150.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  151.         else
  152.             printf("\r\nSuccessfully Removed\r\n");//display successfull message
  153.     }
  154.     catch (_com_error &error)//catch exception here
  155.     {
  156.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  157.     }
  158. }
  159.