I would like to automate the use of a Windows program. More precisely, I would like to:
(1) open the Open File dialog, by clicking command Open...
in the File
menu of the application
(2) enter the name of the file to open in the Open File dialog
(3) click the Open
button of the Open File dialog
The problem is getting the handle of the edit control in phase (2). I do it with EnumChildWindows()
, with a callback function passed as an argument to EnumChildWindows()
. This callback function retrieves the name of the class of its hwnd
argument (with GetClassName()
); when that name is "Edit"
and the control is visible, we have found the edit control.
This approach works, but only after sleeping for 100 ms or so before calling EnumChildWindows()
: it takes some time for the edit control of the dialog to become available, even after you can get a handle of that dialog.
Instead of sleeping for 100 ms, I can also poll after sleeping at regular, smaller intervals:
- sleep for 10 ms
- call
EnumChildWindows()
- sleep for 10 ms if
EnumChildWindows()
didn’t find the edit control - call
EnumChildWindows()
- etc., until
EnumChildWindows()
finds the edit control - if the edit control is not found after for example 500 ms, fail
Is there a better solution than using such polling?
Windows API EnumChildWindows()
function documentation: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumchildwindows
Windows API EnumChildProc()
callback function documentation: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633493(v=vs.85)