home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / SQLNS / cpp / dbprop / dbprop.cpp next >
Encoding:
C/C++ Source or Header  |  1998-06-15  |  7.3 KB  |  248 lines

  1. //**********************************************************************
  2. // file dbprop.cpp
  3. // SQL NameSpace Objects demo 
  4. // displaying the Database Properties Dialog for database pubs
  5. //**********************************************************************
  6.  
  7. #define STRICT                            // turn on strict type checking
  8. #define WIN32_LEAN_AND_MEAN                // we do not want to include the whole world
  9. #define INC_OLE2                        // include the needed OLE/COM files
  10. #define UNICODE                            // turn on Unicode
  11. #define _UNICODE
  12.  
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <initguid.h>
  16. #include <tchar.h>
  17. #include <olectl.h>
  18.  
  19. #include <sqlnsx.h>                        // include file for SQL Namespace objects
  20.                                         // installed in \mssql7\devtools\include
  21.  
  22. // some usefull helper macros to make the code more readable
  23. //
  24.  
  25. // SAFE_RELEASE of Com ptrs
  26. #define SAFE_RELEASE(pv)        if(pv) { (pv)->Release(); pv = NULL; }  
  27.  
  28. // SAFE_FREE of BSTRS
  29. #define SAFE_FREE(pv)            if(pv) { SysFreeString(pv); pv = NULL; }  
  30.  
  31. // forward function declaration should be in include file, but for clarity left in here
  32. //
  33. void ReportError();
  34. BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, LPCOLESTR szCmd);
  35. BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, long lCommandID);
  36.  
  37. int WINAPI WinMain(
  38.     HINSTANCE hInstance,            // handle to current instance
  39.     HINSTANCE hPrevInstance,        // handle to previous instance
  40.     LPSTR lpCmdLine,                // pointer to command line
  41.     int nCmdShow)                    // show state of window
  42. {
  43.     VARIANT                    var;
  44.     ISQLNamespace*            pNS = NULL;            // pointer to Namespace
  45.     HSQLNSITEM                hServer;            // handle to server
  46.     HSQLNSITEM                hDBs;                // handle to databases
  47.     HSQLNSITEM                hDB;                // handle to database (pubs)
  48.     ISQLNamespaceObject*    pDbObj = NULL;        // pointer to Namespace Object
  49.  
  50.     // Initialize COM
  51.     //
  52.     CoInitialize(NULL);
  53.  
  54.     // Create the SQL Namespace object (loads SQLNS.ENU)
  55.     //
  56.     if FAILED(CoCreateInstance(CLSID_SQLNamespace, NULL, CLSCTX_INPROC_SERVER, IID_ISQLNamespace, (LPVOID*)&pNS))
  57.     {
  58.         // check if SQLNS.ENU is registered (use regsvr32 c:\mssql7\binn\sqlns.enu)
  59.         //
  60.         return(1);
  61.     }
  62.  
  63.     // First step is to initialize the Namespave
  64.     // You can initialize it to have the default root (in which case your 
  65.     // server should have been previously registered) or you can specify a root.
  66.     // You can specify a server group, server, or a database as the root of the namespace
  67.     // For server group specify the group name and for a server or a database to be the root
  68.     // specify a connect string
  69.     // The connect string should be of the form (see ODBC manuals for more information)
  70.     // "Server=<servername>;UID=<login id>;pwd=<password>;Trusted_Connection=<Yes|No>;Database=<db name>"
  71.     // To specify local server use "Server=.;" if using Named Pipes, otherwise specify hostname.
  72.     // When the connect string is created call the Initialize method on the Namespace object
  73.     
  74.     VariantInit(&var);
  75.     V_VT(&var) = VT_BSTR;
  76.     // create a connect string, same way as in ODBC
  77.     // 
  78.     // some samples all connection to local server
  79.     // V_BSTR(&var) = SysAllocString(L"Server=.;UID=sa;pwd=;Trusted_Connection=No;");
  80.     // V_BSTR(&var) = SysAllocString(L"Server=.;UID=sa;pwd=;Trusted_Connection=No;Database=pubs");
  81.     //
  82.     V_BSTR(&var) = SysAllocString(L"Server=.;UID=;pwd=;Trusted_Connection=Yes");
  83.  
  84.     // use the connect string to initialize the SQL Namespace environment
  85.     // (you only do this once for the lifetime of the pNS object)
  86.     //
  87.     if (FAILED(pNS->Initialize(L"SQL Namespace Demo Application", SQLNSRootType_Server, &var, NULL)))
  88.     {
  89.         ReportError();
  90.         goto RetErr;
  91.     }
  92.     VariantClear(&var);
  93.  
  94.     // Get your root item. This is either server group, server or database, based on what
  95.     // you specified in the pNS->Initialize.
  96.     //
  97.     if (FAILED(pNS->GetRootItem(&hServer)))
  98.     {
  99.         ReportError();
  100.         goto RetErr;
  101.     }
  102.  
  103.     // From your root object, you can walk down the hierchy, by getting GetFirstChildItems
  104.     // Server->Databases
  105.     //
  106.     if (FAILED(pNS->GetFirstChildItem(hServer, SQLNSOBJECTTYPE_DATABASES, NULL, &hDBs)))
  107.     {
  108.         ReportError();
  109.         goto RetErr;
  110.     }
  111.     
  112.     // One level deeper we select a specific database
  113.     // Server->Databases->Database("pubs")
  114.     if (FAILED(pNS->GetFirstChildItem(hDBs, SQLNSOBJECTTYPE_DATABASE, L"pubs", &hDB)))
  115.     {
  116.         ReportError();
  117.         goto RetErr;
  118.     }
  119.     
  120.     // From the Database("pubs") we get a Namespace object, on which we can execute commands
  121.     //
  122.     if (FAILED(pNS->GetSQLNamespaceObject(hDB, &pDbObj)))
  123.     {
  124.         ReportError();
  125.         goto RetErr;
  126.     }
  127.  
  128.     // If you don't know if the object supports a particular command you can call 
  129.     // IsCommandSupportedByObject passing in the CommandID or command name of the 
  130.     // command you are interested in.
  131.     // Example: IsCommandSupportedByObject(pNS, pDbObj, L"Properties")
  132.     // Example: IsCommandSupportedByObject(pNS, pDbObj, SQLNS_CmdID_PROPERTIES)
  133.     
  134.     // Now execute the command, which will bring up the dialog.
  135.     // Normaly you should be specifying a parent window (in this sample we don't)
  136.     //
  137.     if (FAILED(pDbObj->ExecuteCommandByID(SQLNS_CmdID_PROPERTIES, NULL, SQLNamespace_DontCare)))
  138.     {
  139.         ReportError();
  140.         goto RetErr;
  141.     }
  142.     
  143.     // You can alternately execute a command by calling ExecuteCommandByName
  144.     // or you can invoke the command by indexing through the commands collection
  145.     // Code sample to index through the commands collection can be found in IsCommandSupportedByObject
  146.  
  147. RetErr:
  148.     SAFE_RELEASE(pDbObj);
  149.     SAFE_RELEASE(pNS);
  150.     return(0);
  151. };
  152.  
  153. void ReportError()
  154. {
  155.     OLECHAR szMsg[512];
  156.     LPERRORINFO pErrorInfo;
  157.  
  158.     szMsg[0] = 0;
  159.     if ((SUCCEEDED(GetErrorInfo(0, &pErrorInfo))) && (pErrorInfo != NULL))
  160.     {
  161.         BSTR bstrDesc = NULL;
  162.         BSTR bstrSource = NULL;
  163.         
  164.         pErrorInfo->GetDescription(&bstrDesc);
  165.         pErrorInfo->GetSource(&bstrSource);
  166.  
  167.         if (bstrDesc && wcslen(bstrDesc))
  168.         {
  169.             wcscat(szMsg, L"\r\n\r\n");
  170.             if (bstrSource && wcslen(bstrSource))
  171.             {
  172.                 wcscat(szMsg, L"[");
  173.                 wcscat(szMsg, bstrSource);
  174.                 wcscat(szMsg, L"] - ");
  175.             }
  176.             wcscat(szMsg, bstrDesc);
  177.         }
  178.         if (bstrDesc)
  179.             SysFreeString(bstrDesc);
  180.         if (bstrSource)
  181.             SysFreeString(bstrSource);
  182.         pErrorInfo->Release();
  183.     }
  184.     
  185.     MessageBoxW(NULL, szMsg, L"Error", MB_OK);
  186.  
  187. }
  188.  
  189. BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, LPCOLESTR szCmd)
  190. {
  191.     ISQLNamespaceCommands*    pCmds;
  192.     ISQLNamespaceCommand*    pCmd = NULL;
  193.     VARIANT                    v;
  194.     BOOL                    bRet = FALSE;
  195.  
  196.     VariantInit(&v);
  197.  
  198.     if (SUCCEEDED(pObj->GetCommands(&pCmds)))
  199.     {
  200.         V_VT(&v) = VT_BSTR;
  201.         V_BSTR(&v) = SysAllocString(L"Security Wizard");
  202.         if (SUCCEEDED(pCmds->Item(v, &pCmd)) && pCmd)
  203.         {
  204.             bRet = TRUE;
  205.         }
  206.         
  207.         VariantClear(&v);
  208.         pCmd->Release();
  209.         pCmds->Release();
  210.     }
  211.     return bRet;
  212. }
  213.  
  214. BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, ULONG lCommandID)
  215. {
  216.     ISQLNamespaceCommands*    pCmds;
  217.     ISQLNamespaceCommand*    pCmd;
  218.     VARIANT                    v;
  219.     long                    l;
  220.     ULONG                    lCmdID;
  221.     BOOL                    bRet = FALSE;
  222.  
  223.     VariantInit(&v);
  224.  
  225.     if (SUCCEEDED(pObj->GetCommands(&pCmds)))
  226.     {
  227.         pCmds->GetCount(&l);
  228.         
  229.         V_VT(&v) = VT_I4;
  230.  
  231.         for (; l > 0; l--)
  232.         {
  233.             V_I4(&v) = l;
  234.             pCmds->Item(v, &pCmd);
  235.     
  236.             pCmd->GetCommandID(&lCmdID);
  237.             pCmd->Release();
  238.             if (lCmdID == lCommandID)
  239.             {
  240.                 bRet = TRUE;
  241.                 break;
  242.             }
  243.         }
  244.         pCmds->Release();
  245.     }
  246.     return bRet;
  247. }
  248.