Capture a scripts PID when it starts, gives wrong PID Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 17:19

Ok so I have a script that opens and exits other scripts.
When starting a new script, I want to catch the PID so that i can close it later, but I get the autohotkey laucher PID, not the scripts PID:

Code: Select all

run 'vlc.ahk', "D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2\",,&vlc_pid1						; starting a script containing my vlc-hotkeys + saving the pid (this is actually the laucher PID)
sleep 200
vlc_pid2 := Wingetpid("D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2\vlc.ahk - AutoHotkey")	; catching the PID again for the same script (the path + "AutoHotkey" is part of the script name)
OutputDebug vlc_pid1 																					; 15772
OutputDebug vlc_pid2																					; 18120

title_pid1 := Wingettitle("ahk_pid" vlc_pid1)
title_pid2 := Wingettitle("ahk_pid" vlc_pid2)

OutputDebug "title_pid1: " title_pid1																	; C:\Program Files\AutoHotkey\UX\launcher.ahk - AutoHotkey v2.0.11
OutputDebug "title_pid2: " title_pid2																	; D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2\vlc.ahk - AutoHotkey v2.0.11


ok, so what im getting when trying to catch the PID on run, is the autohotkey launcher PID..
I have other ways to make it work, im mostly curious if there is some workaround for this ?
coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 17:45

I haven't tested the launcher, but it sounds and looks like a prelaunch routine to switch among ahk version executables at runtime, similar to hashbang and ideas others here posted years ago. Nice to see it made into mainline, very progressive.
Essentially, the OS is not running the script directly, but passing it to the launcher, which runs first, reads the script, or the version directive then hands it off the actual autohotkey.exe that needs to run, hence why you get the PID of the launcher.

If you did the normal install, you have the launcher and verb registered, maybe this:
https://www.autohotkey.com/docs/v2/Program.htm#launcher-run

Then this, try with the full path and working dir, or the relative path and working dir, or only the full path.

Code: Select all

