Passing arguments to COM Server

Jeff Winkelman jwinkelman at movaz.com
Thu Apr 4 09:44:00 EST 2002


I am having some difficulty with a COM object I'm trying to use from
Python.

Background:
I have an application that I am trying to call a Python COM Server
from.  One limitation of this application is that it cannot access any
COM Server that doesn't have a type library associated with it.

So, for example, it does not support the equivalent VB code:

set obj = CreateObject("Library.Class")

I have struggled with getting a type library built that represents my
Python COM object, so I have resorted to building a COM DLL in Visual
Basic that is a wrapper around my Python COM Server.  The Visual Basic
DLL is visiable to my front end application, and at a basic level,
this seems to work.

So in summary, the front end app. calls a visual basic DLL that in
turn calls a Python COM Server.

The next step requires that I pass a COM Object from the front end app
all the way down to python.

This, on the surface, seems to work as well.  When I get to my Python
COM Server, I turn the PyIDispatch object into a COM object that's
usable by Python with the following line of code:

obj = win32com.client.Dispatch(obj)

The problem I have is that when I try to call methods of this passed
in COM object, the front end object generates an error stating "Wrong
Number of Positional Parameters".

For example,

obj.SetValNumber("xxxx", 0, 10.0) generates a COM error from the front
end application with the above description.

I realize that this is an error being generated by the front end app,
however, I am not clear why the correct number of parameters is not
making it to the object.

One concern that I have is if I print out the object in python, I get
the following:

The object is <COMObject <unknown>>

It seems that Python knows this is a COM object, but doesn't know what
kind.

I have run makepy on the Type Library for the front end application,
but that didn't seem to make any differnce.

I am positive that I am attempting to pass the right arguments to the
COM object, however, something is happening along to create this
problem.

Does anyone have any suggestions on things I might try?

Here's my Python COM Server Code.

import win32com
import win32com.client

class IPython:
    _public_methods_ = ['runScript']
    _reg_progid_ = "Movaz.IPython"
    _reg_desc_ = "Python Inteface"
    _reg_clsid_ = "{FF2AA877-5F2B-4F28-A283-DB0C772442E9}"

    def __init__(self):
        print 'Creating IPython Object'
    
    def runScript(self, script, context):
        # Use these commands in Python code to auto generate .py
support
        from win32com.client import gencache
        gencache.EnsureModule('{B2794EF3-C0B6-11D0-939C-0020AF68E893}',
0, 1, 0)
        o = win32com.client.Dispatch(context)
        
        print 'The context object is', `o`
        
        o.SetValNumber("Locals.Gregg", 0, 100)
        return "Running Script " + str(script) + " " #+ str(name)

def Register():
    import win32com.server.register
    return win32com.server.register.UseCommandLine(IPython)
    
if __name__=='__main__':
    print "Registering COM server..."
    Register()

And a dump from Python when the error occurs.

Object with win32trace dispatcher created (object=None)
Creating IPython Object
in <IPython.IPython instance at 02E5B024>._QueryInterface_ with
unsupported IID IPersistStreamInit
({7FD52380-4E07-101B-AE2D-08002B2EC713})

in <IPython.IPython instance at 02E5B024>._QueryInterface_ with
unsupported IID IPersistPropertyBag
({37D84F60-42CB-11CE-8135-00AA004BB851})

in _GetIDsOfNames_ with '('runScript',)' and '1033'

in _Invoke_ with 1000 1033 3 (u'Test', <PyIDispatch at 0x2e5cf9c with
obj at 0x2b458e8>)
The object is <COMObject <unknown>>
Traceback (most recent call last):
  File "C:\Python21\win32com\server\dispatcher.py", line 40, in
_Invoke_
    return self.policy._Invoke_(dispid, lcid, wFlags, args)
  File "C:\Python21\win32com\server\policy.py", line 274, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Python21\win32com\server\policy.py", line 279, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None,
None)
  File "C:\Python21\win32com\server\policy.py", line 509, in
_invokeex_
    return apply(func, args)
  File "C:\Python21\IPython.py", line 25, in runScript
    o.SetValNumber("Locals.Gregg", 0, 100)
  File "C:\Python21\win32com\client\dynamic.py", line 432, in
__getattr__
    raise pythoncom.com_error, details
com_error: (-2147352567, 'Exception occurred.', (0, 'TSAPI', 'Expected
1 positional (non-named) parameters; found 0.', None, 0, -17808),
None)
    

Thanks in advance for any help.

Jeff



More information about the Python-list mailing list