《C++CLI调用ArcObjects》由会员分享,可在线阅读,更多相关《C++CLI调用ArcObjects(17页珍藏版)》请在金锄头文库上搜索。
1、1 新建一个空的工程命名为 AoWrapperDemo2 添加一个新项目,命名为 AoWrapper右键项目-属性,添加 ESRI.ArcGIS 相关引用将配置类型改为动态链接库添加一个 c+文件,命名为 GeometryWrapper由于该类功能简单,没必要添加头文件using namespace System;using namespace System:Diagnostics;using namespace ESRI:ArcGIS:Geometry;using namespace ESRI:ArcGIS:esriSystem;using namespace ESRI:ArcGIS;nam
2、espace AoWrapperpublic ref class GeometryWrapperpublic:GeometryWrapper()ESRI:ArcGIS:RuntimeManager:Bind(ProductCode:Engine);IAoInitialize pAoInitialize=gcnew AoInitializeClass();pAoInitialize-Initialize(esriLicenseProductCode:esriLicenseProductCodeEngineGeoDB);GeometryWrapper()public:int line_test()
3、IPoint ipPt1 =gcnew PointClass();IPoint ipPt2 =gcnew PointClass();ipPt1-PutCoords(1, 2);ipPt2-PutCoords(2, 3);ILine ipLine=gcnew LineClass();Stopwatch watch=gcnew Stopwatch();watch-Start();for(long i=0;iPutCoords(ipPt1,ipPt2);/Console:WriteLine(i);watch-Stop();Console:WriteLine(watch-ElapsedMillisec
4、onds);return 0;完成 C+/CLI 对 ArcObjects 的调用封装类库3 添加一个新项目,命名为 NAoHelper,利用本地 c+直接操作 AO修改头文件 stdafx.h,导入 ArcObjects 相关库/ stdafx.h : include file for standard system include files,/ or project specific include files that are used frequently, but/ are changed infrequently/#pragma once#include “targetver.h
5、“#define WIN32_LEAN_AND_MEAN / Exclude rarely-used stuff from Windows headers/ Windows Header Files:#include / TODO: reference additional headers your program requires here#pragma warning(push)#pragma warning(disable : 4192) /* Ignore warnings for types that are duplicated in win32 header files */#p
6、ragma warning(disable : 4146) /* Ignore warnings for use of minus on unsigned types */#import “C:Program Files (x86)ArcGISDesktop10.1comesriSystem.olb“ raw_interfaces_only, raw_native_types, no_namespace, named_guids, exclude(“OLE_COLOR“, “OLE_HANDLE“, “VARTYPE“)#import “C:Program Files (x86)ArcGISD
7、esktop10.1comesriGeometry.olb“ raw_interfaces_only, raw_native_types, no_namespace, named_guids,exclude(“OLE_COLOR“)/ Load the ArcGISVersion library./ This code is commonly placed in the StdAfx.h header file.#import “libid:6FCCEDE0-179D-4D12-B586-58C88D26CA78“ raw_interfaces_only,no_implementation添加
8、一个头文件 NAoHelper.h修改后的头文件如下:#ifndef AOHELPER_H_#define AOHELPER_H_#ifdef NAOHELPER_EXPORTS#define DAPI _declspec(dllexport)#else#define DAPI _declspec(dllimport)#endif/导出这个类class DAPI AoHelperpublic:AoHelper();AoHelper();int line_test();private:int InitializeAo();#endif打开 NAoHelper.cpp,修改后如下:/ NAoHel
9、per.cpp : Defines the exported functions for the DLL application./#include “stdafx.h“#include “NAoHelper.h“AoHelper:AoHelper():CoInitialize(NULL);/不初始化也可以/InitializeAo();AoHelper:AoHelper():CoUninitialize();int AoHelper:InitializeAo()ArcGISVersionLib:IArcGISVersionPtr ipVer(_uuidof(ArcGISVersionLib:
10、VersionManager);VARIANT_BOOL succeeded;if (FAILED(ipVer-LoadVersion(ArcGISVersionLib:esriArcGISDesktop , L“10.1“,IAoInitializePtr m_AoInit;/(CLSID_AoInitialize);m_AoInit.CreateInstance (CLSID_AoInitialize);esriLicenseStatus ls;HRESULT h= m_AoInit-Initialize(esriLicenseProductCode:esriLicenseProductC
11、odeEngineGeoDB ,return 0;int AoHelper:line_test()IPointPtr ipPt1(CLSID_Point);IPointPtr ipPt2(CLSID_Point);ipPt1-PutCoords(1, 2);ipPt2-PutCoords(2, 3);ILinePtr ipLine(CLSID_Line);for(long i = 0; i PutCoords(ipPt1,ipPt2);return 0;编译,完成本地 c+调用 AO 封装类库4 添加新项目,命名为 NAoWrapper,利用 C+/CLI 对 NAoHelper 进行封装的库
12、,以便于 C# 调用编辑包含文件(头文件)目录修改头文件 NAoHelper.h/ NAoWrapper.h#pragma once#include “NAoHelper.h“using namespace System;namespace NAoWrapper public ref class RefAoWrapper/ TODO: Add your methods for this class here.private:AoHelper *ao;public:int line_test();RefAoWrapper();RefAoWrapper();修改 NAoWrapper.cpp/ T
13、his is the main DLL file.#include “stdafx.h“#include “NAoWrapper.h“using namespace System:Diagnostics;namespace NAoWrapper RefAoWrapper:RefAoWrapper()ao=new AoHelper();RefAoWrapper:RefAoWrapper()delete ao;int RefAoWrapper :line_test()Stopwatch watch=gcnew Stopwatch();watch-Start();ao-line_test();wat
14、ch-Stop();Console:WriteLine(watch-ElapsedMilliseconds);return 0;5 添加新项目,命名为 CSharpAoHeleper,C#直接调用 ArcObjects添加 ESRI.ArcGIS.相关引用将 Class1.cs 重命名为 CSharpAoHeleper,代码如下using System;using System.Collections.Generic;using System.Linq;using System.Text;using ESRI.ArcGIS.Geometry;using System.Diagnostics;n
15、amespace CSharpAoHeleperpublic class CSharpAoHeleperpublic int line_test()IPoint ipPt1 = new PointClass();IPoint ipPt2 = new PointClass();ipPt1.PutCoords(1, 2);ipPt2.PutCoords(2, 3);ILine ipLine = new LineClass();Stopwatch watch = new Stopwatch();watch.Start();for (long i = 0; i / The main entry poi
16、nt for the application./ STAThreadstatic void Main()if (!RuntimeManager.Bind(ProductCode.Engine)if (!RuntimeManager.Bind(ProductCode.Desktop)Console.WriteLine(“Unable to bind to ArcGIS runtime. Application will be shut down.“);return;IAoInitialize pAoInitialize = new AoInitializeClass();esriLicenseStatus licenseStatus = pAoInitialize.Ini