Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Findwindow / use PID to track a window that changes name?

If you have a question or need help, this is the place to be.
Post Reply
Roamin
Posts: 6
Joined: Sat Dec 27, 2008 5:19 am

Findwindow / use PID to track a window that changes name?

Post by Roamin »

I'm looking for a way to track the same window, now the problem is that the process is explorer.exe . I have set events so that when a specific remote control button is pressed, either my Videos library or my Music library is accessed via windows explorer. Now regularly i would just use findwindow, along with wildcards to isolate the window I want, but I cannot do this with Explorer , as there is at least one always running, the windows desktop. When i browse my libraries, the window name constantly changes, making the findwindow, in its current form, unusable. Is there a way to implement findwindow, to find a window according to the PID ? Although i may have many explorer.exe running in my process tree, they all have a different PID, which i could use to track the same explorer that was first launched, and use it to regain focus on that window, allowing to keep browsing my libraries. Now, it might not be possible to simply reuse the same explorer to return to my "root" folder of my libraries, as i would have to pass the correct folder argument to explorer while its running, rather then while its launching, but i would still use the PID to terminate the old explorer, and relaunch it pointing to the "root".

So basically, i'm looking for a way to track the windows based on their PID , because the window name completely changes, and no wild cards can help.

On a side not, i found the updated __init__.py in another thread (for the window plugin, move absolute) , while looking for more options of findwindow, and although i have done the same thing using the registry to detect if my 2nd display was active, the devs should include in the the latest release of EG.

Thank you.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Findwindow / use PID to track a window that changes name

Post by Pako »

I hope I got it right.
(So far) there is no other option than to use the script. PID can be obtained as follows:

Code: Select all

from eg.WinApi import GetWindowThreadProcessId
pid = GetWindowThreadProcessId(hwnd)[0]
Pako
Roamin
Posts: 6
Joined: Sat Dec 27, 2008 5:19 am

Re: Findwindow / use PID to track a window that changes name

Post by Roamin »

Thanks a lot for your support Pako, unfortunately, i don't know anything about python scripting to make anything usefull with the info you gave me. I only know a bit of C++, and i've programmed PIC microcontrollers in ASM before. If I try to run your 2 lines of code as a python script, i receive a hwnd not defined error. I tried adding it directly to findwindow.py but I then noticed that the script already imports GetWindowThreadProcessId , and it also does get the PID of the windows it tracks.

I guess what I want to do is a bit more complex then I first thought, because even if i were to launch explorer.exe , and get its PID , I would need to pass that value back to findwindow, and so I would need to track the window PID in a variable, which all that is way out of my league. ;)

Well, this is the final little detail to make my EG setup perfect. Guess I will have to settle with "next to perfect" for now. Maybe i'll look for another software to browse my media library.

Thank you very much.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Findwindow / use PID to track a window that changes name

Post by Pako »

I can not have any specific advice, I do not know your exact situation.
However, I will try it.
PID_searching.png
PID_searching.png (81.06 KiB) Viewed 4531 times
1) In the first step you normal way will find a window. It is the action Find Window:
SearchWindow.png
2) Do you need to know and remember the PID of this window. This is done in Python Script action.

Code: Select all

from eg.WinApi import GetWindowThreadProcessId
eg.globals.myPid = GetWindowThreadProcessId(eg.result[0])[0]
3) Title of your window changes and you want to find it by PID. This is done in a macro "Find Window by PID" using Python Script action.

Code: Select all

from eg.WinApi import GetWindowThreadProcessId, GetWindowText
findMyWindow = eg.WindowMatcher(u'explorer.exe', None, u'CabinetWClass', None, None, None, False, 0.0, 0)
myWindows = findMyWindow()
for hwnd in myWindows:
    if GetWindowThreadProcessId(hwnd)[0] == eg.globals.myPid:
        eg.plugins.Window.FindWindow(u'explorer.exe', GetWindowText(hwnd), u'CabinetWClass', None, None, None, False, 0.0, 0)
        break
       
4) Now you found a window and you can manipulate it. In this example, it is the action "Bring to front"

Pako
Roamin
Posts: 6
Joined: Sat Dec 27, 2008 5:19 am

Re: Findwindow / use PID to track a window that changes name

Post by Roamin »

Thank you very very much Pako! I've used what you showed me to do all that i wanted to do. I can now keep track of every single explorer.exe i launch, pressing the same button will either launch a specific windows explorer, or bring the already existing one to front. Also whenever a windows explorer closes, I look to see if the ones i'm keeping track of are still open, if they are closed, next time around my button press will launch them again. This is exactly what i was trying to do :).

Although everything is working perfect, i have a question on how things are done. Now i currently do this :

Code: Select all