pid := RunWait("*Launch vlc.ahk", "D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2\")
Formatting may be wonky, but just do the verb on the script with runwait. /launch causes the launcher to auto exit upon hand off, returning the pid of the script it launched as its exit code, which runwait receives.
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 18:02

j46 wrote: I get the autohotkey laucher PID, not the scripts PID
Try this:

Code: Select all

run '"' . A_AhkPath . '" vlc.ahk', 'D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2',, &vlc_pid1
TAC109
Posts: 1128
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 18:17

Alternatively see here and scroll down to Run *Launch.

Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 18:39

@coffee wow no dude it worked perfectly. I would never have found this - thank you!
Such a specific solution to the problem too..
User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 18:51

@teadrinker nice! this also works!
Ive read up on A_AhkPath but still dont quite understand what is happening, do you mind to explain ?

From https://www.autohotkey.com/docs/v2/Variables.htm#AhkPath
A_AhkPath - For non-compiled or embedded scripts: The full path and name of the EXE file that is actually running the current script.

so the EXE that actually runs the file ? sound like we would get yet another PID if I were to guess. And what are all the ' " . about ?

still, thank you!
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: Capture a scripts PID when it starts, gives wrong PID  Topic is solved

30 Dec 2023, 20:31

j46 wrote: so the EXE that actually runs the file ? sound like we would get yet another PID if I were to guess
If you have two versions of AHK installed and you run the script by double-clicking or with run 'vlc.ahk', 'D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2',, &vlc_pid1, then the launcher starts first, which detects the version of the script and selects the appropriate interpreter (exe file) with which to run the AHK script, and the vlc_pid1 variable contains the process ID of the launcher (not the process of the running script).
But if you run the script using the line

Code: Select all

run '"' . A_AhkPath . '" vlc.ahk', 'D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2',, &vlc_pid1
then in this case your script vlc.ahk (not the Launcher) will be started, using the same interpreter that was used to run the original script, and the vlc_pid1 variable already contains the process ID of the running script.
j46 wrote: And what are all the ' " . about ?
The path to the interpreter in the A_AhkPath variable may contain spaces, for example:
C:\Program Files\AutoHotkey\v2\AutoHotkey.exe
In this case it must be enclosed in quotation marks:

Code: Select all

run '"' . A_AhkPath . '" vlc.ahk'
In the end, such an entry is resolved to

Code: Select all

run '"C:\Program Files\AutoHotkey\v2\AutoHotkey.exe" vlc.ahk'
User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 21:37

@teadrinker wow this is really insightful and clever I love it
I think i read this post like 10 times combined with trying stuff in the editor and reading the documentation but i think it almost sunk in now

The launcher I had no idea about that but it makes sense.
And with run you can specify what program you want to open the file with, by stating it before the file, I understand it? And we're saying to open it with the program/path that started our current script ie C:\Program Files\AutoHotkey\v2\AutoHotkey.exe. Nice :)

The one thing though still, the inner most ' ' and the dots, I dont get it. Why are they needed ? Why not just

Code: Select all

run '"A_AhkPath" vlc.ahk'
The dots combine things, such as strings, as far as I know, is that what they are doing here ?

Code: Select all

run '"' . A_AhkPath . '" vlc.ahk'
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: Capture a scripts PID when it starts, gives wrong PID

30 Dec 2023, 22:10

Dots are used to concatenate strings, but in this case they are only needed for clarity, to visually separate one string from another. You can do without them:

Code: Select all

run '"' A_AhkPath '" vlc.ahk'
j46 wrote: Why not just
You can see the difference like this:

Code: Select all

MsgBox '"A_AhkPath" vlc.ahk' . '`n'
     . '"' A_AhkPath '" vlc.ahk'
In the first case, A_AhkPath will be interpreted by AHK not as a variable, but as literal text.
In this example, the last dot has real meaning. It indicates to the interpreter that the line '"' A_AhkPath '" vlc.ahk' is a continuation of the previous line of code, not a new line.
User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Capture a scripts PID when it starts, gives wrong PID

31 Dec 2023, 08:28

@teadrinker so quotation marks because A_AhkPath may contain spaces and the single marks (') to escape it beeing interpreted as a litural string right ?
Haven't encountered this entanglement before, but I think I get it..

Just fooling around i found that

Code: Select all

run '' A_AhkPath ' vlc.ahk', 'D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2',, &vlc_pid1
also works even though A_AhkPath do contain a space in "\program files\", so I guess that does the same thing just with fewer symbols or what is different? Does the single and double quotes do the same thing here ?
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Capture a scripts PID when it starts, gives wrong PID

31 Dec 2023, 11:37

j46 wrote: so quotation marks because A_AhkPath may contain spaces and the single marks (') to escape it beeing interpreted as a litural string right ?
Explained here: Strings / Text

j46 wrote: Just fooling around i found that

Code: Select all

run '' A_AhkPath ' vlc.ahk', 'D:\Albin\Code\Autohotkey\Autohotkey scripts\Scripts_v2',, &vlc_pid1
also works even though A_AhkPath do contain a space in "\program files\", so I guess that does the same thing just with fewer symbols or what is different? Does the single and double quotes do the same thing here ?
No, what you did does not put quotes around the path at all, so it’s not doing the same thing. The two single quotes you put at the start accomplish nothing (you concatenated an empty string to the front of it). If it works, it’s only because it was able to find the correct path without quotes despite the spaces. The quotes ensure that it understands what is meant to be included in the path, but it doesn’t mean it will always fail without them.

The thing you need to understand is teadrinker’s example is putting double quotes in the actual string (and defines the literal string using single quotes so the double quotes don’t need to be escaped). Your version puts no quotes (not even single quotes) inside the string. See the difference by running this script:

Code: Select all

var := 'hello'
MsgBox '"' var '"' ; analogous to teadrinker’s version
MsgBox '' var '' ; analogous to your version
User avatar
j46
Posts: 20
Joined: 23 Apr 2022, 10:29

Re: Capture a scripts PID when it starts, gives wrong PID

02 Jan 2024, 20:57

@boiler
@teadrinker

Yepp I get it now, after playing around with it for a while - big thanks! This is awsome actually I feel a bit leveled up.
I dont know why that felt so difficult at first, just a bit unfamiliar I guess..
anyway Im happy

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: bob65536, shipaddicted, songdg and 39 guests