wjh wrote: > > Dears, > > I have a problem with passing out type parameter to win32com function. > In C# envirement one of the COM’s function prototype is: > > int ActEasyIF.ReadDeviceBlock(string szDevice, int Isize, > out int IpIdata); > > when calling the function ,it passes a first element of a int array to > the third parameter, like this: > > ACTMULTILib.ActEasyIF aa = new ACTMULTILib.ActEasyIF(); > > aa.ReadDeviceBlock(“D0”, 10, out da[0])) > > The function read 10 integer value from device’s register D0 to D9,and > write those integer value to the array da, from 0 to 10 . The function > run success in C#. >
If this actually works, it's an accident, because your declaration is completely wrong. The declaration "out int IpData" says you will be returning exactly ONE int. It doesn't allow the called function to return an array of ints, and C# makes no guarantees that consecutive elements actually follow each other in memory. This should have been declared as something like "ref int[] IpData". What does the function declaration look like in C++ or in the original IDL? > I call this COM function by win32comclient in python > > >>> from win32com.client import Dispatch > > >>> import pythoncom > > >>> parser = Dispatch("ActMulti.ActEasyIF") > > >>> parser.ActLogicalStationNumber = 1 > > >>> parser.Open() > > 0 #Open device success > > >>> da = [0] *10 > > >>> parser.ReadDeviceBlock("D0", 10, da[0]) > > (0, 256) #read data success > You need to remember that Python always does pass by value. What this statement will do is pass the integer value of the first element of "da", literally 0. It does not pass the "address" of the "da" array. For an "out" parameter, you don't usually pass anything at all. So: hr, da = parser.ReadDeviceBlock("DO", 10) I believe that the (0, 256) represents a successful return (0), and the value of the first returned element (256). If your C++ declaration looks like your C# declaration, then this is exactly what I would expect, because you are saying that the function returns one integer. That's what you got here. > After call the function, it returns a tuple,the first element means > read success, second is value read from device’s D0 register, but the > value don’t write to list da . > > I think the problem is the method of calling the third parameter,but > how to pass "out" parameter to win32com, is there anything hint? > The problem is the way you have declared the function. Right now, it is doing exactly what you asked: you said the function has one integer as output, and that's exactly what you got back. If you need this to return an array, then you need to change your IDL so that it returns an array. -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc.
_______________________________________________ python-win32 mailing list python-win32@python.org https://mail.python.org/mailman/listinfo/python-win32