Rafael Yengibaryan wrote:
>
> As I understand, ctypes or comtypes make it possible to call functions
> or access data from a library written in another language, don' t they?
> Now, is it right that I should use some library which includes
> COM-objects (or COM-interfaces) and access it using comtypes? Don' t I
> need additional C++ coding?

ctypes makes it possible to call arbitrary DLLs (which can be written in
any language) from Python.  That's unrelated to COM.

In order to use a COM interface, you have to know what functions it
includes (and in what order), and what parameters each function
accepts.  There are several ways to get that information.  Some COM
objects implement an interface called IDispatch.  That lets you ask the
object things like "what functions do you support?", and "what
parameters does that function take?"  I don't need any information other
than the object's address.  That is called "late binding".  The PyWin32
win32com supports those generically -- no special processing is needed. 
Word and Excel (and most very large COM servers) support IDispatch.

Many COM object don't implement IDispatch, because it is inefficient. 
In that case, you have to know what each function is for, what order
they appear in the function table, and what the parameters are.  That's
called "early binding", because I have to have all that information
before I get the object.  If ALL you have is the object, with no other
information, you can't really use it, because you can't guess what the
functions are.  So, COM objects intended for general use also ship a
"type library", with a .tlb extension.  The type library contains a
compiled description of the functions and parameters for a particular
interface.  If you have a type library, you can use "makepy" in the
win32com distribution to build a Python wrapper that will let you use
the object.

However, type libraries only work if the objects follow certain rules. 
Some people write COM objects as if they were just C++ objects, without
worrying about the COM rules about parameter types and return values. 
For those objects, makepy can't always create a wrapper.  In that case,
you have to fall back to comtypes.  Comtypes works at a lower level, and
requires you to specify exactly what the functions are and what the
parameters are.  Comtypes, like ctypes, is fabulous magic, but it's
really hard to get it right to begin with.

Here's a chapter from Mark Hammond's incredibly valuable book "Python
Programming on Win32" that goes into some detail about this:
     http://oreilly.com/catalog/pythonwin32/chapter/ch12.html


> Can you please describe the steps of writing an ordinary COM-extension
> in python. Do I have to use C/C++ or is it possible to only use python.

I guess that depends on your definition of "ordinary".  You do need a
certain amount of C++ glue, but all that you need is provided by PyWin32
and comtypes.  You don't have to write it.  That book chapter above
includes a simple COM server in 12 lines of Python.

-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to