Sign in to follow this  
Followers 0
Drifter

Help with capturing a specific windows handle?

12 posts in this topic

I have a script that will run a particular program using ShellExecute(). What I want to be able to do is capture the handle of said program so I can check later whether the program has closed or not. The issue here is that, say we were opening notepad. The user may already have notepads opened outside of my program, I want to track ONLY the notepad window which was opened by the script using ShellExecute(). Is this possible?

Note: I am NOT looking for ShellExecuteWait(). My program needs to do other things (like have a responsive GUI) and so I will be tracking the handle with an AdLib. Thanks ;)

Share this post


Link to post
Share on other sites



You'd probably want to grab the PID of the program you opened. See the help file for the Run command or more specifically, the RunAsWait command (although you don't want that one, it has a better description).


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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I'm not sure exactly what a PID is, it seems similar to a window handle though. The issue here is that ShellExecute() doesn't return a PID, does it? or can i actually say

$ident = ShellExecute($path)

and then check it with WinExists() ?

Edit: In this case I guess it would be ProcessExists() ;)

Edited by Drifter

Share this post


Link to post
Share on other sites

I'm not sure exactly what a PID is, it seems similar to a window handle though. The issue here is that ShellExecute() doesn't return a PID, does it? or can i actually say

$ident = ShellExecute($path)

and then check it with WinExists() ?

Edit: In this case I guess it would be ProcessExists() ;)

PID = Process ID

ShellExecute does not return a PID, that's correct forgot about that one, but a simple way of getting the PID from the process you start is to use ProcessList("<name of your program>") before you execute it, and then execute the program, and run ProcessList again and compare the differences, your last run program will be the one that wasn't there before. If you execute Notepad 4 times, it will have 4 entries for Notepad.exe with 4 different PIDs, run it a 5th time you'll be able to tell which one just got started by filtering out the 4 that were there before.


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

Share this post


Link to post
Share on other sites

a very decent idea, and sounds relatively simple to implement. I will give it a little bit of work and see what i come up with. Maybe should store the different PID's in an array and use a delimiter such as a comma? I guess that's the only part I would have trouble with, actually storing the data in a way I can use to compare, especially if I don't know if ill get 1 result or 100 ;)

Share this post


Link to post
Share on other sites

actually a problem with this arises. I will only have access to what the program is supposed to be named in the title bar. The Process name might be different.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I'm getting issues with Run() as it says something about a -changedir flag. This is why I switched to ShellExecute(). Seems Run() is flawed in some way. I've been working on some code and I think I'm almost there.....

#Include <File.au3>

AutoItSetOption("WinTitleMatchMode",2)

$name = "Calculator"
$path = "C:\Windows\system32\calc.exe"

progStart($path)

Func progStart($path)

    ;prepare the launch process
    Local $szDrive,$szDir,$szFName,$szExt

    _PathSplit($path,$szDrive,$szDir,$szFName,$szExt)
    $workingDir = $szDrive & $szDir

    ;check what was running before
    $list1 = ProcessList()

    ShellExecute($path,"",$workingDir)
    WinWait($name,"",5000)
    $list2 = ProcessList()
    
    ;This is a particular PID with the same title info as the program we want, we dont know
    ;if this is the PID itself that we want... we will find it....
    $tempPid = WinGetProcess($name)

    If $list2[0][0] > 0 Then
        For $i = 1 To $list2[0][0]
            If $list2[$i][1] = $tempPid Then
                $tempPidName = $list2[$i][0]; This is the name of the process we need to find
            EndIf
        Next
    EndIf

    For $j = 1 To $list1[0][0]
        If $list1[$j][0] = $tempPidName Then
            ;result found, store the PID and compare to similar results in next for loop.
        EndIf
    Next

    For $k = 1 To $list2[0][0]
        If $list2[$k][0] = $tempPidName Then
            ;result found, store the PID and compare to similar results in last for loop.
        EndIf
    Next

    ;compare matches here, find Pid that is new to $list2 that isnt in $list1

EndFunc

I just need to be sure to catch and handle if in either ProcessList() call there's nothing, not thinking i did that except in one case, and I need to compare the two lists of PID's in two separate variables still.

How am I doing?

Edited by Drifter

Share this post


Link to post
Share on other sites

Seems a little overly complicated, personally if I had some sort of problem with

$PID = Run("notepad.exe")

I would use something like this

ShellExecute("notepad.exe")
$a = ProcessList("notepad.exe")
$PID = $a[$a[0][0]][1]
MsgBox(0,"PID of Newly launched Notepad","Closing : " & $PID)
ProcessClose($PID)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

@JohnOne

I hadn't realized you could grab the PID of the last opened program that way, makes sense though because the tasklist isn't sorted by the program names or the PIDs. I learn something new everyday. ;)


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

Share this post


Link to post
Share on other sites

Seems a little overly complicated[...]

Thank you very much JohnOne... I did wonder if this was possible. What you are saying is that the latest process listed is ALWAYS the process most recently started, yes?

Share this post


Link to post
Share on other sites

Im sure I read that, on these forums, but cannot find the thread, but in all the tests I done, the list was in the order of when the processes were started, and it makes sense that it would work that way.

Perhaps a Developer or someone with a greater understanding than I, of autoit innards could confirm this.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
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
Sign in to follow this  
Followers 0