今天一边看帮助文档一边尝试导入ArcGIS库文件,总是不成功,出现很多错误,后来细心的一条一条导入,纠正错误,最后编译成功了,所以就在这分享下经验。导入ArcGIS库的时候是要有引入许多参数才能编译成功的,比如esrisystem这个库文件,代码如下:
#pragma warning(push)#pragma
warning(disable : 4192) /* Ignore warnings for types that are duplicated in win32 header files */
#pragma
warning(disable : 4146) /* Ignore warnings for use of minus on unsigned types */
#import
"/Program Files/ArcGIS/com/esriSystem.olb" /* Type library to generate C++ wrappers */
/
raw_interfaces_only, /* Don't add raw_ to method names */
/
raw_native_types, /* Don't map to DTC smart types */
/
no_namespace, /* Don't wrap with C++ name space */
/
named_guids, /* Named guids and declspecs */
/
exclude("OLE_COLOR", "OLE_HANDLE", "VARTYPE") /* Exclude conflicting types */
#pragma
warning(pop)
在这里出现你会发现exclude("OLE_COLOR", "OLE_HANDLE", "VARTYPE") 这代码,注释上写的是排除冲突类型,为什么会出现这样的情况?因为在Windows中定义了"OLE_HANDLE"和"OLE_COLOR",为unsigned long类型,而在ArcGIS中定义为long类型(貌似原因是在vb中不支持unsigned类型),你可以在debug目录下的打开esrisystem.tlh查找关于"OLE_HANDLE"和"OLE_COLOR"的定义确实是long类型。
typedef long OLE_COLOR;
typedef long OLE_HANDLE;
把以上放在stdafx.h编译没有任何问题,但是如果我按照帮助文档把导入多个库文件的代码放进去,就出现很多错误,这是什么原因内?
#pragma warning(push)#pragma
warning(disable : 4192) /* Ignore warnings for types that are duplicated in win32 header files */
#pragma
warning(disable : 4146) /* Ignore warnings for use of minus on unsigned types */
#import
"/Program Files/ArcGIS/com/esriSystem.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids, exclude("OLE_COLOR", "OLE_HANDLE", "VARTYPE")#import
"/Program Files/ArcGIS/com/esriSystemUI.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids#import
"/Program Files/ArcGIS/com/esriGeometry.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids#import
"/Program Files/ArcGIS/com/esriDisplay.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids#import
"/Program Files/ArcGIS/com/esriOutput.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids#import
"/Program Files/ArcGIS/com/esriGeoDatabase.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids#import
"/Program Files/ArcGIS/com/esriCarto.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids#pragma
warning(pop)
这要还是上面所说的原因:类型冲突!如果你在导入esrisystemui库你会发现出现"ICommand"和"IProgressDialog"这两个"Struct"类型重定义,那么在导入esrisystemui库代码后面加上exclude("ICommand","IProgressDialog")。按照这样的方法,在导入其他库文件时候问题就可以一次解决。以上错误代码这样修改就没有任何问题了:
#pragma warning(push)
#pragma warning(disable : 4192) /* Ignore warnings for types that are duplicated in win32 header files */
#pragma warning(disable : 4146) /* Ignore warnings for use of minus on unsigned types */
#import "D:/Program Files/ArcGIS/com/esriSystem.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids, exclude("OLE_COLOR", "OLE_HANDLE", "VARTYPE")
#import "D:/Program Files/ArcGIS/com/esriSystemUI.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids,exclude("ICommand","IProgressDialog")
#import "D:/Program Files/ArcGIS/com/esriGeometry.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "D:/Program Files/ArcGIS/com/esriDisplay.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "D:/Program Files/ArcGIS/com/esriOutput.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "D:/Program Files/ArcGIS/com/esriGeoDatabase.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids,exclude("IRelationship","IRow","ICursor")
#import "D:/Program Files/ArcGIS/com/esriCarto.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids,exclude("UINT_PTR","ITableDefinition")
#pragma warning(pop)