pythoncom and IDispatch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fraca7

    pythoncom and IDispatch

    Hello. I got a little problem while using pythoncom to automate IE; for
    some reason, changing the 'selectedIndex' on an instance of
    IHTMLSelectElem ent doesn't fire the 'onchange' event (I guess this is a
    bug in mshtml).

    So, I tried to get the 'onchange' event handler and call it myself.
    According to the docs, this is a simple IDispatch implementation and
    calling Invoke() should do the trick; I actually have a working example
    of this in Delphi.

    But I can't manage to get it work in Python; the following code


    idisp = pythoncom.WrapO bject(elt.oncha nge)
    idisp.Invoke(py thoncom.DISPID_ VALUE,
    0x400, # LOCALE_USER_DEF AULT
    pythoncom.DISPA TCH_METHOD,
    False)

    fails with an AttributeError:

    Traceback (most recent call last):

    File "C:\Python22\Li b\site-packages\win32c om\server\polic y.py", line
    283, in _
    Invoke_

    return self._invoke_(d ispid, lcid, wFlags, args)

    File "C:\Python22\Li b\site-packages\win32c om\server\polic y.py", line
    288, in _
    invoke_

    return S_OK, -1, self._invokeex_ (dispid, lcid, wFlags, args, None,
    None)
    File "C:\Python22\Li b\site-packages\win32c om\server\polic y.py", line
    581, in _
    invokeex_

    return func(*args)

    File "ietest.py" , line 44, in OnDocumentCompl ete

    self.deleg.onDo cumentComplete( Dispatch(disp), url)

    File "ietest.py" , line 122, in onDocumentCompl ete

    self.current.on DocumentComplet e(self, browser, url)

    File "ietest.py" , line 141, in onDocumentCompl ete

    sink.nextStep()

    File "ietest.py" , line 96, in nextStep

    self.current.on Start(self)

    File "ietest.py" , line 191, in onStart

    False)

    pywintypes.com_ error: (-2147352567, 'Exception occurred.', (0, 'Python
    COM Serve
    r Internal Error', 'Unexpected Python Error: exceptions.Attr ibuteError:
    _Invoke_
    ', None, 0, -2147467259), None)


    Did I miss something ?

    TIA
  • Roger Upole

    #2
    Re: pythoncom and IDispatch


    "fraca7" <fraca7@free.fr > wrote in message news:44688c2a$0 $7047$636a55ce@ news.free.fr...[color=blue]
    > Hello. I got a little problem while using pythoncom to automate IE; for some reason, changing the 'selectedIndex' on an
    > instance of IHTMLSelectElem ent doesn't fire the 'onchange' event (I guess this is a bug in mshtml).[/color]

    As I understand it, this was done as a security measure to foil script
    exploits.
    [color=blue]
    > So, I tried to get the 'onchange' event handler and call it myself. According to the docs, this is a simple IDispatch
    > implementation and calling Invoke() should do the trick; I actually have a working example of this in Delphi.
    >
    > But I can't manage to get it work in Python; the following code
    >
    >
    > idisp = pythoncom.WrapO bject(elt.oncha nge)
    > idisp.Invoke(py thoncom.DISPID_ VALUE,
    > 0x400, # LOCALE_USER_DEF AULT
    > pythoncom.DISPA TCH_METHOD,
    > False)
    >
    > fails with an AttributeError:
    >[/color]

    You can access the underlying IDispatch using the
    _oleobj_ property, ie elt.onchange._o leobj_.Invoke(. .....)

    Alternately, you can also use FireEvent, which would look
    something like this (untested):
    elt.FireEvent(' onchange',elt.o nchange)


    Roger



    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

    Comment

    Working...