Page 1 of 1
Uniquely ID a window?
Posted: Sat Mar 04, 2017 4:44 am
by Dude
Say I have the Windows Calculator open, and a folder named Calculator open. Now, assume I want to get the hWnd of the Windows Calculator. FindWindow_() alone is no good, because it matches both windows. So, I do additional check of the window's class and lcase(getfilepart(executable)), which is much better, and works quite well.
My question is: are those 3 properties enough to uniquely ID a window? Caption, class, executable? I can't use size or position, because the user can change those. Can anyone think of anything else that may be useful? Thanks!
Re: Uniquely ID a window?
Posted: Sat Mar 04, 2017 5:59 am
by TI-994A
Dude wrote:Say I have the Windows Calculator open, and a folder named Calculator open. Now, assume I want to get the hWnd of the Windows Calculator. FindWindow_() alone is no good, because it matches both windows. So, I do additional check of the window's class and lcase(getfilepart(executable)), which is much better, and works quite well.
Technically, even those might not be quite sufficient. In the unlikely event that there's another application with the same name, the class and executable name would still match. Of course, the path
(system32) could be another determiner, but unless the process is launched by our application, other approaches would not really be bulletproof.
Just an opinion, though.

Re: Uniquely ID a window?
Posted: Sat Mar 04, 2017 7:43 am
by Dude
TI-994A wrote:even those might not be quite sufficient
I know, hence why I'm asking if there's anything else that could be used.
Actually, now that I think about it, I could also use styles like if the window is resizable, is tool style, has a max or min button, etc.
TI-994A wrote:the path (system32) could be another determiner
No, tried that, and it fails because of "System32" vs "SysWow64"; that is, you don't know which path the system will use. I had this problem with Calc.exe and Notepad.exe, for example. It was reporting that Calculator didn't match, until I realized it was because one calc.exe had instance launched with "System32" and the other was with "SysWow64". So, not reliable, and hence why I use just the GetFilePart() instead. Also, sometimes the user might launch the same app/window from the same exe but from the exe being copied to two different folders, so another reason to ignore the path.
Re: Uniquely ID a window?
Posted: Sat Mar 04, 2017 7:51 am
by TI-994A
Dude wrote:TI-994A wrote:even those might not be quite sufficient
I know, hence why I'm asking if there's anything else that could be used.
Actually, now that I think about it, I could also use styles like if the window is resizable, has a max or min button, etc.
TI-994A wrote:the path (system32) could be another determiner
No, tried that, and it fails because of "System32" vs "SysWow64"; that is, you don't know which path the system will use. I had this problem with Calc.exe and Notepad.exe, for example. It was reporting that Calculator didn't match, until I realized it was because one calc.exe had instance launched with "System32" and the other was with "SysWow64". So, not reliable, and hence why I use just the GetFilePart() instead. Also, sometimes the user might launch the same app/window from the same exe but from the exe being copied to two different folders, so another reason to ignore the path.
Exactly; not bulletproof.

Re: Uniquely ID a window?
Posted: Sat Mar 04, 2017 7:57 am
by Dude
Using the caption, class, lower-case exe name (without path), and window styles (sizable? tool window? max button?) seems to work best. I also tried using things like GetParent_() and GetWindow_(..#GW_CHILD) but they failed by changing, probably because the OS changes them. So don't use those. Just putting this info here for future reference.

Re: Uniquely ID a window?
Posted: Sat Mar 04, 2017 11:22 am
by Keya
Code: Select all
hWnd = FindWindow_(#Null,"Calculator") ;<- you might also want to be more specific by specifying the Class
pid = GetWindowThreadProcessId_(hWnd)
;// If LCase(GetPathFromPid(pid)) = LCase(WindowsDirectory & "\system32\calc.exe") ;need to check syswow64 too
If LCase(GetFilePart(GetPathFromPid(pid))) = "calc.exe" ;or maybe filename alone is suffice
;confirmed it's Windows calc.exe and not Explorer window looking at C:\Calculator\ folder
EndIf
Re: Uniquely ID a window?
Posted: Sat Mar 04, 2017 11:48 am
by nco2k
@Dude
you wont find a window named "Calculator" on a foreign os.
i would browse the list of all running processes and check if the exes name and location match the one im looking for.
>> it fails because of "System32" vs "SysWow64"; that is, you don't know which path the system will use.
%SystemRoot%\system32 (CSIDL_SYSTEM) is for 64bit and %SystemRoot%\SysWOW64 (CSIDL_SYSTEMX86) is for 32bit. once you know the running exes bitness, its easy to do a proper path comparison.
>> sometimes the user might launch the same app/window from the same exe but from the exe being copied to two different folders.
true, he could also rename it. you could check for additional information like the company name, copyright, version and so on. but all of that data can be faked as well.
c ya,
nco2k