#include "stdafx.h"
#include "iostream.h"
#include "atlbase.h"
#include "comdef.h" 字串1
#pragma warning(push)
#pragma warning(disable : 4192)
#pragma warning(disable : 4146) 字串6
#import "esriSystem.olb"
raw_interfaces_only,                            
raw_native_types,                               
no_namespace,                                   
named_guids,                                     字串7
exclude("OLE_COLOR", "OLE_HANDLE", "VARTYPE")
#import "esriGeometry.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "esriDataSourcesFile.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "esriGeoDatabase.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
字串2
#pragma warning(pop)
字串5
//CString VariantToString1(VARIANT * va);
字串2
int main(int argc, char* argv[])
{
::CoInitialize(NULL);
字串1
IWorkspaceFactoryPtr pWSFactory(CLSID_ShapefileWorkspaceFactory);
IWorkspacePtr pWS;
// BSTR path;
// path = ::SysAllocString(L"C:\test");
HRESULT hr = pWSFactory->OpenFromFile(CComBSTR(L"C:\test\bj"),0,&pWS);
if (FAILED(hr) || pWS==NULL)
{
cerr<<"Failed to open the destination folder."<<endl;
return 1;
} 字串1
IFeatureWorkspacePtr pFWS(pWS);
IFeatureClassPtr pFClass;
pFWS->OpenFeatureClass (CComBSTR(L"bj_est1"),&pFClass);
字串3
IFeatureCursorPtr pFCursor;
pFClass->Search (NULL,VARIANT_TRUE,&pFCursor);
字串7
long lngFieldCount ;
IFieldsPtr pFields;
IFieldPtr pField;
pFCursor->get_Fields (&pFields);
pFields->get_FieldCount (&lngFieldCount); 字串4
IFeaturePtr pFeature;
pFCursor->NextFeature (&pFeature); 字串8
USES_CONVERSION; 字串9
long t;
CComBSTR bsValues;
CComVariant varValue;
esriFieldType esriFldType;
while (pFeature)
{
bsValues = CComBSTR(L"  ");
for (t =0; t < lngFieldCount ; t++)
{
pFields->get_Field(t, &pField);
pField->get_Type(&esriFldType);
switch (esriFldType)
{
case (esriFieldTypeGeometry):
bsValues += CComBSTR(L"Shape  ");
break;
default:
pFeature->get_Value(t, &varValue);
if (varValue.vt != VT_BSTR)
varValue.ChangeType(VT_BSTR); 字串4
bsValues += varValue.bstrVal;
bsValues += CComBSTR(L"  ");
break;
}
} 字串3
//  _bstr_t outString(bsValues,FALSE); //方法1:为控制台输出作准备
//  cout << outString<<endl;
//方法2:也可以用OLE2A来进行转化,
//当然需要加上 USES_CONVERSION;
cout << OLE2A(bsValues)<< endl;
pFCursor->NextFeature (&pFeature);
} 字串6
pWSFactory=NULL;
pWS=NULL;
pFWS=NULL;
pFClass=NULL;
pFCursor=NULL;
pFields=NULL;
pField=NULL;
字串8
::CoUninitialize();
字串3
return 0;
} 字串5