[python-win32] Importing an array from COM into Python

Mark Hammond mhammond at skippinet.com.au
Wed Apr 5 02:17:03 CEST 2006


> I am trying to get the arrays out of this function (from the IDL):
>
> [id(175), helpstring("method CreateFooBarArray")] HRESULT
> CreateFooBarArray([out]DOUBLE** FooArray, [out] DOUBLE** BarArray,
> [out,retval] ULONG* SizeOfFooBarArrays);
>
> The two arrays (Foo and Bar) are created with malloc and filled with
> useful data inside the COM object.
>
> I am attempting to access them with the following Python code.
>
> size = o.CreateFooBarArray(fooArray, barArray)

Note that Python is calling the function via IDispatch, and not natively
using the vtable.  Hence the arrays are passed as Variants and SAFEARRAYs,
not directly.

Also note that given the above signature, the Python call should look like:

size, fooArray, barArray = CreateFooBarArray()

As all params are [out].

> This does not work.  I get an error in \client\__init__.py at line 447 in
> _ApplyTypes_.  It says "float() argument must be a string or number"
> Other COM functions with simple types work fine.  I ran makepy.py over the
> COM library and I'm using early-bound automation if that makes any
> difference.

That is strange, but would be related to the args you passed.

> So I have two questions:
>
> 1) The COM object is brand new and created in-house so I can have it
> changed to my liking.  What's the best way to pass a large array
> (thousands of elements) of doubles out of a COM object into Python?  I
> have Mark Hammond's book, which is very helpful, but passing arrays
> between languages doesn't seem to be covered.

That should work fine.

> 2) If the COM object handles the memory allocation, will the arrays be
> properly garbage collected?

As above, Python will never directly make the call so memory management is
handled by the IDispatch implementation your object provides.  I'm actually
surprised that the IDL allows that as an automation interface!

Also check out the com\TestSources directory in the source archive or in
CVS - there we have a C++ implemented COM object that exchanges various
types of data with Python (and ditto one for VB/Python)

Mark



More information about the Python-win32 mailing list