Uniquely ID a window?

Everything else that doesn't fall into one of the other PB categories.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Uniquely ID a window?

Post 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!
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Uniquely ID a window?

Post 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. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Uniquely ID a window?

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Uniquely ID a window?

Post 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. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Uniquely ID a window?

Post 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. :)
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Uniquely ID a window?

Post 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
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Uniquely ID a window?

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Post Reply