Execute wmplayer.exe, but don't transfer active session to it

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Cyewhang2 Chung Cyewhang2 Chung 7 months, 4 weeks ago.

  • Author
    Posts
  • #37462
    Profile photo of Cyewhang2 Chung
    Cyewhang2 Chung
    Participant

    wrote a script which execute wmplayer.exe to play a sound file, then ask a question or hit enter key to listen to the next sound. But currently I need to move the mouse's pointer and click on the Powershell's console/shell to enter the answer or hit enter key to continue. Is there a way which I can execute an external program/ applet/ application, but the user active windows dialog session is still be with the Powershell's console which activated/called the external program (example wmplayer.exe)? Or way to transfer back the activate user session.

    Thanks!

  • #37499
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    I think this will help you a bit. You can use this code to focus to any window you want.

    Add-Type -TypeDefinition @"
      using System;
      using System.Runtime.InteropServices;
      public class SFW {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    "@
    
    $app = (Get-Process -Name wmplayer).MainWindowHandle
    [SFW]::SetForegroundWindow($app)
    
  • #37507
    Profile photo of Cyewhang2 Chung
    Cyewhang2 Chung
    Participant

    Thanks Richard !
    I tried this;
    ——————
    Add-Type...etc... as what you had provided..
    $app = (Get-Process -Name powershell).MainWindowHandle

    ./wmplayer file.mp3
    [SFW]::SetForegroundWindow($app)
    ————————–
    And it worked.. It did switch back to powershell and make it the active session...
    But when I add into my script, it did not work.. still troubleshooting.. B(

    Will update again.. hope I can resolve it. Oh.. I only had one powershell process running.

  • #37508
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    Can you show the script that you made?

  • #37558
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    If you have it added to your script just after you launch wmplayer, it could very easily be a timing issue. The code is looking for the powershell process and bringing it to the foreground before the system displays the UI for wmplayer.

    Since you have not provided the code you have been playing with, it's hard to say what your options are. There are many ways to execute wmplayer in code. For example. If you are using Start-Process, you could use the -WindowStyle parameter to start the process minimized. Then it will not be on screen to take focus away from the powershell window. Providing a sample of what you are trying will definitely help in getting answers relevant to the challenge.

  • #37767
    Profile photo of Cyewhang2 Chung
    Cyewhang2 Chung
    Participant

    THANKS Richard and Curtis!

    I have no issue to send you guys the script, it is actually my first Powershell script.. a mini task to learn PS. About 200 lines.. it read and write to a CVS file. Below are section of the script. If you need to the whole script and CVS, I will upload it somewhere.

    BTW, I tried adding 'start-sleep -5' between wmplayer and [SFW]::SetForegroundWindow($app), but it don't help.

    Add-Type -TypeDefinition @"
      using System;
      using System.Runtime.InteropServices;
      public class SFW {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    "@
    $app = (Get-Process -Name powershell).MainWindowHandle
    
    
    #ToTestNext =2 mean new word added this round
    $PSspellRoot = "C:\Users\PSspell"
    $output = "$PSspellRoot\"
    [int16]$Max = 10           #If Max not reached, add more new words
    [int16]$MaxR = 10          #Max Reenforced Word
    
    $Words2 = Import-csv ".\WordBanks2.csv" | Select @{Name="Serial"; Expression={[int16]$_.Serial}}, `
                 Word,Meaning,`
                 @{Name="SoundFile"; Expression={[int16]$_.SoundFile}}, `
                 @{Name="ToTestNext"; Expression={[int16]$_.ToTestNext}}, ` 
                 @{Name="NumberWrong"; Expression={[int16]$_.NumberWrong}}, `
                 @{Name="NumberCorrect"; Expression={[int16]$_.NumberCorrect}}, `
                 @{Name="Difficulty"; Expression={[int16]$_.Difficulty}}, `
                 @{Name="ConsecutiveCounter"; Expression={[int16]$_.ConsecutiveCounter}}, `
                 @{Name="RepetitionCounter"; Expression={[int16]$_.RepetitionCounter}}
    
    $Lookup = Import-csv ".\LookUpRepeat.csv" | Select @{Name="1"; Expression={[int16]$_.1}}, `
                @{Name="2"; Expression={[int16]$_.2}}, @{Name="3"; Expression={[int16]$_.3}}, ` 
                @{Name="4"; Expression={[int16]$_.4}}, @{Name="5"; Expression={[int16]$_.5}}
    
    #==== other lines ========== not shown here.. basically to select words to test.
    
    $c=0   # counter to locate which word row
    foreach ($i in $Words2.ToTestNext)
        { if ($i -eq 1) 
            { $w = $Words2[$c].word
            ./wmplayer $output$w".mp3"
            Start-Sleep -s 5    #test
            [SFW]::SetForegroundWindow($app)
    
    ===other lines ====
    

    Basically, it is for spelling. The CVS file record words and other info, the same word spelling are re-enforced depending on the word difficulty and consecutive the word are being spelled correctly. The script can be shorten by using function..

    Added: 19 April : after I add the Start-sleep I notice the flashing cursor appeared back on the Powershell session, but I still need to click on the session windows to enable keyboard entry.

You must be logged in to reply to this topic.