[python-win32] Passing Arrays by Reference

Jens B. Jorgensen jens.jorgensen@tallan.com
Thu, 07 Nov 2002 12:36:50 -0600


And that makes it work?! Whoa. Weird.

Erignac, Charles A wrote:

>I used Jens's example below to check that I could pass a python array by
>reference to a vb method that modifies it. I also checked that the
>CATIA.Position.GetComponents call that failed to modify the array passed by
>reference in python actually works from vb.
>So as a quick fix I created a vb proxy that places the call on behalf of
>python. It is not elegant but it works. This is what the proxy looks like:
>
>Public Sub GetComponents(o As Object, ByRef a() As Variant)
>  o.GetComponents a
>End Sub
>
>Charles
>
>-----Original Message-----
>From: Jens B. Jorgensen [mailto:jens.jorgensen@tallan.com]
>Sent: Wednesday, November 06, 2002 1:04 PM
>To: Erignac, Charles A
>Cc: 'python-win32@python.org'
>Subject: Re: [python-win32] Passing Arrays by Reference
>
>
>Things seem to work fine for me. I created a test ActiveX DLL in VB with 
>a single object. Here's the code for it:
>
>Public Sub DoArrayThing(larr() As Long)
>  Dim i As Long
>  For i = LBound(larr) To UBound(larr)
>    larr(i) = larr(i) + 1
>  Next i
>
>End Sub
>
>Then in python here's what I get:
>
>ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on
>Python 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32
>Type "help", "copyright", "credits" or "license" for more information.
> >>> from win32com.client import Dispatch
> >>> o = Dispatch('TestArray.ArrManip')
> >>> o.DoArrayThing(range(0, 4))
>(1, 2, 3, 4)
> >>> a = _
> >>> o.DoArrayThing(a)
>(2, 3, 4, 5)
> >>>
>
>So it looks like everything python is doing is all fine and good with 
>the arrays.
>
>Erignac, Charles A wrote:
>
>  
>
>>I am calling a method on a COM object that fills in an array passed by
>>reference. The array must be initialized to a given size otherwise the call
>>will fail. Currently I am able to complete the call and the array returned
>>by the method is the one I passed as a parameter but unfortunately the
>>    
>>
>value
>  
>
>>of the array does not change and it should. For your information the COM
>>object I work with is a Position interface maintained by the CATIA COM
>>server. The method is Sub GetComponents(CATSafeArrayVariant array). I
>>understand that the CATSafeArrayVariant type is equivalent to an array of
>>variants.
>>The python call looks like this:
>>	a = range(0,12)
>>	b = myPosition.GetComponents(a)
>>	print a # outputs (0,1,2,3,4,5,6,7,8,9,10,11) instead of the actual
>>components
>>	print b # same output
>>
>>The call is an early binding thanks to pygen.
>>I would appreciate any help regarding this problem. I would like at least
>>    
>>
>to
>  
>
>>know where to look in the win32com code to understand what is going wrong.
>>Best regards,
>>
>>Charles
>>
>>
>>_______________________________________________
>>Python-win32 mailing list
>>Python-win32@python.org
>>http://mail.python.org/mailman/listinfo/python-win32
>> 
>>
>>    
>>
>
>  
>

-- 
Jens B. Jorgensen
jens.jorgensen@tallan.com

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"