Thanks a lot for your reply. 

Here is where I stand now: I can use Dispatch OK, but it seems I always
get a late-bound COM object when I do that, although I am able to use
the constants (the output of print rcx is <COMObject
SPIRIT.SpiritCtrl.1>). 

When I try to use DispatchWithEvents I get two different errors,
depending on whether I am using the ProgID or the CLSID. If I use the
ProgID I get a "SystemError: PyClass_New: base must be a class". With
CLSID I get "pywintypes.com_error: (-2147221164, 'Classe n\xe3o
registrada', None, None)", where the localized message says "Class not
registered". But the OCX must be registered, since I've been able to run
makepy with it.

Here is the program with both tests, and the outputs of two runs:

-- LISTING 4 ---------------
from win32com.client import gencache, constants, Dispatch,
DispatchWithEvents

class Rcx:
    def OnDownloadDone(self, ErrorCode=None, DownloadNo=None):
        print 'OnDownloadDone: %s, %s' % (ErrorCode, DownloadNo)

def test_without_events():
    rcx = Dispatch('SPIRIT.SpiritCtrl.1')
    print rcx
    rcx.ComPortNo = constants.COM2
    rcx.LinkType = constants.InfraRed
    rcx.PBrick = constants.RCX
    print rcx.InitComm()
    print rcx.TowerAndCableConnected()
    print rcx.CloseComm()
    print '*** test_without_events COMPLETED ***'

def test_with_events():
    rcx = DispatchWithEvents('SPIRIT.SpiritCtrl.1', Rcx) # option A
    # rcx = DispatchWithEvents('{D6CD40C0-A522-11D0-9800-D3C9B35D2C47}',
Rcx) # option B
    print rcx
    rcx.ComPortNo = constants.COM2
    rcx.LinkType = constants.InfraRed
    rcx.PBrick = constants.RCX
    print rcx.InitComm()
    print rcx.TowerAndCableConnected()
    print rcx.CloseComm()
    print '*** test_with_events COMPLETED ***'

if __name__ == '__main__':
    spiritModule =
gencache.EnsureModule('{D6CD40C0-A522-11D0-9800-D3C9B35D2C47}', 0, 1, 0)
    print 'spiritModule:', repr(spiritModule)
    spiritClass = gencache.GetClassForProgID('SPIRIT.SpiritCtrl.1')
    print 'spiritClass:', repr(spiritClass)
    test_without_events()
    test_with_events()

---------------------------

-- OUTPUT OF LISTING 4 - OPTION A -------------
spiritModule: <module
'win32com.gen_py.D6CD40C0-A522-11D0-9800-D3C9B35D2C47x0x1x0' from
'c:\python21\win32com\gen_py\D6CD40C0-A522-11D0-9800-D3C9B35D2C47x0x1x0.pyc'>
spiritClass: <class
win32com.gen_py.D6CD40C0-A522-11D0-9800-D3C9B35D2C.Spirit at 0081AD7C>
<COMObject SPIRIT.SpiritCtrl.1>
1
1
1
*** test_without_events COMPLETED ***
Traceback (most recent call last):
  File "rcx_mod2.py", line 36, in ?
    test_with_events()
  File "rcx_mod2.py", line 19, in test_with_events
    rcx = DispatchWithEvents('SPIRIT.SpiritCtrl.1', Rcx) # option A
  File "c:\python21\win32com\client\__init__.py", line 225, in
DispatchWithEvents
    result_class = new.classobj("COMEventClass", (disp_class,
events_class, user_event_class), {"__setattr__" : _event_setattr_})
SystemError: PyClass_New: base must be a class

-- OUTPUT OF LISTING 4 - OPTION B -------------
spiritModule: <module
'win32com.gen_py.D6CD40C0-A522-11D0-9800-D3C9B35D2C47x0x1x0' from
'c:\python21\win32com\gen_py\D6CD40C0-A522-11D0-9800-D3C9B35D2C47x0x1x0.pyc'>
spiritClass: <class
win32com.gen_py.D6CD40C0-A522-11D0-9800-D3C9B35D2C.Spirit at 0081931C>
<COMObject SPIRIT.SpiritCtrl.1>
1
1
1
*** test_without_events COMPLETED ***
Traceback (most recent call last):
  File "rcx_mod2.py", line 36, in ?
    test_with_events()
  File "rcx_mod2.py", line 20, in test_with_events
    rcx = DispatchWithEvents('{D6CD40C0-A522-11D0-9800-D3C9B35D2C47}',
Rcx) # op
tion B
  File "c:\python21\win32com\client\__init__.py", line 208, in
DispatchWithEvent
s
    disp = Dispatch(clsid)
  File "c:\python21\win32com\client\__init__.py", line 94, in Dispatch
    dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)
  File "c:\python21\win32com\client\dynamic.py", line 81, in
_GetGoodDispatchAnd
UserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "c:\python21\win32com\client\dynamic.py", line 72, in
_GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.II
D_IDispatch)
pywintypes.com_error: (-2147221164, 'Classe n\xe3o registrada', None,
None)

-----------------------------------------------


Mark Hammond wrote:
> 
> > Now, reading some other examples included with ActivePython, I realized
> > the COM wrapper class generated by makepy is just a mixin. So I tried
> > this other approach, inspired by the
> > \Python21\Pythonwin\pywin\Demos\ocx\webbrowser.py example included in
> > the distribution:
> 
> ...
> 
> > I also noticed when I print rcx.__obj__ that the result is None. As far
> > as I understand, the __obj__ attr should point to the COM counterpart of
> > my Python class, so it being None must be bad.
> 
> __obj__ is the MFC object.  _oleobj_ should be the underlying COM object.
> 
> > The Activex examples provided with ActivePython assume that the OCX is
> > attached to a window. But in this case this would be useless, since I
> > have already been able to operate the RCX through Spirit.OCX without any
> > GUI. All I need is a way to subclass the Spirit class to implement event
> > handlers.
> 
> The pywin.demos samples certainly do assume a window, and make use of MFC's
> event handling capabilities.  They predate the builtin win32com event
> handling.
> 
> I am afraid I am at the OReilly conference at the moment, so haven't been
> following as closely as I would otherwise.  I can't recall the problem you
> had attempting to use the technique outlined in the docstrings for
> win32com.client.DispatchWithEvents().  Can you mail me that info again (no
> need to CC the list)
> 
> Mark.
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython

Reply via email to