Jump to content

Get PID of application opening a non-exe file through cmd?


 Share

Recommended Posts

4 hours ago, maoyuusha said:

I cant pre-determine which music player its gonna open. 

Then your previous descriptions were somewhat misleading ;):

21 hours ago, maoyuusha said:

However what I want to get is the PID of the default application launched.

[...]

For this example, it opens up groove music so I'd like to get the PID of the Groove Music application which opened the mp3 file.

Until now you have only specified the (full) file name of the .mp3.

Therefore me (probably also @KaFu and @Nine ) assumed, that you want to start the default player associated with the .mp3 extension (here e.g. vlc.exe). Otherwise, the path to the player application would also have had to be given.

Let us briefly summarize :

* you have somewhere a media file (here "123.mp3") which you can access with an AutoIt script.

* the start should be possible by any existing media player, not only by the standard player.

* if the mediaplayer shows an error, you want to abort the application via e.g. taskkill. 

Is that correct so far?

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Here a more robust approach :

#include <Constants.au3>
#include <Array.au3>
#include <WinAPIProc.au3>
#include <File.au3>

Opt("MustDeclareVars", 1)
Opt("ExpandEnvStrings", 1)

Const $sFileName = @ScriptDir & "\David Bowie Starman.wav"
Local $sDrive, $sDir, $sName, $sExt
_PathSplit($sFileName, $sDrive, $sDir, $sName, $sExt)
Local $sDefault = RegRead("HKEY_CLASSES_ROOT\" & $sExt, "")
$sDefault = RegRead("HKEY_CLASSES_ROOT\" & $sDefault & "\shell\open\command", "")
$sDefault = StringRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0]
_PathSplit($sDefault, $sDrive, $sDir, $sName, $sExt)
Local $aList1 = _FindProcess($sName & $sExt)
Run(@ComSpec & ' /c start "dummy title" "' & $sFileName & '"')
Sleep(2000)
Local $aList2 = _FindProcess($sName & $sExt)
MsgBox ($MB_SYSTEMMODAL,"",CompareFileList ($aList2, $aList1))

Func _FindProcess($sProcessName)
  Local $aList = ProcessList(), $aPID[$aList[0][0]]
  $aPID[0] = 0
  For $i = 1 To $aList[0][0]
    If $aList[$i][0] = $sProcessName Then
      $aPID[0] += 1
      $aPID[$aPID[0]] = $aList[$i][1]
    EndIf
  Next
  ReDim $aPID[$aPID[0] + 1]
  Return $aPID
EndFunc   ;==>_FindProcess

Func CompareFileList (ByRef $aListAfter, ByRef $aListBefore)
  For $i = 1 to $aListAfter[0]
    _ArraySearch ($aListBefore, $aListAfter[$i], 1)
    If @error Then Return $aListAfter[$i]
  Next
  Return 0
EndFunc

This can be applied to ANY file extensions. You could also run the default program instead.  Would save you all list creation and comparaison...

Edited by Nine
Link to comment
Share on other sites

18 hours ago, Musashi said:

Then your previous descriptions were somewhat misleading ;):

Until now you have only specified the (full) file name of the .mp3.

Therefore me (probably also @KaFu and @Nine ) assumed, that you want to start the default player associated with the .mp3 extension (here e.g. vlc.exe). Otherwise, the path to the player application would also have had to be given.

Let us briefly summarize :

* you have somewhere a media file (here "123.mp3") which you can access with an AutoIt script.

* the start should be possible by any existing media player, not only by the standard player.

* if the mediaplayer shows an error, you want to abort the application via e.g. taskkill. 

Is that correct so far?

My bad, what you summarized are correct.

 

 

11 hours ago, Nine said:

Here a more robust approach :

#include <Constants.au3>
#include <Array.au3>
#include <WinAPIProc.au3>
#include <File.au3>

Opt("MustDeclareVars", 1)
Opt("ExpandEnvStrings", 1)

