【问题标题】:Python, comtypes and ArcObjects: Error creating AppROT objectPython、comtypes 和 ArcObjects:创建 AppROT 对象时出错
【发布时间】:2011-06-06 21:04:14
【问题描述】:

我正在 Python 2.6.5 和 ArcGIS 10 SP1 下试验 comtypes 和 ArcObjects。我正在使用纯 Python 方法来包装 this answer 中描述的 ArcObjects OLB,但在 comtypes.CoCreateInstance 方法中出现错误。

这是我正在运行的代码:

def WrapModules():
    #force wrapping of all ArcObjects libraries (OLBs)
    import os
    import comtypes.client
    # change com_dir to whatever it is for you
    com_dir = r'C:\Program Files\ArcGIS\Desktop10.0\com'
    coms = [os.path.join(com_dir, x) for x in os.listdir(com_dir) if os.path.splitext(x)[1].upper() == '.OLB']
    map(comtypes.client.GetModule, coms)

def GetApp():
    """Get a hook into the current session of ArcMap"""
    from comtypes.gen import esriFramework
    pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT)
    if pAppROT is not None:
        iCount = pAppROT.Count
        if iCount == 0:
            print 'No ArcGIS application currently running.  Terminating ...'
            return None
        for i in range(iCount):
            pApp = pAppROT.Item(i)  #returns IApplication on AppRef
            if pApp.Name == 'ArcMap':
                print "ArcMap found"
                return pApp
        print 'No ArcMap session is running at this time.'
    print "No AppROT found"
    return None

def NewObj(MyClass, MyInterface):
    """Creates a new comtypes POINTER object where\n\
    MyClass is the class to be instantiated,\n\
    MyInterface is the interface to be assigned"""
    from comtypes.client import CreateObject
    import traceback
    try:
        ptr = CreateObject(MyClass, interface=MyInterface)
        return ptr
    except:
        print traceback.format_exc()
        return None

if __name__ == "__main__":
    WrapModules()
    pApp = GetApp()
    if pApp is not None:
        print "HWND: %d" % pApp.hWnd
    else:
        print "No ArcGIS application found!"

这是脚本的输出:

Traceback(最近一次调用最后一次):
  NewObj 中的文件“C:\temp\ComHelpers.py”,第 35 行
    ptr = CreateObject(MyClass, interface=MyInterface)
  文件“C:\Python26\ArcGIS10.0\lib\site-packages\comtypes\client\__init__.py”,第 235 行,在 CreateObject
    obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
  CoCreateInstance 中的文件“C:\Python26\ArcGIS10.0\lib\site-packages\comtypes\__init__.py”,第 1145 行
    _ole32.CoCreateInstance(byref(clsid),punkouter,clsctx,byref(iid),byref(p))
  GetResult 中的文件“_ctypes/callproc.c”,第 925 行
WindowsError:[错误 -2147221231] ClassFactory 无法提供请求的类

未找到 AppROT
未找到 ArcGIS 应用程序!

感谢您提供的任何见解!

【问题讨论】:

    标签: python arcobjects comtypes


    【解决方案1】:

    为了完整起见,this solution 由 Jason Scheirer 在 GIS Stack Exchange 上发布:

    首先导入 arcpy,您没有进行任何许可检查或按原样设置 ArcObjects 10.0 运行时,因此它不会找到 CoClass。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多