Jump to content

Detecting an application opening another application


Recommended Posts

So I use PrimoPDF to auto-convert some files to PDF for me. After it's done with the conversion, it opens the file in whatever default application its made to open PDF files in (in my case Foxit).

I want to be able to track the handle to the spawned window from PrimoPDF so I can close it directly instead of blindly using a WinClose()/WinKill() against a named window, as the user may have Foxit reader open with another document.

I could check all of the Foxit Reader.exe processes that are running prior to starting, then check those spawned instances against it... but I was hoping for a more straight-forward approach.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Check it uses WinAPIEx to track the child process of another program.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thanks for the link Brew. This is what I ended up with. Looks a bit dirty, but I noticed that PrimoPDF spawns 2 processes, and they may overlap eachother for a split second: First program being what it opens to perform the pseudo-print command (if its a txt file it opens notepad.exe by default) and the second the pdf viewer (foxit reader.exe).

Func _ConvertFiles()
    $sConvertFolder = @AppDataDir & "PDF MergePROCESSING FOLDERConvert"
    $key = "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstallPrimoPDF"
    $primo = StringTrimLeft(StringTrimRight(RegRead($key, "InstallLocation"), 1), 1) & "PrimoPDF.exe"
    $asConvertFolder = _RecFileListToArray($sConvertFolder, "*", 1, 0, 0, 2)
    _ArrayDelete($asConvertFolder, 0)
    For $files in $asConvertFolder
        $hPrimo = Run($primo & ' "' & $files & '"', $sConvertFolder, "Open")
        TrayTip(@ScriptName, "File: " & $files & " being converted...", 5)
        _KillChildProcess($hPrimo)
    Next
    msgbox(0,"","STOPPING AT END OF CONVERT LOOP BEFORE CLEANUP")
    _Cleanup()
EndFunc

  Func _KillChildProcess($hPrimo)
    While 1
      Sleep(1000)
      $aiChild = _WinAPI_EnumChildProcess($hPrimo)
      If Not IsArray($aiChild) Then ContinueLoop
      If Not $aiChild[1][1] = "Foxit Reader.exe" Then
        ContinueLoop
      ElseIf Ubound($aiChild) > 2 Then
        If Not $aiChild[2][1] = "Foxit Reader.exe" Then
          ContinueLoop
        Else
          ProcessClose($aiChild[1][0])
          ExitLoop
        EndIf
      Else
        ProcessClose($aiChild[1][0])
        ExitLoop
      EndIf
    WEnd
  EndFunc

EDIT: Moved the whole Child process check to its own function

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Discovered something awkward about PrimoPDF usage...

Using the above on txt files seem fine (largest was only around 10kb however). I used it on a 300kb .doc file, which PrimoPDF uses OpenOffice to perform its pseudo-print. The loop kept exiting before the foxit reader window was ever even created. So the test script would end and foxit would popup with the finished document still.

Discovered that it did find a PID the first time around, and killed it, but then Primo spawned ANOTHER instance. I suppose since it wasn't done loading the first instance, it thought there was an error so it killed the second instance as well. Ugh...

So I added an additional loop to check again... and now it loops indefinitely because everytime it kills the process prior to the window coming up, it spawns a new one...

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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...