Const $sFileName = @ScriptDir & "\David Bowie Starman.wav"
Local $sDrive, $sDir, $sName, $sExt
_PathSplit($sFileName, $sDrive, $sDir, $sName, $sExt)
Local $sDefault = RegRead("HKEY_CLASSES_ROOT\" & $sExt, "")
$sDefault = RegRead("HKEY_CLASSES_ROOT\" & $sDefault & "\shell\open\command", "")
$sDefault = StringRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0]
_PathSplit($sDefault, $sDrive, $sDir, $sName, $sExt)
Local $aList1 = _FindProcess($sName & $sExt)
Run(@ComSpec & ' /c start "dummy title" "' & $sFileName & '"')
Sleep(2000)
Local $aList2 = _FindProcess($sName & $sExt)
MsgBox ($MB_SYSTEMMODAL,"",CompareFileList ($aList2, $aList1))

Func _FindProcess($sProcessName)
  Local $aList = ProcessList(), $aPID[$aList[0][0]]
  $aPID[0] = 0
  For $i = 1 To $aList[0][0]
    If $aList[$i][0] = $sProcessName Then
      $aPID[0] += 1
      $aPID[$aPID[0]] = $aList[$i][1]
    EndIf
  Next
  ReDim $aPID[$aPID[0] + 1]
  Return $aPID
EndFunc   ;==>_FindProcess

Func CompareFileList (ByRef $aListAfter, ByRef $aListBefore)
  For $i = 1 to $aListAfter[0]
    _ArraySearch ($aListBefore, $aListAfter[$i], 1)
    If @error Then Return $aListAfter[$i]
  Next
  Return 0
EndFunc

This can be applied to ANY file extensions. You could also run the default program instead.  Would save you all list creation and comparaison...

Doesn't work for me. I removed [0] in $sDefault = StirngRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0] since it looks like a syntax error? Msgbox just returns 0. I tried adding a Msgbox(0,"",$sDefault) after the first RegRead but it returns an empty msgbox.

Link to comment
Share on other sites

3 hours ago, maoyuusha said:

[ Refers to the latest script of @Nine :]

I removed [0] in $sDefault = StirngRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0] since it looks like a syntax error?

Just because something "looks" like a syntax error, it doesn't have to be one ;).

Explanatory note (regardless of other things) :

StringRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)  ==> Returns an array of matches !

$sDefault = StringRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0]  ==> sets String $sDefault = element [0] of this array. NO syntax error !!

You can also write it like this if you want:

Local $aDefaultArr = StringRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)
; _ArrayDisplay($aDefaultArr, "@@@DEBUG : Array " & $aDefaultArr) ; just for display
$sDefault = $aDefaultArr[0]
ConsoleWrite("> @@@DEBUG : $sDefault : " & $sDefault & @CRLF)

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

14 hours ago, maoyuusha said:

after the first RegRead but it returns an empty msgbox.

what is the value of $sExt before first RegRead ?  Put some error check around this regread so we know what is going on.

there is 3 possibilities here (I can think of)

1- your file name is badly formatted

2- you don't have access to registry (credentials)

3- your registry has missing keys or is corrupted (hope it is not that one)

Edited by Nine
Link to comment
Share on other sites

So I put back the [0] dunno why it didnt compile for me before. 

Seems like the problem is specific with how Windows 10 operates Groove Music as default music player.


I placed a bunch of Msgboxes around which made me come to this conclusion.
When Windows Media Player is my default music player it works perfectly!

#include <Constants.au3>
#include <Array.au3>
#include <WinAPIProc.au3>
#include <File.au3>

Opt("MustDeclareVars", 1)
Opt("ExpandEnvStrings", 1)

