这个错误我是找了好多天才找到了
AE(ArcgisEngine)在用VC环境进行开发时,对license的初始化失败,
并在VC的编译输出窗口中提示Could not bind to a valid ArcGIS installation
是因为VC没找到Arcgis Engine的安装位置,参考官网的介绍(好不容易找到的)
http://help.arcgis.com/EN/sdk/10.0/ArcObjects_NET/conceptualhelp/index.html#//000100000nmm000000
Loading an ArcGIS runtime
对于独立的Arcgis 应用程序必须先绑定一个runtime,方法如下
// Load the ArcGISVersion library.
// This code is commonly placed in the StdAfx.h header file.
//这段代码放到stdafx.h里(或者放到主要的头文件中)
#import "libid:6FCCEDE0-179D-4D12-B586-58C88D26CA78" raw_interfaces_only no_implementation
--------------------------------------分割线--------------------------------------------------------------
// The following code can be used to bind to ArcGIS Engine Runtime:
//下面的代码要加在::AoInitialize(NULL);之后,IAoInitializePtr ipInit(CLSID_AoInitialize);之前
ArcGISVersionLib::IArcGISVersionPtr ipVer(__uuidof(ArcGISVersionLib ::VersionManager));
VARIANT_BOOL succeeded;
if (FAILED(ipVer->LoadVersion(ArcGISVersionLib::esriArcGISEngine, L "10.0", &succeeded)))
return 0;
------------------------------
例如 main.cpp
#import "libid:6FCCEDE0-179D-4D12-B586-58C88D26CA78" raw_interfaces_only no_implementation
#include <ArcSDK.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int LocateTheArcgisVersion()
{
ArcGISVersionLib::IArcGISVersionPtr ipVer(__uuidof(ArcGISVersionLib::VersionManager));
VARIANT_BOOL succeeded;
if (FAILED(ipVer->LoadVersion(ArcGISVersionLib::esriArcGISEngine, L"10.0",&succeeded)))
return 0;
return 1;
}
int main(int argc, char **argv)
{
::AoInitialize(NULL);
LocateTheArcgisVersion();
// ArcGIS Engine licensing
IAoInitializePtr ipInit(CLSID_AoInitialize);
esriLicenseStatus status;
ipInit->Initialize(esriLicenseProductCodeEngine,&status);
if(status == esriLicenseCheckedOut)
{
cout<<"lisense ok";
}
::AoUninitialize();
system("PAUSE");
AoExit(0);
return 0;
}
我是使用的ArcGIS Desktop 10开发的,所以我的代码如下:
stdafx.h:
- // stdafx.h : 标准系统包含文件的包含文件,
- // 或是经常使用但不常更改的
- // 特定于项目的包含文件
- #pragma once
- #ifndef _SECURE_ATL
- #define _SECURE_ATL 1
- #endif
- #ifndef VC_EXTRALEAN
- #define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料
- #endif
- // 如果您必须使用下列所指定的平台之前的平台,则修改下面的定义。
- // 有关不同平台的相应值的最新信息,请参考 MSDN。
- #ifndef WINVER // 允许使用特定于 Windows XP 或更高版本的功能。
- #define WINVER 0x0501 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
- #endif
- #ifndef _WIN32_WINNT // 允许使用特定于 Windows XP 或更高版本的功能。
- #define _WIN32_WINNT 0x0501 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
- #endif
- #ifndef _WIN32_WINDOWS // 允许使用特定于 Windows 98 或更高版本的功能。
- #define _WIN32_WINDOWS 0x0410 // 将它更改为适合 Windows Me 或更高版本的相应值。
- #endif
- #ifndef _WIN32_IE // 允许使用特定于 IE 6.0 或更高版本的功能。
- #define _WIN32_IE 0x0600 // 将此值更改为相应的值,以适用于 IE 的其他版本。值。
- #endif
- #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的
- // 关闭 MFC 对某些常见但经常可放心忽略的警告消息的隐藏
- #define _AFX_ALL_WARNINGS
- #include <afxwin.h> // MFC 核心组件和标准组件
- #include <afxext.h> // MFC 扩展
- #include <afxdisp.h> // MFC 自动化类
- #ifndef _AFX_NO_OLE_SUPPORT
- #include <afxdtctl.h> // MFC 对 Internet Explorer 4 公共控件的支持
- #endif
- #ifndef _AFX_NO_AFXCMN_SUPPORT
- #include <afxcmn.h> // MFC 对 Windows 公共控件的支持
- #endif // _AFX_NO_AFXCMN_SUPPORT
- #ifdef _UNICODE
- #if defined _M_IX86
- #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
- #elif defined _M_IA64
- #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
- #elif defined _M_X64
- #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
- #else
- #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
- #endif
- #endif
- #import "libid:6FCCEDE0-179D-4D12-B586-58C88D26CA78" raw_interfaces_only no_implementation
初始化:
ArcgisOpr.cpp:
- #include "stdafx.h"
- #include "ArcgisOpr.h"
- ArcgisOpr::ArcgisOpr(void)
- {
- m_pAo = NULL;
- m_pFcls = NULL;
- m_pFwks = NULL;
- m_isOpen = 0;
- m_clsType = esriGeometryNull;
- }
- ArcgisOpr::~ArcgisOpr(void)
- {
- CloseFeatureClass();
- CoUninitialize();
- }
- int ArcgisOpr::LocateTheArcgisVersion()
- {
- ArcGISVersionLib::IArcGISVersionPtr ipVer(__uuidof(ArcGISVersionLib::VersionManager));
- VARIANT_BOOL succeeded;
- if (FAILED(ipVer->LoadVersion(ArcGISVersionLib::esriArcGISDesktop, L"10.0",&succeeded)))
- return 0;
- return 1;
- }
- long ArcgisOpr::InitFcls(const char* pszWSFilePath)
- {
- TCHAR strPath[100] = {0};
- HRESULT hr = S_FALSE;
- esriLicenseStatus esriLice = esriLicenseUnavailable;
- BSTR bstrPath = NULL;
- IWorkspacePtr ipWksPtr = NULL;
- IWorkspaceFactoryPtr ipWksFtyPtr = NULL;
- //com组建初始化
- CoInitialize(NULL);
- LocateTheArcgisVersion();
- /*
- ::AoInitialize(NULL);
- LocateTheArcgisVersion();
- // ArcGIS Engine licensing
- IAoInitializePtr ipInit(CLSID_AoInitialize);
- esriLicenseStatus status;
- ipInit->Initialize(esriLicenseProductCodeEngine,&status);
- if(status == esriLicenseCheckedOut)
- {
- cout<<"lisense ok";
- }
- ::AoUninitialize();
- system("PAUSE");
- AoExit(0);
- return 0;
- */
- //初始化AO组件的license
- m_pAo.CreateInstance(CLSID_AoInitialize);
- hr = m_pAo->Initialize(esriLicenseProductCodeArcInfo,&esriLice);
- //建立ACCESS数据库工作区间工厂
- hr = ipWksFtyPtr.CreateInstance(CLSID_ShapefileWorkspaceFactory);
- if (FAILED(hr))
- {
- return(0);
- }
- //获取工作区
- bstrPath = _com_util::ConvertStringToBSTR(pszWSFilePath);
- hr = ipWksFtyPtr->OpenFromFile(bstrPath,0,&ipWksPtr);
- if (FAILED(hr))
- {
- return(0);
- }
- //得到要素类工作空间
- hr = ipWksPtr->QueryInterface(IID_IFeatureWorkspace,(void**)&m_pFwks);
- if (FAILED(hr))
- {
- return(0);
- }
- //释放指针
- if (bstrPath!=NULL)
- {
- SysFreeString(bstrPath);
- bstrPath = NULL;
- }
- return(1);
- }
- // //功能:打开地理数据库
- // //参数:clsName [in]-地理数据库名字
- // //返回值:成功返回1,失败返回0或者负数
- long ArcgisOpr::OpenFeatureClass(const char *szClsName)
- {
- IWorkspacePtr pWksPtr = NULL;
- IFeatureWorkspacePtr pFWksPtr = NULL;
- IFeatureClassPtr pFclsPtr = NULL;
- IWorkspaceFactoryPtr pWksFPtr = NULL;
- HRESULT hr = S_FALSE;
- long rtn = 0;
- BSTR clsName = _com_util::ConvertStringToBSTR(szClsName);
- if(clsName==NULL)
- return (0);
- //1.在打开之前先关闭前面打开的
- CloseFeatureClass();
- //2、获取工作空间
- //3。打开地理数据库要素类
- hr = m_pFwks->OpenFeatureClass(clsName,&m_pFcls);
- if(SUCCEEDED(hr))
- {
- //4。取要素类类型
- hr = m_pFcls->get_ShapeType(&m_clsType);
- m_isOpen = 1;
- if(SUCCEEDED(hr))
- {
- rtn = 1;
- }
- }
- else
- {
- CloseFeatureClass();
- rtn = -1;
- }
- return(rtn);
- }
- // //功能:关闭要素类
- // //参数:无
- // //返回:成功返回1,失败返回0或者负数
- long ArcgisOpr::CloseFeatureClass()
- {
- m_pFcls = NULL;
- m_isOpen = 0;
- m_clsType = esriGeometryNull;
- return (1);
- }
- long ArcgisOpr::GetClsType(esriGeometryType type)
- {
- type = m_clsType;
- return(1);
- }
- long ArcgisOpr::IsOpened()
- {
- return m_isOpen;
- }
- //获取要素个数
- long ArcgisOpr::GetFeasNum(long *pnCnt)
- {
- IQueryFilterPtr pQuery = NULL;
- HRESULT hr = S_FALSE;
- long rtn = 0;
- pQuery.CreateInstance(CLSID_QueryFilter);
- hr = m_pFcls->FeatureCount(pQuery,pnCnt);
- if(SUCCEEDED(hr))
- rtn=1;
- return (rtn);
- }
- //获取要素类的属性结构
- long ArcgisOpr::GetAttStru(IFields **pFlds)
- {
- ITablePtr pTab = NULL;
- HRESULT hr = S_FALSE;
- pTab = m_pFcls;
- hr = pTab->get_Fields(pFlds);
- if (SUCCEEDED(hr)==1)
- {
- return(1);
- }
- return(0);
- }
- //获取某一要素的属性值
- long ArcgisOpr::GetAtt(long fID/*从0开始*/, ARC_IRow **pRow)
- {
- long rtn=0;
- IFeaturePtr pFeature;
- HRESULT hr = 0;
- IFieldsPtr ptfields;
- if(m_isOpen==0)
- return (0);
- hr = m_pFcls->GetFeature(fID,&pFeature);
- if(SUCCEEDED(hr))
- {
- hr= pFeature->QueryInterface(IID_ARC_IRow,(void**)pRow);
- if(pRow!=NULL)
- {
- rtn =1;
- }
- }
- return (rtn);
- }
- //通过要素名称获取要素的ID
- long ArcgisOpr::GetFeaIDByName(const char* pszFldName,const char* pszFeaName,long *pnID)
- {
- int i = 0;
- long rtn = 0;
- long nCnt =0;
- ARC_IRowPtr pArow = NULL;
- IRowBufferPtr pRowBuf = NULL;
- HRESULT hr = S_FALSE;
- ITablePtr pTbl = m_pFcls;
- IFeaturePtr pFea = NULL;
- IFieldsPtr pFlds = NULL;
- char szFldName[64] = {0};
- long nIndex = 0;
- rtn = GetFeasNum(&nCnt);
- if (rtn <=0)
- {
- return(0);
- }
- GetAttStru(&pFlds);
- long nFldCnt;
- BSTR bstrFldName = NULL;
- bstrFldName = _com_util::ConvertStringToBSTR(pszFldName);
- pFlds->FindField(bstrFldName,&nIndex);
- VARIANT varValue;
- for (i=0;i<nCnt;i++)
- {
- hr = m_pFcls->GetFeature(i,&pFea);
- pArow = pFea;
- VariantInit(&varValue);
- pArow->get_Value(nIndex,&varValue);
- if(strcmp(pszFeaName,_com_util::ConvertBSTRToString(varValue.bstrVal))==0)
- {
- *pnID = i;
- }
- else
- {
- continue;
- }
- if (bstrFldName!=NULL)
- {
- SysFreeString(bstrFldName);
- bstrFldName = NULL;
- }
- }
- return(0);
- }
- //获取多边形外界矩形范围
- long ArcgisOpr::polygon_GetRect(long id,D_RECT *rect)
- {
- IFeaturePtr pFeature;
- HRESULT Isucc = 0;
- IEnvelopePtr pEnve = NULL;
- //1.判断入口参数
- if(id<1)
- return (0);
- if(m_isOpen==0)
- return (0);
- // id=id-1; //esri的要素id从0开始,zoyon从1开始
- //2.取要素
- Isucc = m_pFcls->GetFeature(id,&pFeature);
- Isucc = pFeature->get_Extent(&pEnve);
- if(FAILED(Isucc))
- return(0);
- pEnve->get_XMin(&rect->xmin);
- pEnve->get_YMin(&rect->ymin);
- pEnve->get_XMax(&rect->xmax);
- pEnve->get_YMax(&rect->ymax);
- return (1);
- }
- //获取多边形的边界点
- long ArcgisOpr::GetDotsByPolygon(long nID,long *pnPolyCnt,long *pnTolPntCnt,/*D_DOT *pDots*/D_DOT Dots[])
- {
- HRESULT hr = S_FALSE;
- IFeaturePtr pFea = NULL;
- IGeometryPtr pGeo = NULL;
- IPolygonPtr pPolygon = NULL;
- IGeometryCollectionPtr pGeoCol = NULL;
- ISegmentCollectionPtr pSegCol = NULL;
- IGeometryPtr pSmallPoly = NULL;
- ISegmentPtr pSeg = NULL;
- IPointPtr pPnt = NULL;
- D_DOT *dots = NULL;
- long nPolygonCnt = 0;
- long nSegCnt = 0;
- long nPntCnt = 0;
- long i = 0;
- int j = 0;
- long nPreNum = 0;
- long nTolPntsNum = 0;
- if (m_clsType == esriGeometryPolygon)
- {
- m_pFcls->GetFeature(nID,&pFea);
- pFea->get_Shape(&pGeo);
- pPolygon = pGeo;
- pGeoCol = pPolygon;
- pGeoCol->get_GeometryCount(&nPolygonCnt);
- dots = new D_DOT[20000];
- for (i = 0;i<nPolygonCnt;i++)
- {
- pGeoCol->get_Geometry(i,&pSmallPoly);
- pSegCol = pSmallPoly;
- pSegCol->get_SegmentCount(&nSegCnt);
- nPntCnt = nSegCnt+1;
- for (j = 0;j<nSegCnt;j++)
- {
- pSegCol->get_Segment(j,&pSeg);
- pSeg->get_FromPoint(&pPnt);
- pPnt->get_X(&dots[j+nTolPntsNum].x);
- pPnt->get_Y(&dots[j+nTolPntsNum].y);
- dots[j+nTolPntsNum].nHoleID = i+1;
- dots[j+nTolPntsNum].nIndex = j+1;
- if (j==53)
- {
- j=53;
- }
- }
- pSeg->get_ToPoint(&pPnt);
- pPnt->get_X(&dots[j+nTolPntsNum].x);
- pPnt->get_Y(&dots[j+nTolPntsNum].y);
- dots[j+nTolPntsNum].nHoleID = i+1;
- dots[j+nTolPntsNum].nIndex = j+1;
- nPreNum = nTolPntsNum;
- nTolPntsNum = nTolPntsNum+nPntCnt;
- }
- *pnPolyCnt = nPolygonCnt;
- *pnTolPntCnt = nTolPntsNum;
- for(i=0;i<nTolPntsNum;i++)
- {
- Dots[i] = dots[i];
- }
- delete []dots;
- return(1);
- }
- delete []dots;
- return(1);
- }
- //添加点到要素类中。
- long ArcgisOpr::AddDots(const char* pszName,long nPntsCnt,D_DOT dots[])
- {
- HRESULT hr = S_FALSE;
- IFeaturePtr pFea = NULL;
- IPointPtr pPnt = NULL;
- BSTR bstrName = NULL;
- bstrName = _com_util::ConvertStringToBSTR(pszName);
- m_pFwks->OpenFeatureClass(bstrName,&m_pFcls1);
- int i= 0;
- for (i = 0;i<nPntsCnt;i++)
- {
- m_pFcls1->CreateFeature(&pFea);
- pPnt.CreateInstance(CLSID_Point);
- pPnt->put_X(dots[i].x);
- pPnt->put_Y(dots[i].y);
- pFea->putref_Shape(pPnt);
- pFea->Store();
- }
- if(bstrName!=NULL)
- {
- SysFreeString(bstrName);
- bstrName = NULL;
- }
- return(1);
- }
- //获取点要素类中所有的点
- long ArcgisOpr::GetAllDots(const char* pszClsName,D_DOT dots[],long *pPointsNum){
- HRESULT hr = S_FALSE;
- IFeaturePtr pFea = NULL;
- IPointPtr pPnt = NULL;
- IGeometryPtr pGeo = NULL;
- BSTR bstrName = NULL;
- D_DOT dot;
- bstrName = _com_util::ConvertStringToBSTR(pszClsName);
- m_pFwks->OpenFeatureClass(bstrName,&m_pFcls1);
- int i= 0;
- long nFeaNum = 0;
- m_pFcls1->FeatureCount(NULL,&nFeaNum);
- for (i = 0;i<nFeaNum;i++)
- {
- m_pFcls1->GetFeature(i,&pFea);
- pFea->get_Shape(&pGeo);
- pPnt = pGeo;
- pPnt->get_X(&(dot.x));
- pPnt->get_Y(&(dot.y));
- dots[i] = dot;
- }
- *pPointsNum = nFeaNum;
- if(bstrName!=NULL)
- {
- SysFreeString(bstrName);
- bstrName = NULL;
- }
- return(1);
- }
- //获得所有的县
- long ArcgisOpr::GetAllXian(const char *pszClsName,Xian xian[],long *pXianNum)
- {
- IFeaturePtr ptFea = NULL;
- BSTR bstrName = _com_util::ConvertStringToBSTR(pszClsName);
- HRESULT hr = S_FALSE;
- IRowBufferPtr ptRowBuf = NULL;
- VARIANT var;
- VariantInit(&var);
- hr = m_pFwks->OpenFeatureClass(bstrName,&m_pFcls);
- if (hr == S_FALSE)
- {
- return 0;
- }
- long nFeaNum = 0;
- m_pFcls->FeatureCount(NULL,&nFeaNum);
- Xian tempXian;
- for (int i = 1030;i<nFeaNum;i++)
- {
- memset((void*)&tempXian,0,sizeof(Xian));
- m_pFcls->GetFeature(i,&ptFea);
- ptRowBuf = ptFea;
- ptRowBuf->get_Value(1,&var);
- strcpy(tempXian.szXianName,_com_util::ConvertBSTRToString(var.bstrVal));
- xian[i] = tempXian;
- }
- *pXianNum = nFeaNum;
- if (bstrName!=NULL)
- {
- SysFreeString(bstrName);
- bstrName = NULL;
- }
- return 1;
- }
- //填充县的字段
- long ArcgisOpr::FillXianFld(const char *pszClsName,Xian xian[])
- {
- IFeaturePtr ptFea = NULL;
- BSTR bstrName = _com_util::ConvertStringToBSTR(pszClsName);
- HRESULT hr = S_FALSE;
- IRowBufferPtr ptRowBuf = NULL;
- VARIANT var;
- ARC_IRowPtr ptRow = NULL;
- VariantInit(&var);
- hr = m_pFwks->OpenFeatureClass(bstrName,&m_pFcls);
- if (hr == S_FALSE)
- {
- return 0;
- }
- long nFeaNum = 0;
- m_pFcls->FeatureCount(NULL,&nFeaNum);
- Xian tempXian;
- for (int i = 1030;i<nFeaNum;i++)
- {
- tempXian = xian[i];
- m_pFcls->GetFeature(i,&ptFea);
- ptFea.QueryInterface(IID_ARC_IRow,&ptRow);
- ptRowBuf = ptFea;
- var.bstrVal = _com_util::ConvertStringToBSTR(tempXian.szCityName);
- ptRowBuf->put_Value(2,var);
- UpdateRow(ptRowBuf,ptRow);
- ptRow->Store();
- }
- if (bstrName!=NULL)
- {
- SysFreeString(bstrName);
- bstrName = NULL;
- }
- return 1;
- }
- //将缓冲行更新到要素中
- long ArcgisOpr::UpdateRow(IRowBufferPtr rowBuff,ARC_IRowPtr oldRow)
- {//测试成功
- IFieldsPtr flds;
- IFieldPtr fld;
- long rtn=0,fldNum=0,i=0;
- esriFieldType esiType;
- VARIANT_BOOL flag=0;
- VARIANT eValue;
- HRESULT Isucc = -1;
- if(m_isOpen==0)
- return (0);
- if(rowBuff==NULL || oldRow==NULL)
- return(0);
- Isucc = rowBuff->get_Fields(&flds);
- if(Isucc!=S_OK)
- return -1;
- Isucc = flds->get_FieldCount(&fldNum);
- if(fldNum<1)
- return (0);
- for(i=0;i<fldNum;i++)
- {
- flds->get_Field(i,&fld);
- if(fld==NULL)
- return (0);
- fld->get_Type(&esiType);
- fld->get_Editable(&flag);
- if((esiType!=esriFieldTypeGeometry)&&(-1==flag))//不拷贝OID
- {
- Isucc=rowBuff->get_Value(i,&eValue);
- if(eValue.vt==VT_NULL)
- {
- rtn = 1;
- continue;
- }
- Isucc=oldRow->put_Value(i,eValue);
- // if(FAILED(Isucc))
- // return (0);
- }
- }
- if(SUCCEEDED(Isucc))
- rtn = 1;
- return (rtn);
- }