ADSI OLE DB Example

This following ADSI example shows a simple command-prompt program that uses the OLE DB interfaces to query a directory service. The beginning comment section describes which functions were removed to simplify the example. The complete example code is included in the ADSI SDK in the samples/activeds/sampapp/cxx/adsqry directory.

//--------------------------------------------------------------
//  Microsoft Active Directory 2.0 Sample Code
//  Copyright (C) Microsoft Corporation, 1997
//  File:       main.cxx
//  Contents:   Main for adsqry
//--------------------------------------------------------------
// ... global declarations, free memory calls, and error handling 
//        print routines, mapping input arguments to 
//        search arguments etc. omitted for brevity
 
#include "main.hxx"
 
int __cdecl
main( int argc, char ** argv)
{
 //  Load up a DBPropSet for DB data
 //    and a CmdPropSet for Command data
    hr = ProcessArgs(argc, argv);
    hr = CoInitialize(NULL);
    //
    // Instantiate a data source object for LDAP provider
    //
    hr = CoCreateInstance(
         CLSID_ADsDSOObject,
             0,
             CLSCTX_INPROC_SERVER,
             IID_IDBInitialize,
             (void **)&pIDBInit
             );
 // Initialize the Data Source
     hr = pIDBInit->Initialize();
    if (cDBPropSet) {
        pIDBInit->QueryInterface(
            IID_IDBProperties,
            (void**) &pIDBProperties);
        hr = pIDBProperties->SetProperties(
                 cDBPropSet,
                 rgDBPropSet);
 
    }
 // Get the interface to create a session
    pIDBInit->QueryInterface(
        IID_IDBCreateSession,
        (void**) &pIDBCS);
 
 // Create a session returning a pointer to its CreateCommand interface
    hr = pIDBCS->CreateSession(
             NULL,
             IID_IDBCreateCommand,
             (LPUNKNOWN*) &pIDBCreateCommand
             );
 // Create a command from the session object
    hr = pIDBCreateCommand->CreateCommand(
             NULL,
             IID_ICommandText,
             (LPUNKNOWN*) &pICommandText
             );
 // Set the CommandText for the Query
    hr = pICommandText->SetCommandText(
             rguidDialect,
             pszCommandText
             );
// Set the properties on the Command Object 
    if (cCmdPropSet) {
        hr = pICommandText->QueryInterface(
                 IID_ICommandProperties,
                 (void**) &pICommandProperties);
        hr = pICommandProperties->SetProperties(
                 cCmdPropSet,
                 rgCmdPropSet);
    }
 
    hr = pICommandText->QueryInterface(
             IID_ICommand,
             (void**) &pICommand);

 // Do the Query and get back a rowset
    hr = pICommand->Execute(
             NULL,
             IID_IRowset,
             NULL,
             NULL,
             (LPUNKNOWN *)&pIRowset
             );
    hr = pIRowset->QueryInterface(
             IID_IColumnsInfo,
             (void**) &pIColsInfo
             );
    hr = pIColsInfo->GetColumnInfo(
             &cCol,
             &prgColInfo,
             &szColNames
             );
 // The no. of attributes is one less than the no. of columns because of
 //  the Bookmark column
    nAttrs = cCol - 1;
    pMyStatus = (DBBINDSTATUS *) LocalAlloc(
                                     LPTR,
                                     sizeof(DBBINDSTATUS) * nAttrs
                                     );
    hr = CreateAccessorHelper(
             pIRowset,
             nAttrs,
             prgColInfo,
             &myAccessor,
             pMyStatus
             );
    pMyData = (Data *) LocalAlloc(
                           LPTR,
                           sizeof(Data) * nAttrs
                           );
    //  Get the rows; 256 at a time
    phRows = NULL;
    hr = pIRowset->GetNextRows(
             NULL,
             0,
             256,
             &cRowsObtained,
             &phRows
             );
    j = cRowsObtained;
    while (cRowsObtained) {
        for (i = 0; i < cRowsObtained; i++) {
 // Get the data from each row
            hr = pIRowset->GetData(
                     phRows[i],
                     myAccessor,
                     (void*)pMyData
                     );
           PrintData(pMyData, nAttrs, prgColInfo);
        }
        pIRowset->ReleaseRows(
                      cRowsObtained, 
                      phRows,
                      NULL,
                      NULL,
                      NULL
                      );
 // Get the next 256 rows
        hr = pIRowset->GetNextRows(
                 NULL,
                 0,
                 256,
                 &cRowsObtained,
                 &phRows
                 );
        j+=cRowsObtained;
    }
    printf("Rows printed = %d\n", j);
    exit(0);
 
}