SQL-NS Sample Program Using C++

Sample C++ code for using SQL-NS to display the Properties dialog box for the pubs database is located in the \Mssql7\Devtools\Samples\Sqlns\Cpp\Dbprop directory. Use these files:

//**********************************************************************

// file dbprop.cpp

// SQL NameSpace Objects sample program

// Displaying the Database Properties dialog box for database pubs

//**********************************************************************

  

#define STRICT                            // Turn on strict type checking

#define WIN32_LEAN_AND_MEAN                // Restrict inclusions

#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 useful 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 the include file, but

// is left in here for clarity.

//

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 prompt

    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 SQLNamespace object

  

    // Initialize COM

    //

    CoInitialize(NULL);

  

    // Create the SQLNamespace 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 namespace

// 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 connection string. The connection

// 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 connection string is created, call the Initialize method on

// the SQLNamespace object.

    

    VariantInit(&var);

    V_VT(&var) = VT_BSTR;

    // Create a connection string, same way as when using 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 connection 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 pNS->Initialize.

    //

    if (FAILED(pNS->GetRootItem(&hServer)))

    {

        ReportError();

        goto RetErr;

    }

  

    // From your root object, you can walk down the hierarchy, by getting

    //GetFirstChildItems.

    // Server->Databases

    //

    if (FAILED(pNS->GetFirstChildItem(hServer, SQLNSOBJECTTYPE_DATABASES, NULL, &hDBs)))

    {

        ReportError();

        goto RetErr;

    }

    

    // One level deeper, 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), get a SQLNamespace object, on which you

    //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 box.

    // Normally you should be specifying a parent window (this sample

    // does not).

    //

    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;

}

  

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.