Home/ Reference/ Connection & Init

win32com.client.gencache.EnsureDispatch(progid)

Once the constant cache exists you can write cdrTextShape instead of 6. Generating the CorelDRAW type library fails now and then (permissions, version conflicts); wh

Signature

win32com.client.gencache.EnsureDispatch(progid)

Notes

Once the constant cache exists you can write cdrTextShape instead of 6. Generating the CorelDRAW type library fails now and then (permissions, version conflicts); when it does, fall back to dynamic.Dispatch and use the numeric constants.

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
import win32com.client

# with the type library cache you can write cdrTextShape instead of numbers
try:
    app = win32com.client.gencache.EnsureDispatch("CorelDRAW.Application.21")
    print("Type library cache built, cdrXxx constants are available")
except Exception as e:
    # cache generation often fails (permissions, version clash); fall back to late binding
    print("Cache failed, falling back to late binding: ", e)
    app = win32com.client.dynamic.Dispatch("CorelDRAW.Application.21")

doc = app.ActiveDocument
print("Pages: ", doc.Pages.Count)

The real difference: whether the type library is read

Dispatch is late binding through IDispatch: method names and arguments are resolved at call time. EnsureDispatch first locates the component's type library, reads the interfaces, constants, and parameter types out of it, generates a Python module into a local cache, and returns an object carrying that static information.

Three concrete gains that late binding cannot give you:

Cost one: the first call is slow

The first EnsureDispatch reads the type library, generates the module, and writes the cache — hundreds of milliseconds to several seconds. After that it is cached and fast. Irrelevant on the command line, very noticeable in a script invoked every minute. That kind of workload is better served by Dispatch.

Cost two: the cache goes stale, and the errors point elsewhere

The cache lives roughly at %LOCALAPPDATA%\Temp\gen_py\<py version>\, or under site-packages\win32com\gen_py, depending on installation and permissions. Directory names combine CLSID, lcid, and version.

The classic failure: CorelDRAW is upgraded, the old cache directory is still there, and the ProgID stem has not changed, so Python keeps loading the stale module. The symptom is not "type library not found", it is:

The fix is direct: stop every Python process, delete the matching directory under gen_py, and let it regenerate. Put that line in your upgrade checklist; it costs far less than diagnosing it after the fact.

Cost three: concurrent first-generation races

Several processes calling EnsureDispatch on the same ProgID for the first time all write into the same directory at once, producing a truncated module or an ImportError. This shows up in CI, in container startup, and in multiprocessing batch runs.

Avoid it by pre-warming: run `python -c "import win32com.client as w; w.gencache.EnsureDispatch('CorelDRAW.Application')"` during image build or first deployment so the cache is baked in and nothing needs writing at runtime. The pre-warm has to happen with the same interpreter bitness and the same user context as the real run.

One behaviour difference that confuses people

Under gencache, indexed collection access changes shape. With late binding doc.Pages(1) works; under gencache you more often need doc.Pages.Item(1), and some properties become methods. This is not a bug — it follows from the type library declaring the member as a parameterised propget.

When you hit TypeError: 'method' object is not callable, or the mirror-image error, first establish which binding you are on, then adjust the call.

A complete version

Python
import pythoncom
import win32com.client
from win32com.client import gencache

def connect_typed(progid="CorelDRAW.Application"):
    pythoncom.CoInitialize()
    try:
        app = gencache.EnsureDispatch(progid)
    except Exception as e:
        # stale or half-written cache is the usual cause
        print("EnsureDispatch failed:", e)
        print("If it keeps failing, delete the matching dir under %LOCALAPPDATA%\\Temp\\gen_py")
        app = win32com.client.Dispatch(progid)

    app.Visible = True
    print("version:", app.Version)
    return app

app = connect_typed()

How to choose

About this section: Connection & Init

Connection & Init — connecting to CorelDRAW over COM.

Other entries in this section