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!