odd problem with watsup and VB6 application with modal dialog

Rob Williscroft rtw at freenet.co.uk
Sat Nov 11 13:20:20 EST 2006


Grumman wrote in news:JNc5h.194$3i6.48 at newsfe03.lga in comp.lang.python:

[snip]

> Roughly, I have a script that fills in a field, sets a combobox, then 
> clicks a button via clickButton. At this point, the python interpreter
> hangs. The only thing I've been able to identify as different about
> this form is that it is set as dialog-modal.
> 
> I've managed to work around this by adding a second python script that
> contains just enough code to click the offending button. I call it via
> os.system('start other.py') and it starts in a different process,
> clicks the button then hangs. 
> 
> But the main script continues on to fill in the following form, and 
> click another button. When the VB app returns to its starting form, 
> destroying the other form instances, the hung interpreter ends.
> 
> This works, but it is quite the ugly kludge isn't it?
> 
> Is this a normal behaviour for watsup with modal windows? Is there a 
> saner way around it?

AFAICT the problem is that the clickButton() function calls
the internal windows API function SendMessage(), which waits
for the buttons event handler to return something.

There is an alternative PostMessage(), which returns as soon as
the message is put in the target windows message queue.

A potential proplen with this is that it will return immediately
so the caller will need to wait (time.sleep) until the button 
handler has recieved the message and done something about it.

Here's a rewrite of the winGuiAuto.clickButton function,
post_clickButton() that uses PostMessage:

def post_clickButton( hwnd ):
  '''All code here adapted from winGuiAuto.py
  see http://www.tizmoi.net/watsup/intro.html
  '''
  
  def _buildWinLong(high, low):
    '''Build a windows long parameter from high and low words.
    
    See http://support.microsoft.com/support/kb/articles/q189/1/70.asp
    '''
    return int( 0xFFFFFFFF & ( (high << 16) | (low & 0xFFFF) ) )
      
  def _postNotifyMessage(hwnd, notifyMessage):
    '''Post a notify message to a control.'''
    import win32gui
    import win32con
    import win32api
    
    win32gui.PostMessage(
        win32gui.GetParent(hwnd),
        win32con.WM_COMMAND,
        _buildWinLong(
        notifyMessage, win32api.GetWindowLong(hwnd, win32con.GWL_ID)
        ),
        hwnd
      )
  
  import win32con
  _postNotifyMessage( hwnd, win32con.STN_CLICKED )
 
if __name__=='__main__':

  '''Just here to show how it works, this code won't actually 
  work unless you have a sutible GUI programme running.
  '''

  from watsup.winGuiAuto import \
    findControl,setEditText, \
    findTopWindow,clickButton
  from time import sleep

  def main():
          
      form=findTopWindow(wantedText='Main_Form')
      button=findControl(form,wantedText='Open')

      print 'clicking button to open dialog...'
      post_clickButton(button)

      sleep( 0.5 )
      
      form=findTopWindow(wantedText='Dialog_Form')
      button=findControl(form,wantedText='Close')
      
      print "clicking close button"
      post_clickButton(button)
      
  main() 

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list