home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / sqldmo / cpp / dmoping / dmoping.cpp next >
Encoding:
C/C++ Source or Header  |  1998-09-16  |  10.1 KB  |  432 lines

  1. // ************************************************************************ //
  2. // dmoping.cpp
  3. // 
  4. // purpuse of this tool is to test the ability to instantiate a SQLDMO object
  5. // and connect to the server indicated. Check which version of the server is 
  6. // running and use the appropriate object model based on the SQL Serverversion.
  7. //
  8. // For SQL Server version 7.0 the SQLDMO is used and for SQL Server version 6.x
  9. // the SQLOLE object model is used.
  10. //
  11. // usage dmoping [-S] [-U -P | -E] [-6 | -7] [-V] [-v] [-?]
  12. //        -S server name, defaults to local server
  13. //        -U user name, defaults to sa
  14. //        -P password, defaults to "" (blank)
  15. //        -E integrated security, defaults to FALSE
  16. //        -6 force use of SQLOLE, defaults automatic based on version ping
  17. //        -7 force use of SQLDMO, defaults automatic based on version ping
  18. //        -V verbose mode, defaults to FALSE
  19. //        -? help and usage information
  20. // ************************************************************************ //
  21.  
  22. #include "dmoping.h"
  23.  
  24. // ************************************************************************ //
  25. // CDmoPing constructor - set all defaults
  26. // ************************************************************************ //
  27. CDmoPing::CDmoPing()
  28. :    m_pServer(NULL), 
  29.     m_pUser(NULL),
  30.     m_pPassword(NULL),
  31.     m_bIntegratedSec(FALSE),
  32.     m_bVerbose(FALSE),
  33.     m_eSQLVersion(SQLDMOSQLVer_Unknown)
  34. {
  35.  
  36. }
  37.  
  38.  
  39. // ************************************************************************ //
  40. // CDmoPing destructor
  41. // ************************************************************************ //
  42. CDmoPing::~CDmoPing()
  43. {
  44.     if (m_pServer)
  45.     {
  46.         delete [] m_pServer;
  47.         m_pServer = NULL;
  48.     }
  49.  
  50.     if (m_pUser)
  51.     {
  52.         delete [] m_pUser;
  53.         m_pUser = NULL;
  54.     }
  55.  
  56.     if (m_pPassword)
  57.     {
  58.         delete [] m_pPassword;
  59.         m_pPassword = NULL;
  60.     }
  61. }
  62.  
  63. // ************************************************************************ //
  64. // PargeArgs - poor mans command argument parser
  65. // ************************************************************************ //
  66. BOOL CDmoPing::Parse(INT argc, TCHAR* argv[])
  67. {
  68.     int    i = 1;
  69.  
  70.     while (i < argc)
  71.     {
  72.         if (! IsArgDelimiter(argv[i]) )
  73.         {
  74.             goto Error;
  75.         }
  76.  
  77.         switch (argv[i][1])
  78.         {
  79.         // server
  80.         case 'S':
  81.             GetArg(i, m_pServer, argc, argv);
  82.             break;
  83.  
  84.         // login ID
  85.         case 'U':
  86.             GetArg(i, m_pUser, argc, argv);
  87.             break;
  88.  
  89.         // password.
  90.         case 'P':
  91.             GetArg(i, m_pPassword, argc, argv);
  92.             break;
  93.         
  94.         // integrated security
  95.         case 'E':
  96.             m_bIntegratedSec = TRUE;
  97.             break;
  98.  
  99.         // force 6.x SQLOLE usage
  100.         case '6':
  101.             m_eSQLVersion = SQLDMOSQLVer_65;
  102.             break;
  103.         
  104.         // force 7.0 SQLDMO
  105.         case '7':
  106.             m_eSQLVersion = SQLDMOSQLVer_70;
  107.             break;
  108.  
  109.         // version
  110.         case 'v':
  111.             break;
  112.  
  113.         // verbose mode
  114.         case 'V':
  115.             m_bVerbose = TRUE;
  116.             break;
  117.  
  118.         case '?':
  119.             goto Usage;
  120.  
  121.         default:
  122.             goto Error;
  123.         
  124.         }
  125.         i++;
  126.     }
  127.  
  128.     return(TRUE);
  129.  
  130. Error:
  131.     _tprintf(TEXT("error dmoping argument (%d) %s\n\n"), i, argv[i] );
  132.  
  133. Usage:
  134.     _tprintf(TEXT("usage dmoping [-S] [-U -P | -E] [-6 | -7] [-V] [-v] [-?] \n"));
  135.     _tprintf(TEXT("  -S server name, defaults to local server \n"));
  136.     _tprintf(TEXT("  -U user name, defaults to sa \n"));
  137.     _tprintf(TEXT("  -P password, defaults to \"\" (blank) \n"));
  138.     _tprintf(TEXT("  -E integrated security, defaults to FALSE\n"));
  139.     _tprintf(TEXT("  -6 force use of SQLOLE, defaults automatic based on version ping\n"));
  140.     _tprintf(TEXT("  -7 force use of SQLDMO, defaults automatic based on version ping\n"));
  141.     _tprintf(TEXT("  -V verbose mode, defaults to FALSE\n"));
  142.     _tprintf(TEXT("  -? help and usage information\n"));
  143.  
  144.     return(FALSE);
  145. }
  146.  
  147. // ************************************************************************ //
  148. // GetVersion - uses SQLDMO to determine the SQL Server version 
  149. //                using pSQLServer->PingSQLServerVersion
  150. // ************************************************************************ //
  151. BOOL CDmoPing::GetVersion()
  152. {
  153.     BOOL fRet = FALSE;
  154.     LPSQLDMOSERVER    pSQLServer = NULL; 
  155.  
  156.  
  157.     if FAILED(CoCreateInstance(CLSID_SQLDMOServer, NULL, CLSCTX_INPROC_SERVER,
  158.         IID_ISQLDMOServer, (LPVOID*)&pSQLServer))
  159.     {
  160.         return FALSE;
  161.     }
  162.  
  163.     if SUCCEEDED(pSQLServer->PingSQLServerVersion(&m_eSQLVersion, m_pServer, m_pUser, m_pPassword))
  164.     {
  165.         fRet = TRUE;
  166.     }
  167.  
  168.     if (pSQLServer)
  169.     {
  170.         pSQLServer->Release();
  171.         pSQLServer = NULL;
  172.     }
  173.  
  174.     return fRet;
  175. }
  176.  
  177. // ************************************************************************ //
  178. // Ping - instantiate an SQLOLE or SQLDMO object based on the m_eSQLVersion
  179. //          connect to the server and retrieve the version information of
  180. //        SQLOLE/SQLDMO and the underlaying database layer DB-Library/ODBC
  181. // ************************************************************************ //
  182. BOOL CDmoPing::Ping()
  183. {
  184.     if (SQLDMOSQLVer_70 == m_eSQLVersion)
  185.     {
  186.         LPSQLDMOSERVER pSQLServer;
  187.  
  188.         if FAILED(CoCreateInstance(CLSID_SQLDMOServer, NULL, CLSCTX_INPROC_SERVER,
  189.             IID_ISQLDMOServer, (LPVOID*)&pSQLServer))
  190.         {
  191.             DisplayError();
  192.             return(FALSE);
  193.         }
  194.  
  195.         if (m_bIntegratedSec)
  196.         {
  197.             pSQLServer->SetLoginSecure(TRUE);
  198.         }
  199.  
  200.         if FAILED(pSQLServer->Connect(m_pServer, m_pUser, m_pPassword) )
  201.         {
  202.             DisplayError();
  203.             delete pSQLServer;
  204.             return(FALSE);
  205.         }
  206.  
  207.         BSTR strVersion;
  208.         pSQLServer->GetVersionString(&strVersion);
  209.         _tprintf(_T("%s\n"), strVersion);
  210.         SysFreeString(strVersion);
  211.  
  212.         BSTR strODBCVersion;
  213.         LONG lSQLDMOVersion;
  214.         LONG lSQLDMOVersionMajor;
  215.         LONG lSQLDMOVersionMinor;
  216.  
  217.         LPSQLDMOAPPLICATION pApplication;
  218.         
  219.         pSQLServer->GetApplication(&pApplication);
  220.         pApplication->GetVersionBuild(&lSQLDMOVersion);
  221.         pApplication->GetVersionMajor(&lSQLDMOVersionMajor);
  222.         pApplication->GetVersionMinor(&lSQLDMOVersionMinor);
  223.         pApplication->GetODBCVersionString(&strODBCVersion);
  224.  
  225.         _tprintf(_T("SQL-DMO version %d.%d.%d\n"), 
  226.             lSQLDMOVersion, lSQLDMOVersionMajor, lSQLDMOVersionMinor);
  227.         _tprintf(_T("ODBC version %s\n"), strODBCVersion);
  228.         
  229.         SysFreeString(strODBCVersion);
  230.  
  231.         pSQLServer->DisConnect();
  232.         
  233.         delete pSQLServer;
  234.  
  235.     }
  236.     else if (SQLDMOSQLVer_65 == m_eSQLVersion || SQLDMOSQLVer_60 == m_eSQLVersion)
  237.     {
  238.         LPSQLOLESERVER pSQLServer;
  239.  
  240.         if FAILED(CoCreateInstance(CLSID_SQLOLEServer, NULL, CLSCTX_INPROC_SERVER,
  241.             IID_ISQLOLEServer, (LPVOID*)&pSQLServer))
  242.         {
  243.             DisplayError();
  244.             return(FALSE);
  245.         }
  246.  
  247.         if (m_bIntegratedSec)
  248.         {
  249.             pSQLServer->SetLoginSecure(TRUE);
  250.         }
  251.  
  252.         if FAILED(pSQLServer->Connect(m_pServer, m_pUser, m_pPassword) )
  253.         {
  254.             DisplayError();
  255.             delete pSQLServer;
  256.             return(FALSE);
  257.         }
  258.  
  259.         BSTR strVersion;
  260.         pSQLServer->GetVersionString(&strVersion);
  261.         _tprintf(_T("%s\n"), strVersion);
  262.         SysFreeString(strVersion);
  263.  
  264.         BSTR strDBLibraryVersion;
  265.         LONG lSQLOLEVersion;
  266.         LONG lSQLOLEVersionMajor;
  267.         LONG lSQLOLEVersionMinor;
  268.  
  269.         LPSQLOLEAPPLICATION pApplication;
  270.         
  271.         pSQLServer->GetApplication(&pApplication);
  272.         pApplication->GetVersionBuild(&lSQLOLEVersion);
  273.         pApplication->GetVersionMajor(&lSQLOLEVersionMajor);
  274.         pApplication->GetVersionMinor(&lSQLOLEVersionMinor);
  275.         pApplication->GetDBLibraryVersionString(&strDBLibraryVersion);
  276.  
  277.         _tprintf(_T("SQL-OLE version %d.%d.%d\n"), 
  278.             lSQLOLEVersion, lSQLOLEVersionMajor, lSQLOLEVersionMinor);
  279.         _tprintf(_T("DB-Library version %s\n"), strDBLibraryVersion);
  280.         
  281.         SysFreeString(strDBLibraryVersion);
  282.  
  283.         pSQLServer->DisConnect();
  284.         
  285.         delete pSQLServer;
  286.     }
  287.     else
  288.     {
  289.         // no SQLDMO or SQLOLE version available for this version
  290.         //
  291.         return(FALSE);
  292.     }
  293.  
  294.     return(TRUE);
  295.  
  296. }
  297.  
  298. // ************************************************************************ //
  299. // main
  300. // ************************************************************************ //
  301. INT    _tmain(INT argc, TCHAR** argv, TCHAR** envp)
  302. {
  303.     int    iExitStatus = 1;    // assume failure.
  304.  
  305.     try
  306.     {
  307.         // Initialize COM
  308.         //
  309.         if FAILED(CoInitialize (NULL))
  310.         {
  311.             return (iExitStatus);
  312.         }
  313.  
  314.         CDmoPing* pPing = new CDmoPing();
  315.  
  316.         if (FALSE == pPing->Parse(argc, argv) )
  317.         {
  318.             return (iExitStatus);
  319.         }
  320.  
  321.         if (SQLDMOSQLVer_Unknown == pPing->GetSQLVersion() )
  322.         {
  323.             pPing->GetVersion();
  324.         }
  325.  
  326.         pPing->Ping();
  327.  
  328.         delete pPing;
  329.             
  330.         CoUninitialize();
  331.         
  332.         iExitStatus = 0;    // Success
  333.     }
  334.     catch(...)
  335.     {
  336.         _tprintf(TEXT("error dmoping unhandled exception (%d)\n"), GetLastError() );
  337.     }
  338.  
  339.     return (iExitStatus);
  340. }
  341.  
  342. // **********************************************************************
  343. // display error information
  344. // **********************************************************************
  345. VOID DisplayError()
  346. {
  347.     LPERRORINFO    pErrorInfo = NULL;
  348.     BSTR        strDescription, strSource;
  349.  
  350.     GetErrorInfo(0,&pErrorInfo);
  351.  
  352.     pErrorInfo->GetDescription(&strDescription);
  353.     pErrorInfo->GetSource(&strSource);
  354.  
  355.     _tprintf(TEXT("Error: %s\n"),strSource);
  356.     _tprintf(TEXT("%s\n"),strDescription);
  357.  
  358.     pErrorInfo->Release();
  359.     
  360.     SysFreeString(strDescription);
  361.     SysFreeString(strSource);
  362.     
  363.     return;
  364. }
  365.  
  366. // **********************************************************************
  367. // IsArgDelimiter - determine if the first character is a delimiter
  368. // **********************************************************************
  369. BOOL IsArgDelimiter(LPTSTR lpArg)
  370. {
  371.     if (lpArg[0] == '/' || lpArg[0] == '-')
  372.     {
  373.         return(TRUE);
  374.     }
  375.     else
  376.     {
  377.         return(FALSE);
  378.     }
  379. }
  380.  
  381.  
  382. // **********************************************************************
  383. // GetArg - return command line argument in string buffer
  384. // **********************************************************************
  385. BOOL GetArg(INT& i, LPTSTR& lpValue, INT argc, TCHAR* argv[])
  386. {
  387.     LPTSTR lpStr = lpValue;
  388.  
  389.     if (argv[i][2] == '\0')
  390.     {
  391.         // still in range
  392.         if ((i + 1) < argc) 
  393.         {
  394.             // next starts with Arg Delimiter, so this value is NULL
  395.             if (IsArgDelimiter(argv[i + 1]) )
  396.             {
  397.                 lpStr = NULL;
  398.             }
  399.             // use the next Arg as the value
  400.             else
  401.             {
  402.                 size_t nSize = _tcslen(argv[i + 1]);
  403.                 if (lpStr)
  404.                 {
  405.                     delete [] lpStr;
  406.                 }
  407.                 lpStr = new TCHAR[nSize + sizeof(TCHAR)];
  408.                 _tcscpy(lpStr, argv[i + 1]);
  409.                 i++;
  410.             }
  411.         }
  412.         else
  413.         {
  414.             lpStr = NULL;
  415.         }
  416.     }
  417.     else
  418.     {
  419.         size_t nSize = _tcslen(argv[i]) - 2;
  420.         if (lpStr)
  421.         {
  422.             delete [] lpStr;
  423.         }
  424.         lpStr = new TCHAR[nSize + sizeof(TCHAR)];
  425.         _tcscpy(lpStr, argv[i] + 2);
  426.     }
  427.  
  428.     lpValue = lpStr;
  429.  
  430.     return(TRUE);
  431. }
  432.