Question

I'm trying to use with Python a COM server which expose only the IDispatch interface and have neither IDL file nor type library for it. I do have documentation for the different methods and how to use them.

Trying to use the win32com package fails for me, because it seems that when no type information is available win32com fallback to assuming any attribute access is property get or set, never a method invocation.

That is, when I do the following:

  import win32com.client
  c = win32com.client.GetActiveObject(server_progid)
  c.someServerMethod(arg1, arg2)

win32com tries to get the someServerMethod property on the server, ignoring arg1, arg2 completely. Digging into the code it seems because the python is invoking self.__getattr__ which has no arg1, arg2.

I'm looking for a way to solve this:

  • Some syntax to tell win32com I'm actually calling a method ;
  • Some other python COM client which actually implement this behavior ;
  • Other suggestions, except the obvious 'manually convert the documentation into type-library'.

Thanks!

Was it helpful?

Solution

A possible solution (which I'm currently implementing) is to wrap the usage of win32com.client with a proxy which calls _make_method_ for every method invocation, using some logic. Using a code recipe from here I changed to method every property which does not start with get_ or set_ (just an example, any heuristic which allows to tell properties from methods will do).

import new
from types import MethodType

class Proxy(object):

    def __init__(self, target):
        self._target = target

    def __getattr__(self, aname):
        target = self._target
        ### Beginning of special logic ###
        if aname[:4]!='set_' and aname[:4]!='get_':
        ### End of special logic ###
            # Rebind the method to the target.
            return new.instancemethod(f.im_func, self, target.__class__)
        else:
            return f

OTHER TIPS

You should be able to use

c._make_method_("someServerMethod")

to tell win32com to treat it as a method instead of a property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top