from eg.WinApi import GetWindowThreadProcessId, GetWindowText
findMyWindow = eg.WindowMatcher(u'explorer.exe', None, u'CabinetWClass', None, None, None, False, 0.0, 0)
myWindows = findMyWindow()
eg.globals.SuccessVideo = 0
for hwnd in myWindows:
    if GetWindowThreadProcessId(hwnd)[0] == eg.globals.VideoPid:
        eg.plugins.Window.FindWindow(u'explorer.exe', GetWindowText(hwnd), u'CabinetWClass', None, None, None, False, 0.0, 0)
        eg.globals.SuccessVideo = 1
        break  
    else: eg.globals.SuccessVideo = 0
And next I do the simple python command :
Explorer.png
Explorer.png (17.48 KiB) Viewed 4513 times
eg.result = eg.globals.SuccessVideo

But is there anyway to make the actual python function return a value?

I can't thank you enough for your help, and google's for allowing me to fine tune my event sheet to a perfect state. Right now i cannot think of anything else that would make my setup any better.

I have a 50" plasma connected to my pc along with an HD pvr from videotron (canada) and I use the same remote to control my TV, PVR, PC along with a IR toy using winlirc. Like any universal remote, mine can be programmed with codes, but unfortunately not all buttons of the remote are assigned when you program a DVD, or a second TV to the AUX button. The manual says how to change the AUX to DVD, TV as you wish, but doesnt say you can also use it as a 2nd CBL remote. So using the same technique to set a DVD or TV , i set a second CBL (pvr box) to my remote, change the code from 001 to 002 and all buttons are available , and are found by winlirc, except menu and settings produce the same key. So this was my perfect setting. I've setup all the play/pause etc directly to VLC hotkeys, used the input button to toggle extended and single monitor displays. I've also made logic around my vlc's fullscreen button so that it will detect if my external monitor is plugged, and if so it will fullscreen on that monitor, if not , it will fullscreen on the default monitor. I have 3 buttons on my remote , A , B , C , so i used A to launch windows explorer to my video library, and B to launch my music library. The same buttons are used to find the windows and bring them to front if they are already launched. I've also added a alt-tab function i found in these forums, so that my channel up / down buttons toggle the windows.

This setup is perfect, i really cant think of anyhting else it could/should do .. and i have so many buttons left on my remote..

Thanks a ton! Virtual beer on me!
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Findwindow / use PID to track a window that changes name

Post by Pako »

Now I see better what you need.
I think it is better to do something like this:
RoaminConfig.png
RoaminConfig.png (5.28 KiB) Viewed 4503 times

Code: Select all

findMyWindow = eg.WindowMatcher(u'explorer.exe', None, u'CabinetWClass', None, None, None, False, 0.0, 0)
myWindows = findMyWindow()
pids = [eg.WinApi.GetWindowThreadProcessId(hwnd)[0] for hwnd in myWindows]
if "MusicPid" in eg.globals.__dict__:
    if not eg.globals.MusicPid in pids:
        eg.TriggerEvent("ClosedMusicLibrary")
if "VideoPid" in eg.globals.__dict__:
    if not eg.globals.VideoPid in pids:
        eg.TriggerEvent("ClosedVideoLibrary")
I can not try it, but I think it will work.

BTW: You can directly assign a value to eg.result inside in the script :

Code: Select all

eg.result = eg.globals.SuccessVideo
Pako
Roamin
Posts: 6
Joined: Sat Dec 27, 2008 5:19 am

Re: Findwindow / use PID to track a window that changes name

Post by Roamin »

Thanks once more Pako! I modified my event sheet to use your method and it works just as well, but with fewer statements so it should be more efficient. I'm still proud i had managed to get it working without asking too much. I guess the reason assigning eg.result inside a python script wasn't working is the condition i was testing was probably bad, and only ever giving me 1 answer.

Great, another virtual beer on me! As I said, i can't think of anything more to add to my setup right now, so I guess this is case closed. Maybe others looking for a similar way to track windows will profit from this thread.

btw, did you get the latest "move absolute" changes to be added in current/next EG update? ;)
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Findwindow / use PID to track a window that changes name

Post by Pako »

Roamin wrote:btw, did you get the latest "move absolute" changes to be added in current/next EG update? ;)
Modified action Move Absolute is available in current version 0.3.7.r1486.

Pako
You know flattr ? You can Image
Landingham
Posts: 1
Joined: Tue Mar 29, 2011 12:59 am

Re: Findwindow / use PID to track a window that changes name

Post by Landingham »

Roamin wrote:Thanks once more Pako! I modified my event sheet to use your method and it works just as well, but with fewer statements so it should be more efficient. I'm still proud i had managed to get it working without asking too much. I guess the reason assigning eg.result inside a python script wasn't working is the condition i was testing was probably bad, and only ever giving me 1 answer.

Great, another virtual beer on me! As I said, i can't think of anything more to add to my setup right now, so I guess this is case closed. Maybe others looking for a similar way to track windows will profit from this thread.

btw, did you get the latest "move absolute" changes to be added in current/next EG update? ;)




I guess what I want to do is a bit more complex then I first thought, because even if i were to launch explorer.exe , and get its PID , I would need to pass that value back to findwindow, and so I would need to track the window PID in a variable, which all that is way out of my league.
Post Reply