1

I am writing a python script to control a Com object. The official documentation is in C#, VB and C. the documentation (in VB) is as follows:

Public Function GetDATA { 
ByVal vecRecords() As DATAType,
ByRef Time As String, 
optional ByVal filter1 As String, 
optional ByVal kind2 As Kind = KindAll, 
optional ByVal type1 As Types = TypeAll 
) As Long 

As far as i can tell, the 1st argument is the return value and is returned as normal in python. Last three arguments are enum types (translated in to numbers) but are optional in any case. I am left wit the question what argument do I pass in "ByRef Time As String"?

my code is:

>>> from win32com.client import Dispatch
>>> Obj = Dispatch("Service.Foo")
>>> Obj.Function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)

I also ran:

>>> Obj.Function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
     self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
 TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a bufferobject.

and:

>>> a= ("00000000","00000000",-1,-1)
>>> Obj.Function(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
     self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
MemoryError: CreatingSafeArray

The retvalue is not too big, so i really do not understand the last one.

system: windows 7 64bit. python 2.7.2 32 bit (running from cmd started as administrator, the COM object needs it.)

Is there a simple way to discover what the function wants?

Thanks in advance Marbs.

[Edit] I give you a working C# Code Snippet:

System.Array tempArray;
string lTime = "00000000";
Res = Obj.getDATA(out tempArray, ref lTime, "0", Obj.type1, Obj.type2);  

Where Obj.type1 and Obj.type2 are enums form the Obj it self. They both translate in to -1 (an int).

4
  • Try to explore "ServiceFoo" with com browser (i.e. combrowse.py usually found in python_inst\Lib\site-packages\win32com\client directory).
    – barti_ddu
    Feb 17, 2012 at 14:25
  • @barti_ddu, thanks for the grate tip. Any place the argument is something like "Pointer SafeArray User Defined (Flags=2)" (i.e. flags are not equal to "1") I have a problem. Any ideas?
    – user850498
    Feb 19, 2012 at 8:58
  • imho, it looks like SafeArray constructor takes 2 arguments. Have you tried just creating an emty list and passing it as the first argument to Obj.Function()?
    – barti_ddu
    Feb 20, 2012 at 12:39
  • @barti_ddu, I think I know what I am missing here. lTime is passed to the function as a pointer(in C/C++) or a ref (in C#). Obj.getDATA is trying to access the memory "00000000" and being shot down. I cant seem to find a way to create a real pointer to memory in python....
    – user850498
    Feb 21, 2012 at 20:18

2 Answers 2

1

If you are having trouble with something hard, try doing something easy that might give you some insights into the hard problem.

It sounds like you are trying to call a COM API that you are not entirely familiar with, from Python, and that the API makes use of datatypes that are more complex than simple strings and integers. Why not try writing a simple client in a .Net language like C# or VB, and get an idea of what values the library expects? There might even be some examples available in those languages that you could just copy and run. Then from a working example, you could convert that to Python.

Another tack you might take could be to use IronPython, which is already .Net-aware/friendly, and might make your development simpler.

Lastly, another Pythonesque language that is .Net-friendly is Boo, which I have used in the past to build some simple utilities for clients, since you can write code very similar to Python, but which builds to free-standing .DLLs or .EXEs. Although that project has not had any apparent activity in over a year, the language was very useful to me when I was developing some SOAP client and server code, a few years ago.

2
  • Thanks for the idea. I edited in to my question a C# Code Snippet. Do you have any insights on how to convert to python? i will read up on IronPython, but I need to do this in pure python.
    – user850498
    Feb 19, 2012 at 9:26
  • Ok, good, you have a C# snippet. How about calling a simple COM object from Python (using win32com since you have to stay in Python)? Call a function that takes an integer and returns the square of that integer. Then start working up to objects and arrays and enums and the rest.
    – PaulMcG
    Feb 21, 2012 at 1:39
0

When writing in VB, and using the ByRef switch like in the decryption ByRef Time As String, from my question, the function changes the actual variable (like a pointer in c++).

This COM object tried changing some memory it was not allowed to change. That is why I got a memory error when running it in python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.