Const $sFileName = @ScriptDir & "\123.mp3"
Local $sDrive, $sDir, $sName, $sExt
_PathSplit($sFileName, $sDrive, $sDir, $sName, $sExt)
Msgbox(0,"",$sExt) ;1st MsgBox
Local $sDefault = RegRead("HKEY_CLASSES_ROOT\" & $sExt, "")
Msgbox(0,"",$sDefault) ;2nd MsgBox
$sDefault = RegRead("HKEY_CLASSES_ROOT\" & $sDefault & "\shell\open\command", "")
Msgbox(0,"",$sDefault) ;3rd MsgBox
$sDefault = StringRegExp($sDefault, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0]
Msgbox(0,"",$sDefault) ;4th MsgBox
_PathSplit($sDefault, $sDrive, $sDir, $sName, $sExt)
Local $aList1 = _FindProcess($sName & $sExt)
Run(@ComSpec & ' /c start "dummy title" "' & $sFileName & '"')
Sleep(2000)
Local $aList2 = _FindProcess($sName & $sExt)
MsgBox ($MB_SYSTEMMODAL,"",CompareFileList ($aList2, $aList1))

Func _FindProcess($sProcessName)
  Local $aList = ProcessList(), $aPID[$aList[0][0]]
  $aPID[0] = 0
  For $i = 1 To $aList[0][0]
    If $aList[$i][0] = $sProcessName Then
      $aPID[0] += 1
      $aPID[$aPID[0]] = $aList[$i][1]
    EndIf
  Next
  ReDim $aPID[$aPID[0] + 1]
  Return $aPID
EndFunc   ;==>_FindProcess

Func CompareFileList (ByRef $aListAfter, ByRef $aListBefore)
  For $i = 1 to $aListAfter[0]
    _ArraySearch ($aListBefore, $aListAfter[$i], 1)
    If @error Then Return $aListAfter[$i]
  Next
  Return 0
EndFunc

Windows Media Player Default Player
1 : .mp3
2 : WMP11.AssocFile.mp3
3 : C:\Program Files(x86)\Windows Media Player\wmplayer.exe /prefetch:6 /Open %L
4 : C:\Program Files(x86)\Windows Media Player\wmplayer.exe
5 : Correct PID

Groove Music Default Player
1 : .mp3
2 : WMP11.AssocFile.MP3
3 : C:\Program Files(x86)\Windows Media Player\wmplayer.exe /prefetch:6 /Open %L
4 : C:\Program Files(x86)\Windows Media Player\wmplayer.exe
5 : PID 0

Link to comment
Share on other sites

Based on your MsgBox, the default player in registry is still WMP.

Could you check if HKEY_CURRENT_USER\Software\Classes contains a .mp3 key.  That may be the explanation...

Edited by Nine
Link to comment
Share on other sites

On 12/23/2019 at 8:37 PM, Nine said:

Based on your MsgBox, the default player in registry is still WMP.

Could you check if HKEY_CURRENT_USER\Software\Classes contains a .mp3 key.  That may be the explanation...

Sorry for the late reply got busy with Christmas, speaking of Christmas Happy Holidays to you 😁


d2pDWfr.png

 

Value doesn't change whenever I set wmp or groove as my default music player. As for only .mp3 itself it only has (Default) REG_SZ (value not set).

Link to comment
Share on other sites

Windows 10 try the following basic example:

#include <Array.au3>
#include <StringConstants.au3>
#include <WinAPIProc.au3>
Opt("ExpandEnvStrings", 1)
ShellExecute(@ScriptDir & "\KillingInTheName.mp3")
Local $iMP3Player, $sMP3Default = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mp3\UserChoice", "ProgId")
Local $sMP3Player = RegRead("HKCR\" & $sMP3Default & "\shell\open\command", "")
If $sMP3Player = "" Then
    $sMP3Player = RegRead("HKCR\" & $sMP3Default & "\shell\open", "PackageId")
Else
    $sMP3Player = StringRegExp($sMP3Player, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0]
EndIf
Local $aList = ProcessList()
    ReDim $aList[$aList[0][0] + 1][3]
For $i = 1 To $aList[0][0]
    $aList[$i][2] = _WinAPI_GetProcessFileName($aList[$i][1])
    If StringInStr($aList[$i][2], $sMP3Player) Then
        $iMP3Player = $aList[$i][1]
        ConsoleWrite(".Mp3 Process Name = " & $aList[$i][0] & @CRLF)
        ConsoleWrite(".Mp3 PID = " & $aList[$i][1] & @CRLF)
        ConsoleWrite(".Mp3 Process Path = " & $aList[$i][2] & @CRLF)
        ExitLoop
    EndIf
Next

 

Link to comment
Share on other sites

2 hours ago, Subz said:

Windows 10 try the following basic example:

#include <Array.au3>
#include <StringConstants.au3>
#include <WinAPIProc.au3>
Opt("ExpandEnvStrings", 1)
ShellExecute(@ScriptDir & "\KillingInTheName.mp3")
Local $iMP3Player, $sMP3Default = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mp3\UserChoice", "ProgId")
Local $sMP3Player = RegRead("HKCR\" & $sMP3Default & "\shell\open\command", "")
If $sMP3Player = "" Then
    $sMP3Player = RegRead("HKCR\" & $sMP3Default & "\shell\open", "PackageId")
Else
    $sMP3Player = StringRegExp($sMP3Player, '^"(.+?)"', $STR_REGEXPARRAYMATCH)[0]
EndIf
Local $aList = ProcessList()
    ReDim $aList[$aList[0][0] + 1][3]
For $i = 1 To $aList[0][0]
    $aList[$i][2] = _WinAPI_GetProcessFileName($aList[$i][1])
    If StringInStr($aList[$i][2], $sMP3Player) Then
        $iMP3Player = $aList[$i][1]
        ConsoleWrite(".Mp3 Process Name = " & $aList[$i][0] & @CRLF)
        ConsoleWrite(".Mp3 PID = " & $aList[$i][1] & @CRLF)
        ConsoleWrite(".Mp3 Process Path = " & $aList[$i][2] & @CRLF)
        ExitLoop
    EndIf
Next

 

 

Thank you!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...