win32com: Setting ByRef parameters in an event

Paul Moore paul.moore at uk.origin-it.com
Tue Jul 24 09:24:01 EDT 2001


I'm trying to do some event handling code using win32com. I'm using a
component called WScriptEx as my test case, which has a Sleep method which
fires a regular OnTick event. The IDL for the OnTick event is

        HRESULT OnTick(
                        [in] VARIANT RemainingTime, 
                        [in, out] VARIANT* WakeUp);

The WakeUp parameter should be set if the event code wants to stop the sleep
at this point. I've tried this, and it works fine in VBScript:

    Set WScriptEx = WScript.CreateObject("WshToolbox.WScriptEx", "WScriptEx_")

    ' Sleep for 10 seconds, raising an OnTick event every 1 second
    WScriptEx.Sleep 10000, 1000

    Sub WScriptEx_OnTick (ByVal Remaining, ByRef Wakeup)
        WScript.Echo "Tick.... " & Remaining
        Wakeup = True ' Wake the sleep up
    End Sub


Now, based on a post from Mark Hammond ("Re: Handling Com Events in Python
(was Python and Windows Scripting Host)" from Fri, 11 Aug 2000) I can do this
by just returning the necessary value:

    import win32com.client

    class wexEvents:
        def OnTick(self, Remaining, WakeUp):
            print "Tick...", Remaining
            return -1

    wex = win32com.client.DispatchWithEvents("WshToolbox.WScriptEx",
                                             wexEvents)

    wex.Sleep(10000,1000)

But this doesn't seem to work! The thing just keeps ticking remorselessly
until the 10 seconds are up. (And, BTW, if I hit Ctrl-C, I get an error
"Unhandled exception detected before entering Python." - shouldn't I get a
KeybordInterrupt error?)

Am I doing something wrong? Is there a subtle bug here?

Any help would be appreciated - this sort of construct is important to me, and
I'd hate to be reduced to VBScript or (shudder...) Perl.

Thanks,
Paul




More information about the Python-list mailing list