Passing by reference value to COM object

I’m trying to make use the python script option in dynamo in civil 3d to call some functions in a com object library written in C so that we can perform some bespoke calculations for creating drawing objects. I have managed to get the code I need to work in Spyder using python 3.6, however when I use this code in dynamo it does not work.

The com object loads fine and te function that I’m calling is as follows:

short GetNumber(short iStageIndex, short* sNumVal)

The return value of which indicates whether the call was successful or not and sNumVal is the number I’m interested in.

In Spyder (python 3.6) and the ref params are returned in a tuple such that it needs to be called as follows:

ret = GetNumber(0);
success = ret[0]
sNumVal = ret[1]

In Dynamo the above doesn’t appear to work anymore. The above call results in the following error:

StandardError: Error while invoking GetNumber

No tuple is returned so to get rid of this error I have to pass something in to the second parameter:

sNumVal = 0
ret = GetNumber(0, sNumVal)

trouble is sNumVal does not get set - do I need to do something to pass it by reference? I’ve tried googling this at length but have so far not found the answer.

Tip: Use Python 2.7 for writing your code in an external editor. Even better just use Iron Python as the editor. This will keep things from falling out of place like this.

Can you write a small sample code which reproduces your issue and post that?

I can’t really write a sample as you’d need to have the COM object. The question then is how to pass variables by reference using python 2.7

You could certainly write different example that effectively passes a similar set of data. Yes it would take time to do this, but it would give a reproducible solution which could be more readily solved by others in the community. Until then we are somewhat in the dark here based on 5 lines of code out of what appears to be a much longer setup.

Short of a reproducible situation of lesser complexity, you could share the full code base, but that seems a bit more aggressive.

its not really much more than the above. I have a com object (lucky me!) written in C with the above function signature

short GetNumber(short iStageIndex, short* sNumVal)

. I need to pass an integer to it, the function expects a short * and I can’t work out how to pass that. it works nicely in python 3.6 but not in 2.7.

This is the code I have:

t = System.Type.GetTypeFromProgID("MyComObject")
o = System.Activator.CreateInstance(t)

i = 0
o.GetNumber(0,i) <== how to pass reference here

I’ve been trying to make use of the ctypes library but that hasn’t helped so far.

https://ironpython.net/documentation/dotnet/dotnet.html#id52
search for ref and out parameters

1 Like