home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / SQLNS / cpp / dumptree / dumptree.cpp next >
Encoding:
C/C++ Source or Header  |  1998-07-13  |  6.9 KB  |  255 lines

  1. // **********************************************************************
  2. // dumptree -    dump all objects in the SQL Namespace tree
  3. //                
  4. //                using SQLNS, targetting SQL Server 7.0
  5. // **********************************************************************
  6.  
  7. #define STRICT                    // strict type checking
  8. #define WIN32_LEAN_AND_MEAN        // do not include the world
  9. #define INC_OLE2                // include OLE/COM files
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <initguid.h>
  14. #include <tchar.h>
  15. #include <olectl.h>
  16.  
  17. // #import "d:\\mssql7\\binn\\sqlns.enu" no_namespace
  18.  
  19. #include <sqlnsx.h>                        // include file for SQL Namespace objects
  20.                                         // installed in \mssql7\devtools\include
  21.  
  22. // **********************************************************************
  23. // function declarations
  24. // **********************************************************************
  25. void ReportError();
  26. BOOL IsCommandSupportedByObject(ISQLNamespace* pNS, ISQLNamespaceObject* pObj, LPCOLESTR szCmd);
  27. BOOL IsCommandSupportedByObject(ISQLNamespace* pNS, ISQLNamespaceObject* pObj, long lCommandID);
  28. void TreeDump(ISQLNamespace* pNS, HSQLNSITEM hItem);
  29.  
  30. // **********************************************************************
  31. // macros and #defines
  32. // **********************************************************************
  33.  
  34. // SAFE_RELEASE of Com ptrs
  35. #define SAFE_RELEASE(pv)        if(pv) { (pv)->Release(); pv = NULL; }  
  36.  
  37. // SAFE_FREE of BSTRS
  38. #define SAFE_FREE(pv)            if(pv) { SysFreeString(pv); pv = NULL; }  
  39.  
  40. // **********************************************************************
  41. // wmain()
  42. // **********************************************************************
  43. INT wmain()
  44. {
  45.     VARIANT                    var;
  46.     ISQLNamespace*            pNS = NULL;            // pointer to Namespace
  47.  
  48.     // Initialize COM
  49.     //
  50.     CoInitialize(NULL);
  51.  
  52.     // Create the SQL Namespace object (loads SQLNS.ENU)
  53.     //
  54.     if FAILED(CoCreateInstance(CLSID_SQLNamespace, NULL, CLSCTX_INPROC_SERVER, IID_ISQLNamespace, (LPVOID*)&pNS))
  55.     {
  56.         // check if SQLNS.DLL is registered (use regsvr32 sqlns.dll)
  57.         //
  58.         return(1);
  59.     }
  60.  
  61.     // First step is to initialize the Namespave
  62.     // You can initialize it to have the default root (in which case your 
  63.     // server should have been previously registered) or you can specify a root.
  64.     // You can specify a server group, server, or a database as the root of the namespace
  65.     // For server group specify the group name and for a server or a database to be the root
  66.     // specify a connect string
  67.     // The connect string should be of the form (see ODBC manuals for more information)
  68.     // "Server=<servername>;UID=<login id>;pwd=<password>;Trusted_Connection=<Yes|No>;Database=<db name>"
  69.     // To specify local server use "Server=.;" if using Named Pipes, otherwise specify hostname.
  70.     // When the connect string is created call the Initialize method on the Namespace object
  71.     
  72.     VariantInit(&var);
  73.     V_VT(&var) = VT_BSTR;
  74.     // create a connect string, same way as in ODBC
  75.     // 
  76.     // some samples all connection to local server
  77.     // V_BSTR(&var) = SysAllocString(L"Server=.;UID=sa;pwd=;Trusted_Connection=No;");
  78.     // V_BSTR(&var) = SysAllocString(L"Server=.;UID=sa;pwd=;Trusted_Connection=No;Database=pubs");
  79.     //
  80.     V_BSTR(&var) = SysAllocString(L"Server=.;UID=;pwd=;Trusted_Connection=Yes");
  81.  
  82.     // use the connect string to initialize the SQL Namespace environment
  83.     // (you only do this once for the lifetime of the pNS object)
  84.     //
  85.     if (FAILED(pNS->Initialize(L"SQL Namespace Demo Application", SQLNSRootType_Server, &var, NULL)))
  86.     {
  87.         ReportError();
  88.         goto error;
  89.     }
  90.  
  91.     VariantClear(&var);
  92.  
  93.     TreeDump(pNS, NULL);
  94.  
  95. error:
  96.     SAFE_RELEASE(pNS);
  97.     return(0);
  98. };
  99.  
  100. // **********************************************************************
  101. // ReportError
  102. // **********************************************************************
  103. void ReportError()
  104. {
  105.     OLECHAR szMsg[512];
  106.     LPERRORINFO pErrorInfo;
  107.  
  108.     szMsg[0] = 0;
  109.     if ((SUCCEEDED(GetErrorInfo(0, &pErrorInfo))) && (pErrorInfo != NULL))
  110.     {
  111.         BSTR bstrDesc = NULL;
  112.         BSTR bstrSource = NULL;
  113.         
  114.         pErrorInfo->GetDescription(&bstrDesc);
  115.         pErrorInfo->GetSource(&bstrSource);
  116.  
  117.         if (bstrDesc && wcslen(bstrDesc))
  118.         {
  119.             wcscat(szMsg, L"\r\n\r\n");
  120.             if (bstrSource && wcslen(bstrSource))
  121.             {
  122.                 wcscat(szMsg, L"[");
  123.                 wcscat(szMsg, bstrSource);
  124.                 wcscat(szMsg, L"] - ");
  125.             }
  126.             wcscat(szMsg, bstrDesc);
  127.         }
  128.         if (bstrDesc)
  129.             SysFreeString(bstrDesc);
  130.         if (bstrSource)
  131.             SysFreeString(bstrSource);
  132.         pErrorInfo->Release();
  133.     }
  134.     
  135.     MessageBoxW(NULL, szMsg, L"Error", MB_OK);
  136.  
  137. }
  138.  
  139. // **********************************************************************
  140. // IsCommandSupportedByObject
  141. // **********************************************************************
  142. BOOL IsCommandSupportedByObject(ISQLNamespace* pNS, ISQLNamespaceObject* pObj, LPCOLESTR szCmd)
  143. {
  144.     ISQLNamespaceCommands*    pCmds;
  145.     ISQLNamespaceCommand*    pCmd = NULL;
  146.     VARIANT                    v;
  147.     BOOL                    bRet = FALSE;
  148.  
  149.     VariantInit(&v);
  150.  
  151.     if (SUCCEEDED(pObj->GetCommands(&pCmds)))
  152.     {
  153.         V_VT(&v) = VT_BSTR;
  154.         V_BSTR(&v) = SysAllocString(L"Security Wizard");
  155.         if (SUCCEEDED(pCmds->Item(v, &pCmd)) && pCmd)
  156.         {
  157.             bRet = TRUE;
  158.         }
  159.         
  160.         VariantClear(&v);
  161.         pCmd->Release();
  162.         pCmds->Release();
  163.     }
  164.     return bRet;
  165. }
  166.  
  167. // **********************************************************************
  168. // IsCommandSupportedByObject
  169. // **********************************************************************
  170. BOOL IsCommandSupportedByObject(ISQLNamespace* pNS, ISQLNamespaceObject* pObj, ULONG lCommandID)
  171. {
  172.     ISQLNamespaceCommands*    pCmds;
  173.     ISQLNamespaceCommand*    pCmd;
  174.     VARIANT                    v;
  175.     long                    l;
  176.     ULONG                    lCmdID;
  177.     BOOL                    bRet = FALSE;
  178.  
  179.     VariantInit(&v);
  180.  
  181.     if (SUCCEEDED(pObj->GetCommands(&pCmds)))
  182.     {
  183.         pCmds->GetCount(&l);
  184.         
  185.         V_VT(&v) = VT_I4;
  186.  
  187.         for (; l > 0; l--)
  188.         {
  189.             V_I4(&v) = l;
  190.             pCmds->Item(v, &pCmd);
  191.     
  192.             pCmd->GetCommandID(&lCmdID);
  193.             pCmd->Release();
  194.             if (lCmdID == lCommandID)
  195.             {
  196.                 bRet = TRUE;
  197.                 break;
  198.             }
  199.         }
  200.         pCmds->Release();
  201.     }
  202.     return bRet;
  203. }
  204.  
  205. // **********************************************************************
  206. // TreeDump - dump all objects in tree
  207. // **********************************************************************
  208. void TreeDump(ISQLNamespace* pNS, HSQLNSITEM hItem)
  209. {
  210.     static int iLevel;
  211.     HSQLNSITEM hTmp;
  212.  
  213.     if (!hItem)
  214.     {
  215.         pNS->GetRootItem(&hTmp);
  216.         iLevel = 0;
  217.         TreeDump(pNS, hTmp);
  218.     }
  219.     else
  220.     {
  221.         long l;
  222.         BSTR bstr;
  223.         pNS->GetChildrenCount(hItem, &l);
  224.         pNS->GetName(hItem, &bstr);
  225.  
  226.         for (int i = 0; i < iLevel; i++)
  227.         {
  228.             wprintf(L"\t");
  229. #ifdef _DEBUG
  230.             OutputDebugStringW(L"\t");
  231. #endif
  232.         }
  233.  
  234.         wprintf(L"%s\n", bstr);
  235.  
  236. #ifdef _DEBUG
  237.         OutputDebugStringW(bstr);
  238.         OutputDebugStringW(L"\r\n");
  239. #endif
  240.     
  241.         SysFreeString(bstr);
  242.         
  243.         pNS->GetFirstChildItem(hItem, SQLNSOBJECTTYPE_EMPTY, NULL, &hTmp);
  244.         
  245.         iLevel++;
  246.  
  247.         while (hTmp)
  248.         {
  249.             TreeDump(pNS, hTmp);
  250.             pNS->GetNextSiblingItem(hTmp, SQLNSOBJECTTYPE_EMPTY, NULL, &hTmp);
  251.         }
  252.         iLevel--;
  253.     }
  254. }
  255.