Can I always be sure of the hierarchical order of the controls found through FindWindowEx?

What am I doing?

I am automating a third-party application. The login window is as follows:

enter image description here

Note. A background image is embedded inside the form, and both USERNAME and PASSWORD are actually part of the image. Just in case, someone wonders why the tree below the Window Detective doesnโ€™t have, perhaps & raquo; Label

enter image description here

Where I am?

I have no problem handling components. I mean, I can usually access each window / control using the FindWindowEx function.

I can implement the GetWindowTextLength and / or GetWindowText for buttons to distinguish who has a handle. Although this approach works fine with Button (. Caption), it will not work with Edit controls. It seems that the Edit controls do not have any unique properties to distinguish which one. (correct me if I am wrong) ...

Problem

Suppose I need to know at the compilation stage that Edit controls which I am going to process, so I do not send a password for the username and so on. There are only two, but, as I said, I'm not sure how to choose the right one.

I would not mind understanding this at runtime, but I'm not sure how to distinguish the difference without sending a message and actually visually determining which one ...

Current solution

I would not call this a solution, but at the moment I rely only on the fact that every time I run my code, I always get a second (password) handle to the Edit control when the first handle is returned.

Question

Can I be 100% sure that the second Edit element will always be returned first in the hierarchy returned by FindWindowEx ?

If someone can confirm my idea, if I already had a BUT solution, if I cannot always expect the second Edit element to be returned, then I would like to hear some ideas on how to deal with this situation.

Note. I did not think that any code is really required for my question, but if someone wants to see the code, please leave a comment and I will add it to the question.

Thanks so much for your time.

+6
source share
1 answer

Each HWND child has an ID property. Use GetWindowLongPtr( hWnd, GWLP_ID ) to get it. Depending on how the target application is encoded, the ID may be a way to distinguish between a child control. Knowing the ID , you can get the child HWND using the GetDlgItem API (works fine when the parent is not a DialogBox, the API should have been called GetChildByID ).

Beware: some target applications do use random / untrusted values โ€‹โ€‹for identifiers.

There seems to be a general consensus: the listing API relies on Z-Order. See, for example, that SO answer (maybe making your question a kind of โ€œduplicateโ€)

Although some applications may โ€œplayโ€ with the Z-order of their child windows, they will not in the standard case, which means that the first created child window is at the top of the Z-order, and the last one created at the bottom.

+4
source

All Articles