Search code examples
internet-explorerfocusuacnsistiming

When calling two browser windows in NSIS, how to make sure the last one gets focus?


I have this nsis script,

...

nsExec::Exec "openFirstWindow.exe"  ;HERE This exe will open a FIRST WINDOW to whatever url

Pop $exe_return_code
StrCmp $exe_return_code "0" exe_success
Goto exe_done
exe_success:
    ;HERE is the call to the SECOND WINDOW
    UAC::Exec '' '"$PROGRAMFILES\Internet Explorer\iexplore.exe" "http://www.google.ca"' '' ''

exe_done:

...

The problem is that randomly, I get the FIRST window in foreground and the SECOND behind, and sometimes I get the SECOND window in front of the FIRST.

My guess is that it is due to the FIRST window taking random time to open and if the SECOND window finishes opening before the FIRST one, then the FIRST one will get the focus and be on top.

Does anyone have a solution to make sure that the SECOND window has focus ?

Thank you!


Solution

  • Use this simple loop to bring required window in the front. (I used Exec, but works fine with UAC and nsExec)

    ; This Window is opened as first
    Exec '"$PROGRAMFILES\Internet Explorer\iexplore.exe" "www.google.sk"' 
    ; This is opened later
    Exec '"$PROGRAMFILES\Internet Explorer\iexplore.exe" "www.unsigned-softworks.sk/installer"'  
    
    CheckWindow:
      Sleep 500 ; Wait some time (miliseconds)
    
      ; Find by Window class and by Window name 
      FindWindow $1 "IEFrame" "Google - Windows Internet Explorer" 
    
      ; If window is not found (not loaded!) search again 
      IntCmp $1 0 CheckWindow Continue Continue
    
    Continue:
      # If found (opened & loaded), bring it to front
      System::Call "User32::SetForegroundWindow(i) b ($1)"