home *** CD-ROM | disk | FTP | other *** search
- //**********************************************************************
- // file dbprop.cpp
- // SQL NameSpace Objects demo
- // displaying the Database Properties Dialog for database pubs
- //**********************************************************************
-
- #define STRICT // turn on strict type checking
- #define WIN32_LEAN_AND_MEAN // we do not want to include the whole world
- #define INC_OLE2 // include the needed OLE/COM files
- #define UNICODE // turn on Unicode
- #define _UNICODE
-
- #include <windows.h>
- #include <stdio.h>
- #include <initguid.h>
- #include <tchar.h>
- #include <olectl.h>
-
- #include <sqlnsx.h> // include file for SQL Namespace objects
- // installed in \mssql7\devtools\include
-
- // some usefull helper macros to make the code more readable
- //
-
- // SAFE_RELEASE of Com ptrs
- #define SAFE_RELEASE(pv) if(pv) { (pv)->Release(); pv = NULL; }
-
- // SAFE_FREE of BSTRS
- #define SAFE_FREE(pv) if(pv) { SysFreeString(pv); pv = NULL; }
-
- // forward function declaration should be in include file, but for clarity left in here
- //
- void ReportError();
- BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, LPCOLESTR szCmd);
- BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, long lCommandID);
-
- int WINAPI WinMain(
- HINSTANCE hInstance, // handle to current instance
- HINSTANCE hPrevInstance, // handle to previous instance
- LPSTR lpCmdLine, // pointer to command line
- int nCmdShow) // show state of window
- {
- VARIANT var;
- ISQLNamespace* pNS = NULL; // pointer to Namespace
- HSQLNSITEM hServer; // handle to server
- HSQLNSITEM hDBs; // handle to databases
- HSQLNSITEM hDB; // handle to database (pubs)
- ISQLNamespaceObject* pDbObj = NULL; // pointer to Namespace Object
-
- // Initialize COM
- //
- CoInitialize(NULL);
-
- // Create the SQL Namespace object (loads SQLNS.ENU)
- //
- if FAILED(CoCreateInstance(CLSID_SQLNamespace, NULL, CLSCTX_INPROC_SERVER, IID_ISQLNamespace, (LPVOID*)&pNS))
- {
- // check if SQLNS.ENU is registered (use regsvr32 c:\mssql7\binn\sqlns.enu)
- //
- return(1);
- }
-
- // First step is to initialize the Namespave
- // You can initialize it to have the default root (in which case your
- // server should have been previously registered) or you can specify a root.
- // You can specify a server group, server, or a database as the root of the namespace
- // For server group specify the group name and for a server or a database to be the root
- // specify a connect string
- // The connect string should be of the form (see ODBC manuals for more information)
- // "Server=<servername>;UID=<login id>;pwd=<password>;Trusted_Connection=<Yes|No>;Database=<db name>"
- // To specify local server use "Server=.;" if using Named Pipes, otherwise specify hostname.
- // When the connect string is created call the Initialize method on the Namespace object
-
- VariantInit(&var);
- V_VT(&var) = VT_BSTR;
- // create a connect string, same way as in ODBC
- //
- // some samples all connection to local server
- // V_BSTR(&var) = SysAllocString(L"Server=.;UID=sa;pwd=;Trusted_Connection=No;");
- // V_BSTR(&var) = SysAllocString(L"Server=.;UID=sa;pwd=;Trusted_Connection=No;Database=pubs");
- //
- V_BSTR(&var) = SysAllocString(L"Server=.;UID=;pwd=;Trusted_Connection=Yes");
-
- // use the connect string to initialize the SQL Namespace environment
- // (you only do this once for the lifetime of the pNS object)
- //
- if (FAILED(pNS->Initialize(L"SQL Namespace Demo Application", SQLNSRootType_Server, &var, NULL)))
- {
- ReportError();
- goto RetErr;
- }
- VariantClear(&var);
-
- // Get your root item. This is either server group, server or database, based on what
- // you specified in the pNS->Initialize.
- //
- if (FAILED(pNS->GetRootItem(&hServer)))
- {
- ReportError();
- goto RetErr;
- }
-
- // From your root object, you can walk down the hierchy, by getting GetFirstChildItems
- // Server->Databases
- //
- if (FAILED(pNS->GetFirstChildItem(hServer, SQLNSOBJECTTYPE_DATABASES, NULL, &hDBs)))
- {
- ReportError();
- goto RetErr;
- }
-
- // One level deeper we select a specific database
- // Server->Databases->Database("pubs")
- if (FAILED(pNS->GetFirstChildItem(hDBs, SQLNSOBJECTTYPE_DATABASE, L"pubs", &hDB)))
- {
- ReportError();
- goto RetErr;
- }
-
- // From the Database("pubs") we get a Namespace object, on which we can execute commands
- //
- if (FAILED(pNS->GetSQLNamespaceObject(hDB, &pDbObj)))
- {
- ReportError();
- goto RetErr;
- }
-
- // If you don't know if the object supports a particular command you can call
- // IsCommandSupportedByObject passing in the CommandID or command name of the
- // command you are interested in.
- // Example: IsCommandSupportedByObject(pNS, pDbObj, L"Properties")
- // Example: IsCommandSupportedByObject(pNS, pDbObj, SQLNS_CmdID_PROPERTIES)
-
- // Now execute the command, which will bring up the dialog.
- // Normaly you should be specifying a parent window (in this sample we don't)
- //
- if (FAILED(pDbObj->ExecuteCommandByID(SQLNS_CmdID_PROPERTIES, NULL, SQLNamespace_DontCare)))
- {
- ReportError();
- goto RetErr;
- }
-
- // You can alternately execute a command by calling ExecuteCommandByName
- // or you can invoke the command by indexing through the commands collection
- // Code sample to index through the commands collection can be found in IsCommandSupportedByObject
-
- RetErr:
- SAFE_RELEASE(pDbObj);
- SAFE_RELEASE(pNS);
- return(0);
- };
-
- void ReportError()
- {
- OLECHAR szMsg[512];
- LPERRORINFO pErrorInfo;
-
- szMsg[0] = 0;
- if ((SUCCEEDED(GetErrorInfo(0, &pErrorInfo))) && (pErrorInfo != NULL))
- {
- BSTR bstrDesc = NULL;
- BSTR bstrSource = NULL;
-
- pErrorInfo->GetDescription(&bstrDesc);
- pErrorInfo->GetSource(&bstrSource);
-
- if (bstrDesc && wcslen(bstrDesc))
- {
- wcscat(szMsg, L"\r\n\r\n");
- if (bstrSource && wcslen(bstrSource))
- {
- wcscat(szMsg, L"[");
- wcscat(szMsg, bstrSource);
- wcscat(szMsg, L"] - ");
- }
- wcscat(szMsg, bstrDesc);
- }
- if (bstrDesc)
- SysFreeString(bstrDesc);
- if (bstrSource)
- SysFreeString(bstrSource);
- pErrorInfo->Release();
- }
-
- MessageBoxW(NULL, szMsg, L"Error", MB_OK);
-
- }
-
- BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, LPCOLESTR szCmd)
- {
- ISQLNamespaceCommands* pCmds;
- ISQLNamespaceCommand* pCmd = NULL;
- VARIANT v;
- BOOL bRet = FALSE;
-
- VariantInit(&v);
-
- if (SUCCEEDED(pObj->GetCommands(&pCmds)))
- {
- V_VT(&v) = VT_BSTR;
- V_BSTR(&v) = SysAllocString(L"Security Wizard");
- if (SUCCEEDED(pCmds->Item(v, &pCmd)) && pCmd)
- {
- bRet = TRUE;
- }
-
- VariantClear(&v);
- pCmd->Release();
- pCmds->Release();
- }
- return bRet;
- }
-
- BOOL IsCommandSupportedByObject(ISQLNamespace * pNS, ISQLNamespaceObject * pObj, ULONG lCommandID)
- {
- ISQLNamespaceCommands* pCmds;
- ISQLNamespaceCommand* pCmd;
- VARIANT v;
- long l;
- ULONG lCmdID;
- BOOL bRet = FALSE;
-
- VariantInit(&v);
-
- if (SUCCEEDED(pObj->GetCommands(&pCmds)))
- {
- pCmds->GetCount(&l);
-
- V_VT(&v) = VT_I4;
-
- for (; l > 0; l--)
- {
- V_I4(&v) = l;
- pCmds->Item(v, &pCmd);
-
- pCmd->GetCommandID(&lCmdID);
- pCmd->Release();
- if (lCmdID == lCommandID)
- {
- bRet = TRUE;
- break;
- }
- }
- pCmds->Release();
- }
- return bRet;
- }
-