I have been experimenting with Python's comtypes module to access ArcObjects. We are still using ArcGIS 10.0 as we are in the process of upgrading our ArcGIS for Server to version 10.2.2. I am currently running ArcGIS desktop version 10.0 and have a VM with 10.2 installed for testing.

Anyways, the following from the Snippets module works to access an open mxd in version 10.0, but fails in 10.2:

def GetApp(app="ArcMap"):
    import arcpy
    """app must be 'ArcMap' (default) or 'ArcCatalog'\n\
    Execute GetDesktopModules() first"""
    if not (app == "ArcMap" or app == "ArcCatalog"):
        print "app must be 'ArcMap' or 'ArcCatalog'"
        return None
    import comtypes.gen.esriFramework as esriFramework
    import comtypes.gen.esriArcMapUI as esriArcMapUI
    import comtypes.gen.esriCatalogUI as esriCatalogUI
    pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT)
    iCount = pAppROT.Count
    if iCount == 0:
        return None
    for i in range(iCount):
        pApp = pAppROT.Item(i)
        print pApp
        if app == "ArcCatalog":
            if CType(pApp, esriCatalogUI.IGxApplication):
                return pApp
        if CType(pApp, esriArcMapUI.IMxApplication):
            return pApp
    return None

In 10.2, I get the following error message:

Traceback (most recent call last):
  File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\bmi_arcobjects\_snippets.py", line 264, in <module>
  File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\bmi_arcobjects\_snippets.py", line 86, in GetApp
    pAppROT = NewObj(esriFramework.AppROT, esriFramework.IAppROT)
  File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\bmi_arcobjects\_snippets.py", line 128, in NewObj
    ptr = CreateObject(COMClass, interface=COMInterface)
  File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\comtypes\client\__init__.py", line 235, in CreateObject
    obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
  File "\\arcserver1\GIS\_Resources\ESRI\Python\BMI_Library\comtypes\__init__.py", line 1145, in CoCreateInstance
    _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
  File "_ctypes/callproc.c", line 936, in GetResult
WindowsError: [Error -2147221164] Class not registered

I thought this may be an issue with versions since in the gen folder all wrappers ended with "_10_0". Everyone's sys.path has access to the same comtypes which is on our server, so I blew away all the modules in the gen folder and called the following function to load all esri modules to ensure I was getting the most up to date wrappers:

def load_all():
    '''loads all object libraries'''
    from comtypes.client import GetModule
    mods = glob.glob(os.path.join(GetLibPath(), '*.olb'))
    for mod in mods:

This loaded everything, and I still get that same error when running at 10.2. I saw this post with a similar problem but the solution did not work for me: Python, comtypes and ArcObjects: Error creating AppROT object

Has anyone else had this problem? If so, how did you alter the GetApp() function. Thanks.

  • 1
    The comptypes are slightly different between 10.0, 10.1 and 10.2. Have you made sure that you are using the snippets file from here: pierssen.com/arcgis10/upload/python/snippets102.py and have made the changes listed at the top, on the 10.2 machine? Jul 31, 2014 at 1:07
  • Yes, the GetApp() function is exactly the same as the script in that link you provided and POINTER(BSTR): VT_BYREF|VT_BSTR is in the dictionary in my automation.py module. I am not sure what is causing the problem.
    – crmackey
    Jul 31, 2014 at 2:02
  • 1
    Related: ArcObjects + comtypes at 10.1 -- for me, nothing changed between 10.1 and 10.2.
    – blah238
    Jul 31, 2014 at 7:37
  • Unfortunately none of the suggestions in that thread resolved the issue. I have not seen where anyone else has received the same Windows Error I am getting. It doesn't tell me what class is not registered either. I'm not sure what is going on here.
    – crmackey
    Jul 31, 2014 at 14:03
  • 1
    I just wanted to provide an update...I am running 10.2 on a VM using VMWARE and have 10.0 installed on my regular machine. A coworker who is running 10.2.1 as his primary was able to use my code and he did not have any errors. This is either an issue with my VM or the install on my VM.
    – crmackey
    Jul 31, 2014 at 21:38


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.