[python-win32] Re: Python COM

Thomas Heller theller at python.net
Mon Feb 23 15:30:38 EST 2004


"Dave" <dtcheung66 at hotmail.com> writes:

> I have been trying an example from the Python Programming on Win32
> book on the lastest versions of python (2.3.3) and win32all (build
> 163).  I create the COM object and try to call it from VB but i can't
> seem to create the child.
>
> debugging = 1
> if debugging:
>     from win32com.server.dispatcher import DefaultDebugDispatcher
>     useDispatcher = DefaultDebugDispatcher
> else:
>     useDispatcher = None
>     
> class Parent:
>     _public_methods_ = ['CreateChild', 'KissChild']
>     _reg_clsid_= "{E8F7F001-DB69-11D2-8531-204C4F4F5020}"
>     _reg_progid_ = "PythonDemos.Parent"
>   
>     def CreateChild(self):
>         child = Child()
>         print "Our Python child is", child
>         wrapped = wrap( child, useDispatcher=useDispatcher )
>         print "Returning wrapped", wrapped
>         return wrapped
>
>     def KissChild(self, child):
>         print "KissChild called with child", child
>         dispatch = win32com.client.Dispatch(child)
>         print "KissChild called with child named", dispatch.Name
>
>         child = umwrap(child)
>         print "The Python child is", child
>
>     class Child:
>         _public_methods_ = []
>         _public_attrs_ = ['Name']
>         def __init__(self):
>             self.name = "Unnamed"
[...]

> This is the error in the Python Trace Collector:
>
> Object with win32trace dispatcher created (object=None)
> in <ParentChild.Parent instance at 0x04609BE8>._QueryInterface_ with unsupported
>  IID IPersistStreamInit ({7FD52380-4E07-101B-AE2D-08002B2EC713})
> in <ParentChild.Parent instance at 0x04609BE8>._QueryInterface_ with unsupported
>  IID IPersistPropertyBag ({37D84F60-42CB-11CE-8135-00AA004BB851})
> in _GetIDsOfNames_ with '('CreateChild',)' and '1033'
>
> in _Invoke_ with 1000 1033 3 ()
> Traceback (most recent call last):
[...]
>   File "C:\Python23\ParentChild.py", line 16, in CreateChild
>     child = Child()
> NameError: global name 'Child' is not defined

Python is correct here: Child is not defined in the Parent's CreateChild
method.  You must start the definition of class Child in column 1 like so:

class Parent:
     def .....
     ......

class Child:
     _public_methods_ = []
     _public_attrs_ = ['Name']
     def __init__(self):
         self.name = "Unnamed"

Thomas




More information about the Python-win32 mailing list