![]() |
#1 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
SetForegroundWindow by PID
Hello everyone, this is my first post so please be gentle - if there is something wrong about the question format, let me know.
My intention is to bring a window back to foreground with my Process ID. Here is what I'v found so far: code: It works, but to make absolutely sure choosing the correct application (in case of multilingual or same name of windows), I would rather bring the window to foreground providing the process ID, not the window class or the title. I already have my necessary PID. Do you have any ideas how I can perform the SetForegroundWindow with PID? |
![]() |
![]() |
![]() |
#2 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
PHP Code:
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#3 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Thank you very much. I'll try it out the next days.
|
![]() |
![]() |
![]() |
#4 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Unfortunately it doesn't work:
Error: unterminated string parsing line at C:\InnoSetup-Software\Source - NSIS\Focus.nsi:52 -> System::Call 'USER32::GetWindowThreadProcessId(p$0,*i0r1) !include LogicLib.nsh ;!define mypid "3044" Function .onGUIEnd FindWindow $0 "" "Aqua'S KeyTest" IsWindow $0 0 +2 System::Call 'USER32::GetWindowThreadProcessId(p$0,*i0r1) ${If} $1 == $mypid System::Call 'user32::SetForegroundWindow(i r0)' ${EndIf} FunctionEnd Can you send me a little more specific code example I can work with? |
![]() |
![]() |
![]() |
#5 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
Just a typo, missing the final '. I edited my post.
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#6 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
thanks, the line is accepted now.
But I'm afraid I dont know how to search trough all windows in a loop until the process id matches the defined one (that was passed as argument to .exe). That's the final taget for my project. The code I've now shows endless the same item and can't compare the id I gave as parameter (not even when hardcoded as variable) - it always says they match. Can you help me a little bit with this task as I'm very new to NSIS and struggling with the help websites at all? |
![]() |
![]() |
![]() |
#7 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Got a few steps close to my aim, but fails if there are two instances of the Windows. The script should go through all one after the other...
Function .onGUIEnd loop: Pop $0 StrCpy $mypid "2424" ;FindWindow $0 ;IsWindow $0 0 +2 ;System::Call 'user32::SetForegroundWindow(i r0)' FindWindow $0 "" "AquaKeyTest" IsWindow $0 0 +2 System::Call 'USER32::GetWindowThreadProcessId(p$0,*i0r1)' MessageBox MB_OK "mypid(Gesucht)=$mypid und PID(Gefunden)=$1" ${If} $1 = $mypid MessageBox MB_OK "Test" System::Call 'user32::SetForegroundWindow(i r0)' IsWindow $0 done ${EndIf} System::Call "$R0" goto loop |
![]() |
![]() |
![]() |
#8 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
PHP Code:
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#9 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
!include WinMessages.nsh
!include LogicLib.nsh StrCpy $mypid "4128" ;example PID (ok) - later this should applied from command-line parameter StrCpy $2 "" loop: FindWindow $1 "" "Aquakeytest" "" $2 ;Windowtitle (not ok) should be determined automatically from PID StrCpy $2 $1 ${If} $1 P<> 0 ;${AndIf} $0 <> 0 ; PID System::Call 'USER32::GetWindowThreadProcessId(p$1,*i0r3)' ;MessageBox MB_OK "mypid(Search)=$mypid und PID(Found)=$3" ${If} $3 = $mypid System::Call 'USER32::SetForegroundWindow(p$1)' System::Call 'USER32::SwitchToThisWindow(p$1,i1)' ${Else} Goto loop ${EndIf} ${EndIf} Thank you very much - I changed the code a little bit so that it compares the result with my defined PID and use an existing process in general instead of creating a new one. So far it works. Now the PID and Windowtitle is necessary to know, but the Windowtitle should be created from PID automatically - is this possible? |
![]() |
![]() |
![]() |
#10 | |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
Not sure I understand. If you don't know the window title you can leave it as "" like my example.
If you know the title and the pid is part of it then you don't even need the loop Quote:
IntOp $PostCount $PostCount + 1 |
|
![]() |
![]() |
![]() |
#11 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Yes, I don't know the title, so the query for PID is indeed necessary.
But if I try to leave the title blank (FindWindow $1 "" "" "" $2), it doesn't work - in this case the entry it shows does't fit (${If} $1 P<> 0 seems to fail) and it repeats the loop infinte. So I have to retrieve the windotitle from pid before everything else... but how can I achieve this? |
![]() |
![]() |
![]() |
#12 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
There is nothing stopping you from enumerating all windows if you don't know the title nor class. If your loop is infinite you have a bug somewhere.
PHP Code:
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#13 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Thank you - you code works like a charm now!
May I ask to help me one more time? How can I compare the process name instead of the PID if the process name is known only? Input (search): -Process name- Output (found): -Process name- for "p $1" I tried with GetWindowModuleFilename, GetModuleBaseName and several others too, but never get a result at all. |
![]() |
![]() |
![]() |
#14 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
Those functions only work for the current process. https://stackoverflow.com/questions/...me-of-a-window
I'd say, use one of the process plug-ins, https://nsis.sourceforge.io/PS_plug-in etc. IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#15 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Unfortunately, I tried already. Either they don't include retrieving process name or it simply don't work.
I managed to call a result via wmic, but I cant get it to display each line of the variable separately. |
![]() |
![]() |
![]() |
#16 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,548
|
Seems to me you did not try very hard.
PHP Code:
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#17 | |
Junior Member
Join Date: Sep 2022
Posts: 11
|
Maybe, but I didn't focus on that one
In the meantime I get another solution with wmic... it's slow but seems to work: Quote:
|
|
![]() |
![]() |
![]() |
#18 |
Junior Member
Join Date: Sep 2022
Posts: 11
|
OK, with WMIC as for prcessname-based call and some other improvements here is my final solution as a ready-to-use binary download:
https://github.com/Dragodraki/BringExeToFront Hope, that helps somebody else in the future. |
![]() |
![]() |
![]() |
|
Tags |
findwindow, process id, setforegroundwindow |
Thread Tools | Search this Thread |
Display Modes | |
|
|