Query a shapefile
QueryShapefile.cpp
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

  
#include "QueryShapefile.h"

int main(int argc, char* argv[])
{
  cerr << "Query Shapefile -- ArcGIS Engine Developer Sample" << endl;

  if (argc != 2)
  {
    cerr << "Usage: QueryShapefile [shapefile to query]" 
         << endl;
    AoExit(0);
  }
  
  char* fullpath = argv[1];

  if (!InitializeApp())
  {
    AoExit(0);
  }
  
  HRESULT hr = Query(fullpath);
  if (FAILED(hr))
    cerr << "Query failed" << endl;
  else
    cerr << "Done!" << endl;
  
  ShutdownApp();
  AoExit(0);
}

HRESULT Query(char* fullpath)
{
  CComBSTR path;
  CComBSTR name;
  HRESULT hr = GetParentDirFromFullPath(fullpath, &path);
  if (FAILED(hr) || path.Length() <= 0)
  {
    std::cerr << "Couldn't get input path." << std::endl;
    return E_FAIL;
  }
  hr = GetFileFromFullPath(fullpath, &name);
  if (FAILED(hr) || name.Length() <= 0)
  {
    std::cerr << "Couldn't get input file name." << std::endl;
    return E_FAIL;
  }

  // Open the shapefile folder
  IWorkspaceFactoryPtr ipWKSPFac(CLSID_ShapefileWorkspaceFactory);
  IWorkspacePtr ipWKSP;
  hr = ipWKSPFac->OpenFromFile(path, 0, &ipWKSP);
  if (FAILED(hr) || ipWKSP == 0)
  {
    cerr << "Failed to open the destination folder." << endl;
    return hr;
  }

  IFeatureWorkspacePtr ipFeatureWKSP(ipWKSP);
  IFeatureClassPtr ipFeatureClass;
  hr = ipFeatureWKSP->OpenFeatureClass(name, &ipFeatureClass);
  if (FAILED(hr) || ipFeatureClass == 0)
  {
    cerr << "Failed to open the feature class." << endl;
    return hr;
  }
  
  IFeatureCursorPtr ipFeatureCursor;
  ipFeatureClass->Search(0, VARIANT_TRUE, &ipFeatureCursor);
  
  IFieldsPtr ipFields;
  ipFeatureCursor->get_Fields(&ipFields);

  long lFieldCount;
  ipFields->get_FieldCount(&lFieldCount);
  
  IFieldPtr ipField;
  CComBSTR bsFieldNames;
  CComBSTR bsFieldName;
  for (int i = 0; i < lFieldCount; ++i)
  {
    ipFields->get_Field(i, &ipField);
    ipField->get_Name(&bsFieldName);
    bsFieldNames += CComBSTR(L"  "); 
    bsFieldNames += bsFieldName;
  }

  std::wcerr << (BSTR) bsFieldNames << std::endl;

  IFeaturePtr ipFeature;
  ipFeatureCursor->NextFeature(&ipFeature);

  CComBSTR bsValues;
  CComVariant varValue;
  esriFieldType esriFldType;
  while (ipFeature)
  {
    bsValues = CComBSTR(L"  ");
    for (int i = 0; i < lFieldCount; ++i)
    {
      ipFields->get_Field(i, &ipField);
      ipField->get_Type(&esriFldType);
      switch (esriFldType)
      {
      case (esriFieldTypeGeometry):
        bsValues += CComBSTR(L"Shape  ");
        break;
      default:
        ipFeature->get_Value(i, &varValue);
        if (varValue.vt != VT_BSTR)
          varValue.ChangeType(VT_BSTR);
        bsValues += varValue.bstrVal;
        bsValues += CComBSTR(L"  ");
        break;
      }
    }
    std::wcerr << (BSTR) bsValues << std::endl;
    ipFeatureCursor->NextFeature(&ipFeature);
  }
    
  return S_OK;
}