ImgQuery Example VC++

The following example performs a query of documents located on a
1.x Server. The query is based upon keywords assigned to the document.

void CAdmin1Dlg::OnImgquery() 
{
    int iReturn;
    // Perform the query and put results in objResults
        CString szDB = "test_db:";
        VARIANT vDB; vDB.vt = VT_BSTR; V_BSTR(&vDB) = 
            szDB.AllocSysString();
        lbResults.ResetContent();
        // Call ImgQuery( ); get a CStringCollect's IDispatch*.
        LPDISPATCH idispStringCollect=NULL;
        iReturn = ImgAdmin1.ImgQuery(vDB, "finddocs keyword = insurance",
            &idispStringCollect); 
        if (iReturn == 0)
        {
           IStringCollect* pIStringCollect;
           TRY
           {
               // Make new IStringCollect* and attach the Dispatch pointer
               pIStringCollect = new IStringCollect;
               pIStringCollect->AttachDispatch(idispStringCollect, TRUE);
               // Call CStringCollect's Get_NewEnum( ); 
               // get CEnumVARIANT's IUnknown*
              IUnknown* pEnumVarIUnknown = 0;
              pEnumVarIUnknown = pIStringCollect->Get_NewEnum();
              if (pEnumVarIUnknown)
              {
                  // Call CEnumVARIANT IUnknown's QueryInterface;
                  // get an IEnumVARIANT*
                  IEnumVARIANT* pIEnumVariant = 0;
                  pEnumVarIUnknown->QueryInterface(IID_IEnumVARIANT,
                      (void**)&pIEnumVariant); 
                  if(pIEnumVariant)
                  {
                    // Call the IEnumVARIANT's Next( ) till all strings
                    // are retrieved  
                    VARIANT vName;
                    while (S_OK == pIEnumVariant->Next(1, &vName, NULL))
                    {
                           lbResults.AddString((CString)V_BSTR(&vName));
                    }
                    iReturn = 0;       
                    pIEnumVariant->Release();
                  }
                  pEnumVarIUnknown->Release();
              } 
              pIStringCollect->ReleaseDispatch();
              delete pIStringCollect;
              ImgAdmin1.ImgQueryEnd();
           }
           CATCH_ALL (e)
           {
            CString msg; 
            pIStringCollect->ReleaseDispatch();
            delete pIStringCollect;
            ImgAdmin1.ImgQueryEnd();
            msg.Format("Unexpected error 0x%lX", ((COleException *)e)->m_sc);
            MessageBox(msg);
           }
           END_CATCH_ALL
        }
}