Create a new shapefile
CreateShapefile.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 "CreateShapefile.h"

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

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

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

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

  // Open the folder where the shapefile will be created
  std::wcerr << L"Open " << (BSTR) path << L" to contain the shapefile as a workspace" << std::endl;
  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 E_FAIL;
  }
  IFeatureWorkspacePtr ipFeatureWKSP = ipWKSP;

  // Set up a simple fields collection
  IFieldsPtr ipFields(CLSID_Fields);
  IFieldsEditPtr ipFieldsEdit = ipFields;
  IFieldPtr ipField(CLSID_Field);
  IFieldEditPtr ipFieldEdit = ipField;
  
  // Make and add the shape field. It will need a geometry definition 
  //    and a spatial reference
  char* shapeFieldName = "Shape";
  ipFieldEdit->put_Name(CComBSTR(shapeFieldName));
  ipFieldEdit->put_Type(esriFieldTypeGeometry);
  IGeometryDefPtr ipGeomDef(CLSID_GeometryDef);
  IGeometryDefEditPtr ipGeomDefEdit = ipGeomDef;
  ipGeomDefEdit->put_GeometryType(esriGeometryPolygon);
  ISpatialReferencePtr ipUnkSpatial(CLSID_UnknownCoordinateSystem);
  ipGeomDefEdit->putref_SpatialReference(ipUnkSpatial);
  ipFieldEdit->putref_GeometryDef(ipGeomDef);
  ipFieldsEdit->AddField(ipField);

  // Add another miscellanesous text field
  ipField = IFieldPtr(CLSID_Field);
  ipFieldEdit = ipField;
  ipFieldEdit->put_Length(30);
  ipFieldEdit->put_Name(CComBSTR(L"Text Field"));
  ipFieldEdit->put_Type(esriFieldTypeString);
  ipFieldsEdit->AddField(ipField);

  // Create the shapefile (some parameters apply to geodatabase options and
  //    can be defaulted as NULL)
  cerr << "Create " << fullpath << endl;;
  IFeatureClassPtr ipFeatureClass;
  hr = ipFeatureWKSP->CreateFeatureClass(name, ipFields, 
                                         NULL, NULL, 
                                         esriFTSimple, 
                                         CComBSTR(shapeFieldName), 
                                         CComBSTR(L""), 
                                         &ipFeatureClass);

  return hr;
}