[python-win32] A question about COM in general
timr at probo.com
Tue Oct 19 18:41:32 CEST 2010
Eileen Wei wrote:
> I am a software tester and I am exploring ways to exposing APIs of
> Windows applications that’s developed in C++ and MFC to Python for
> testing purpose. I just learned about COM last week and I am wondering
> if it’s the best way to do it? I have also heard about other options
> like SWIG (Simplified Wrapper and Interface Generator) and Boost Python.
> I am aware that the question might be a little off topic, but I am
> thinking users in this mail group might have done similar exploration
> and probably can give me some advice in terms of ease of use, maturity
> of the technology etc.
It's an interesting question. Note that what follows are my personal
opinions, not the "collected knowledge of the ages".
The biggest part of the answer is dependent on what your application
expects. If an application expects to be controlled programmatically,
then it has to take explicit steps to allow itself to be controlled.
You can't remotely control an arbitrary application unless it was
designed to be controlled.
COM is one of the more common ways to allow this kind of control. That,
for example, is how the Office applications are controlled. If your
application already exposes a COM interface, and that's the way
programmers are expected to talk to it, then that's a perfectly sensible
way to go. Python's COM support is pretty good, and it's easy to
If your application has an API that is implemented in a DLL, and that
DLL is not COM, then it is probably not worth the trouble to add COM to
it. Instead, you can use the "ctypes" module that is now built-in to
Python. "ctypes" is a "foreign function library", which means that it
allows Python programs to talk to libraries that were not designed for
Python. You can call virtually any arbitrary DLL function from Python
with ctypes. If the data types are complicated, the code is a little
bit convoluted, but it is usually straightforward and easy to read.
For me, SWIG only seemed to made sense for interfaces that I intended to
publish. SWIG is quite powerful, but it has (in my opinion) a very
steep learning curve. You can do anything, but there are sharp edges.
I have only experimented a little bit with Boost.Python. It does seem
to be very cool, but you have to decide what your model is going to be.
Boost.Python lets you build Python extension modules easily. If you
want your application interface to look like a Python extension module,
then it might make good sense.
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32