home *** CD-ROM | disk | FTP | other *** search
- // **********************************************************************
- // dumptree - dump all objects in the SQL Namespace tree
- //
- // using SQLNS, targetting SQL Server 7.0
- // **********************************************************************
-
- #define STRICT // strict type checking
- #define WIN32_LEAN_AND_MEAN // do not include the world
- #define INC_OLE2 // include OLE/COM files
-
- #include <windows.h>
- #include <stdio.h>
- #include <initguid.h>
- #include <tchar.h>
- #include <olectl.h>
-
- // #import "d:\\mssql7\\binn\\sqlns.enu" no_namespace
-
- #include <sqlnsx.h> // include file for SQL Namespace objects
- // installed in \mssql7\devtools\include
-
- // **********************************************************************
- // function declarations
- // **********************************************************************
- void ReportError();
- BOOL IsCommandSupportedByObject(ISQLNamespace* pNS, ISQLNamespaceObject* pObj, LPCOLESTR szCmd);
- BOOL IsCommandSupportedByObject(ISQLNamespace* pNS, ISQLNamespaceObject* pObj, long lCommandID);
- void TreeDump(ISQLNamespace* pNS, HSQLNSITEM hItem);
-
- // **********************************************************************
- // macros and #defines
- // **********************************************************************
-
- // 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; }
-
- // **********************************************************************
- // wmain()
- // **********************************************************************
- INT wmain()
- {
- VARIANT var;
- ISQLNamespace* pNS = NULL; // pointer to Namespace
-
- // 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.DLL is registered (use regsvr32 sqlns.dll)
- //
- 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 error;
- }
-
- VariantClear(&var);
-
- TreeDump(pNS, NULL);
-
- error:
- SAFE_RELEASE(pNS);
- return(0);
- };
-
- // **********************************************************************
- // ReportError
- // **********************************************************************
- 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);
-
- }
-
- // **********************************************************************
- // IsCommandSupportedByObject
- // **********************************************************************
- 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;
- }
-
- // **********************************************************************
- // IsCommandSupportedByObject
- // **********************************************************************
- 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;
- }
-
- // **********************************************************************
- // TreeDump - dump all objects in tree
- // **********************************************************************
- void TreeDump(ISQLNamespace* pNS, HSQLNSITEM hItem)
- {
- static int iLevel;
- HSQLNSITEM hTmp;
-
- if (!hItem)
- {
- pNS->GetRootItem(&hTmp);
- iLevel = 0;
- TreeDump(pNS, hTmp);
- }
- else
- {
- long l;
- BSTR bstr;
- pNS->GetChildrenCount(hItem, &l);
- pNS->GetName(hItem, &bstr);
-
- for (int i = 0; i < iLevel; i++)
- {
- wprintf(L"\t");
- #ifdef _DEBUG
- OutputDebugStringW(L"\t");
- #endif
- }
-
- wprintf(L"%s\n", bstr);
-
- #ifdef _DEBUG
- OutputDebugStringW(bstr);
- OutputDebugStringW(L"\r\n");
- #endif
-
- SysFreeString(bstr);
-
- pNS->GetFirstChildItem(hItem, SQLNSOBJECTTYPE_EMPTY, NULL, &hTmp);
-
- iLevel++;
-
- while (hTmp)
- {
- TreeDump(pNS, hTmp);
- pNS->GetNextSiblingItem(hTmp, SQLNSOBJECTTYPE_EMPTY, NULL, &hTmp);
- }
- iLevel--;
- }
- }
-