Remote control of File Dialog causes application crash
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Remote control of File Dialog causes application crash

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Question Remote control of File Dialog causes application crash

    Hello,

    I try to remote control another application for automatization purposes. The following code should enter a filename into a opened filedialog.

    It seem to work under W7, but causes the application which opened the dialog to crash in 80% of the cases unter Win XP (but worked perfect in the other 20% of the cases).

    I found that it crashes every time, when the dialog is not opened completely. That is the reason why I am counting the childs of the dialog (I do not want to add fixed delays). But it already seems to be instable in some cases...


    Questions:
    - Does anybody know what can be wrong here (the number of children is correct - I have tested this with Spy++)?

    - Is there another method to detect if the dialog is openend completely than counting the childs (OS independent)?

    - Are all used functions correct?

    - Are there any problems possible with access rights to the other application window and how can it be solved?

    - Is there a more stable other method to remote control a windows file dialog?

    Code:
    static const wchar_t* dialogClassName = L"#32770"; // #32770 is dialog box class name
    
    scope_ptr<wchar_t> wDialogTitle(ConvertToWChar(dialogTitle));
    scope_ptr<wchar_t> wFilePathName(ConvertToWChar(filePathName));
    
    // Search for dialog handle and wait for dialog to open
    HWND dialog = NULL;
    for (int i=0; i<50; i++) // 2.5s
    {
      Sleeper::MSleep(50);
      dialog = FindWindow(dialogClassName, wDialogTitle); 
      if (dialog!=NULL)
      {
       break;
      }
    }
    
    // Wait for dialog to build up completely
    bool openedCompletely = false;
    for (int i=0; i<100; i++) // 5s
    {
      Sleeper::MSleep(50);
      QList<HWND> children = FindChildren(true, dialog, QString(), false, QString(), 0); // Search for all children
      if (children.size()>=childrenCount)
      {
       openedCompletely = true;
       break;
      }
    }
    
    // Activate dialog
    // Should I better use SetForegroundWindow(dialog) here???
    SetActiveWindow(dialog);
    
    // Search file path name line edit
    HWND edit = NULL;
    QList<HWND> children = FindChildren(true, dialog, QString(), false, "Edit", lineEditId);
    edit = children[0];
    
    // Enter file path name
    BOOL textSet = SendMessage(edit, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(wFilePathName.get()));
    
    // Click button
    SendDlgItemMessage(dialog, buttonId, BM_CLICK, 0, 0); // Click "Save" or "Open"
    
    // Wait for dialog to close 
    for (int i=0; i<20; i++) // 1s
    {
      Sleeper::MSleep(50);
      dialog = FindWindow(dialogClassName, wDialogTitle);
      if (dialog==NULL)
      {
       return true;
      }
    }

    I have used the following values for childrenCount and buttonId:

    Windows XP: childrenCount = 20, buttonId = 1148
    Windows 7: childrenCount = 41, buttonId = 1001


    I whould be happy about every hint.

    Thx in advance!

  2. #2
    Join Date
    Mar 2011
    Posts
    2

    Re: Remote control of File Dialog causes application crash

    You maybe can use the function IsWindowVisible on the handles of the desired children and loop until it returns true.

  3. #3
    Join Date
    Jan 2010
    Posts
    2

    Smile Re: Remote control of File Dialog causes application crash

    Yes, that did the trick!

    Checking for visibility of the whole dialog makes it unnecessary to count the children and there are no crashes any more.

    Thank you for that useful hint!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center