[python-win32] returning COM objects from a COM server

Mark Hammond mhammond at skippinet.com.au
Thu Aug 4 13:03:40 CEST 2005


> I have several classes that I would like to map to COM objects to
> make them
> accessible from Excel via COM.

You need to use the win32com.server.util.wrap function.  Note that the
object must be an instance of a class that conforms to the usual win32com
server rules - eg, _public_methods_ etc.  No _reg_ attributes are necessary
in this case.

Do a quick grep for wrap in the win32com test dir.

> when I call from Excel the function return_foo() on the object
> Python.Interpreter, I get in the "Python trace collector" an exception:
>
> in _Invoke_ with 1002 1033 3 (4,)
> pythoncom error: Python error invoking COM method.
> exceptions.TypeError: Objects for SAFEARRAYS must be sequences (of
> sequences), or a buffer object.

That particular error is surprising.

> So, is it possible to create new COM objects that map Python classes
> automatically ? Do they need to register ?

They do not need to register if they are only ever returned via wrap()

> BTW, why is not there a COMServer class in win32all that could enjoy
> metaclass facilities and inheritance to ease the work (I agree it
> is already
> quite easy but ...) ? Is it for historical reasons or is there a deep
> explanation ?

Purely historical reasons.  Over the last few years Python has grown a
number of new facilities win32com could take advantage of.  As usual,
contributions welcome ;)

Mark



More information about the Python-win32 mailing list