Process Command when Process Name Contains Spaces Topic is solved

Ask gaming related questions (AHK v1.1 and older)
megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Process Command when Process Name Contains Spaces

Post by megashub » 10 Aug 2022, 00:34

Code: Select all

IniRead, psname, c:\path\to\ini\config.ini, sectionname, keyname
Process, Wait, %psname%, 3
ProcessCheck1 := ErrorLevel
If Not ProcessCheck1
{
   ext = .exe
   newpsname = %psname%%ext%
}
Process, Wait, %newpsname%, 3
ProcessCheck2 := ErrorLevel
If Not ProcessCheck2
{ 
   MsgBox, "Unable to locate %psname% or %newpsname% -- exiting ..."
   ExitApp
}
If ProcessCheck2 != ""
{
   Process, Exist, %newpsname%
   pspid := ErrorLevel
} else { 
   Process, Exist, %psname%
   pspid := ErrorLevel
}
When I run this while keyname = Process Name With Spaces AHK's Process command fails to find it. But when I use the script provided in the Process KB page's Example #4 to print out all active processes to a MsgBox, Process Name With Spaces is shown in the list exactly as I specified it in the ini. When I use this same script while keyname = ProcessNameWithoutSpaces it works fine.

I also tried posting this to a help channel in Discord, but it was off-hours and the support request timed out.

Any idea what I'm doing wrong?
Last edited by BoBo on 11 Aug 2022, 13:27, edited 1 time in total.
Reason: Moved to 'Gaming'-section.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 10 Aug 2022, 00:48

Probably Process is treating a space delimited executable name like a filepath - that would be an invalid setting…
Name: The name of a process is usually the same as its executable (without path), e.g. notepad.exe or winword.exe. Since a name might match multiple running processes, only the first process will be operated upon. The name is not case sensitive.
Process, Wait,% """" psname """", 3 :?:

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 10 Aug 2022, 00:57

BoBo wrote:
10 Aug 2022, 00:48
Probably Process is treating a space delimited executable name like a filepath - that would be an invalid setting…
Name: The name of a process is usually the same as its executable (without path), e.g. notepad.exe or winword.exe. Since a name might match multiple running processes, only the first process will be operated upon. The name is not case sensitive.
Process, Wait,% """" psname """", 3 :?:
Good idea... moved to that syntax, same result unfortunately.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 10 Aug 2022, 05:12

Code: Select all

#Singleinstance, force
SetWorkingDir, A_ScriptDir

F10::FileAppend,% "MsgBox % " A_ScriptName,% "test test.ahk"            ;create test script with space delimited name

F11::                                                                   ;compile the previously created script into an executable.
   SplitPath, A_AHKPath,, Dir
   SplitPath, A_ScriptFullPath,,,, NoExt
   Run % comspec " /c " Dir "\compiler\Ahk2Exe.exe /in ""test test.ahk"" /out ""test test.exe"" "
   return

F12::                                                                   ;get PID of (already) started executable. 
   ; IniRead, psname, c:\path\to\ini\config.ini, sectionname, keyname
   psname := "test test.exe"
   Process, wait, %psname%
   MsgBox % errorlevel
   return
No problem here. Even checked in TaskManager's 'Details' tab.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 00:24

BoBo wrote:
10 Aug 2022, 05:12

Code: Select all

#Singleinstance, force
SetWorkingDir, A_ScriptDir

F10::FileAppend,% "MsgBox % " A_ScriptName,% "test test.ahk"            ;create test script with space delimited name

F11::                                                                   ;compile the previously created script into an executable.
   SplitPath, A_AHKPath,, Dir
   SplitPath, A_ScriptFullPath,,,, NoExt
   Run % comspec " /c " Dir "\compiler\Ahk2Exe.exe /in ""test test.ahk"" /out ""test test.exe"" "
   return

F12::                                                                   ;get PID of (already) started executable. 
   ; IniRead, psname, c:\path\to\ini\config.ini, sectionname, keyname
   psname := "test test.exe"
   Process, wait, %psname%
   MsgBox % errorlevel
   return
No problem here. Even checked in TaskManager's 'Details' tab.
This is very strange. I revised my script to ensure the quotes were being added correctly to the variable value, and it's still failing the same way. The net effect is that my psname := "Process With Spaces.exe" is the same as yours according to the msgbox printing them out. and yet Process, Wait still fails to find them.

Would you be willing to humor me by creating an actual test config.ini with one section and one keyname and then adding quotes within the ahk script to the resulting variable iniread creates?

What I have is:
config.ini looks like this:

Code: Select all

[sectionname]
keyname=test test
AHK looks like this:

Code: Select all

IniRead, processname, c:\path\to\ini\config.ini, sectionname, keyname
ext = .exe
newprocessname := "" processname . ext ""
newprocessname := """" newprocessname """"
processname := """" processname """"

Process, Wait, %processname%, 3
ProcessCheck1 := ErrorLevel
If not ProcessCheck1 Goto SecondCheck else Goto StorePID
    ; Unable to locate %processname% -- checking for .exe...

SecondCheck:
Process, Wait, %newprocessname%, 3
ProcessCheck2 := ErrorLevel
If not ProcessCheck2
{ 
    MsgBox, "Unable to locate %processname% or %newprocessname% -- exiting..."
    ExitApp
} else Goto StorePID

StorePID:
If ProcessCheck2 != "" 
{
   Process, Exist, %newprocessname%
   FoundPID := ErrorLevel	
} else {
   Process, Exist, %processname%
   FoundPID := ErrorLevel
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 11 Aug 2022, 00:46

Code: Select all

IniRead, processname, c:\path\to\ini\config.ini, sectionname, keyname

Process, Wait, %processname%, 3
The Process command expects it to be 'test test.exe' while your %processname% var only contains 'test test' without the extension. It's not a "mandatory quoted string"-issue.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 00:50

BoBo wrote:
11 Aug 2022, 00:46

Code: Select all

IniRead, processname, c:\path\to\ini\config.ini, sectionname, keyname

Process, Wait, %processname%, 3
The Process command expects it to be 'test test.exe' while your %processname% var only contains 'test test' without the extension.
Wait, I'm confused. Where in the docs does it say that Process always expects .exe? It only says:
Name: The name of a process is usually the same as its executable (without path), e.g. notepad.exe or winword.exe

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 11 Aug 2022, 00:55

…e.g. notepad.exe or winword.exe
So, the name with the extension (but without the path). Remove the exe from my 'test test.exe' setting and my script should wait indefinitely bc there is no process named 'test test' but 'test test.exe'.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 01:00

BoBo wrote:
11 Aug 2022, 00:55
…e.g. notepad.exe or winword.exe
So, the name with the extension (but without the path). Remove the exe from my 'test test.exe' setting and my script should wait indefinitely bc there is no process named 'test test' but 'test test.exe'.
Right. But the ini just has the name without the ext, and since some ...but crucially not all... process names contain .exe ... my script accounts for that by appending .exe whenever the Process command fails to find it without .exe. Also, the ini file is used for other scripts in other circumstances, using other tools and those tools don't play nice with variables with quotes in them. So the need is to add the quotes in this script, and append .exe in cases where it's necessary for a name match.
Last edited by megashub on 11 Aug 2022, 01:15, edited 1 time in total.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 01:09

BoBo wrote:
11 Aug 2022, 00:46
It's not a "mandatory quoted string"-issue.
I'm confused here too. The first thing I tried to use was the keyname without bothering with quotes at all. And I also tried appending .exe to the variable before Process, Wait, and I MsgBox'd the variable to make sure it looked correct. And Process still failed to find a matching process. If it doesn't require quotes around process names that contain spaces, then I don't know why else it would fail. I can confirm the name is correct in Task Manager and via commandline using wmic process.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 11 Aug 2022, 01:31

You see in my script that no string variable containing quotes is necessary for the Process command to be able to handle a space delimited executables name.
OTOH, you can add the extension whenever necessary this way…
Process, wait,% processname ".exe", 3 ;if taking 'test test' from the INIfile.

The "let’s quote the string" was just an assumption (like used with space delimited paths) - until validating proved that a space delimited name won't create a problem if used with the Process command. As said, your %processname% fails bc it lacks the extension, while the %newprocessname% fails bc it contains unnecessary quotes.

Code: Select all

#SingleInstance, Force

IniRead, processName, c:\path\to\ini\config.ini, sectionname, keyname		;get processName without extension
Process, Wait,% processName ".exe", 3										;adding '.exe' extension to the INIs key value
MsgBox % PID := (errorLevel) ? errorLevel : "Unable to locate " processname	;displaying a message if not found. PID if found. Remove 'MsgBox %' after testing.
HTH

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 12:49

BoBo wrote:
11 Aug 2022, 01:31
The "let’s quote the string" was just an assumption (like used with space delimited paths) - until validating proved that a space delimited name won't create a problem if used with the Process command. As said, your %processname% fails bc it lacks the extension, while the %newprocessname% fails bc it contains unnecessary quotes.

Code: Select all

Process, Wait,% processName ".exe", 3										;adding '.exe' extension to the INIs key value
Thanks for this. I commented my lines adding quotes to the variables, and just moved to the above provided Process, Wait syntax, and both my ErrorLevel checks still fail. If the above worked as you've described, the first check would fail because it lacks the extension, but now the second check would succeed and it would not error out. MsgBox once again confirms that the process names both look correct, only now without quotes. Very confusing.

Here's some supporting evidence, in case it's helpful:
pidfinder-01.png
pidfinder-01.png (60.7 KiB) Viewed 1618 times

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 11 Aug 2022, 13:26

I've no idea why you stick with your malfunctioning two step validation while you could do the same in a single line as you can see in the sample I've provided (yep, Process, wait is already providing a PID validation) And TBH, only describing what you've done without providing the code that you've used to get that result is kinda useless as no-one except yourself can see what you've changed and if other flaws have been embedded within that script.
Good luck.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 13:37

BoBo wrote:
11 Aug 2022, 13:26
I've no idea why you stick with your malfunctioning two step validation while you could do the same in a single line as you can see in the sample I've provided (yep, Process, wait is already providing a PID validation) And TBH, only describing what you've done without providing the code that you've used to get that result is kinda useless as no-one except yourself can see what you've changed and if other flaws have been embedded within that script.
Good luck.
I'm happy to supply the code. It's got other actions throughout for other purposes, so I was simply keeping the code as sanitized as possible to speed up Q&A and avoid rabbit-holing or miscategorization. I disagree with this being considered a Gaming request... functionally, it has nothing to do with gaming. In the example I provided, it happens to involve a Steam game, but this could easily be any app. And in fact, will likely be used for non-gaming apps too.

What happened to the response I provided with screen caps? Will that reply be approved to keep this thread current? I'll attach the full script in a moment.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 13:48

Thanks for approving the reply for additional context. As requested, attached is the full script. Apologies for any confusion I may have caused and thank you for the help.
Attachments
steam-k2k-01.ahk
(2.55 KiB) Downloaded 21 times

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 14:23

Just to be clear, the attached version also fails to find the process.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 11 Aug 2022, 14:33

Code: Select all

#NoEnv                        ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn                       ; Enable warnings to assist with detecting common errors.
SendMode Input                ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%   ; Ensures a consistent starting directory.
DetectHiddenWindows, On

if (A_Args.Count() < 2)
	exit("Missing parameter(s). Script will be terminated")

IniRead, hidflag     , C:\Users\LAUNCHBOX\Documents\dump\utilities\xpadder-arcade.ini,% A_Args[2], hidhide
IniRead, processName , C:\Users\LAUNCHBOX\Documents\dump\utilities\xpadder-arcade.ini,% A_Args[2], psname
IniRead, k2xflag     , C:\Users\LAUNCHBOX\Documents\dump\utilities\xpadder-arcade.ini,% A_Args[2], k2x

If (hidflag = 1) {
   RunWait % comspec    " /c ""C:\Program Files\Nefarius Software Solutions\HidHide\x64\HidHideCLI.exe"" 
          .       --dev-hide ""HID\VID_20A0&PID_41B6&MI_02&Col01\7&f5175f4&0&0000"" 
          .       --dev-hide ""HID\VID_D209&PID_0511&MI_00\7&230a7415&0&0000""
          .       --dev-hide ""HID\VID_D209&PID_0511&MI_02\7&1e117c59&0&0000""
          .       --dev-hide ""HID\VID_D209&PID_0512&MI_00\7&1204a3a1&0&0000""
          .       --dev-hide ""HID\VID_D209&PID_0512&MI_02\7&145b94db&0&0000"" "
   RunWait % comspec    " /c ""C:\Program Files\Nefarius Software Solutions\HidHide\x64\HidHideCLI.exe"" --cloak-on"
   }
 
If (k2xflag = 1) {
   Run % "C:\Users\LAUNCHBOX\LaunchBox\ThirdParty\Keyboard2Xinput-1.2.2\Keyboard2XinputGui.exe"
   WinWait, ahk_exe Keyboard2XinputGui.exe
   Sleep, 2000
   }

Run % """C:\Program Files (x86)\Steam\Steam.exe"" -applaunch " . A_Args[1]

isRunning := 0
While (isRunning = 0) {                                                               ; Wait until the game is launched
   RegRead, isRunning, HKCU\Software\Valve\Steam\Apps\%1%, Running
   ToolTip % isRunning
   Sleep, 500
   }
ToolTip

Process, Wait,% processName ".exe", 3
gamePID := (ErrorLevel) ? ErrorLevel : exit("Unable to locate " processname)
WinActivate, ahk_pid %gamePID%
MouseMove, 1920, 1080

While (isRunning = 1) {                                                                ; Wait until the game is closed
   RegRead, isRunning,% "HKCU\Software\Valve\Steam\Apps\" . A_Args[1], Running
   ToolTip % IsRunning
   Sleep, 500
   }
ToolTip
												 ; Game stopped, stop keyboard2Xinput by sending numpad multiply key (see k2x mapping.ini)
If (k2xflag = 1) {
   SendInput {NumpadMult}
   }

If (hidflag = 1) {
   RunWait % comspec " /c ""C:\Program Files\Nefarius Software Solutions\HidHide\x64\HidHideCLI.exe""
          .  --dev-unhide ""HID\VID_20A0&PID_41B6&MI_02&Col01\7&f5175f4&0&0000""
          .  --dev-unhide ""HID\VID_D209&PID_0511&MI_00\7&230a7415&0&0000""
          .  --dev-unhide ""HID\VID_D209&PID_0511&MI_02\7&1e117c59&0&0000""
          .  --dev-unhide ""HID\VID_D209&PID_0512&MI_00\7&1204a3a1&0&0000""
          .  --dev-unhide ""HID\VID_D209&PID_0512&MI_02\7&145b94db&0&0000"" "
   RunWait,% comspec " /c ""C:\Program Files\Nefarius Software Solutions\HidHide\x64\HidHideCLI.exe"" --cloak-off"
   }

exit(msg) {
   MsgBox % msg
   ExitApp
   }
JFTR. The Tooltip should provide if your While/RegRead construct is actually delivering any real-time output.
BTw, the script is using the vars %1%/%2% (changed to :arrow: A_Args) it looks like it has to be executed via a command line with two parameters!

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces

Post by megashub » 11 Aug 2022, 14:53

BoBo wrote:
11 Aug 2022, 14:33
JFTR. The Tooltip should provide if your While/RegRead construct is actually delivering any real-time output.
BTw, the script is using the vars %1%/%2% (changed to :arrow: A_Args) it looks like it has to be executed via a command line with two parameters!
With regard to commandline params, that's correct. I wrote it that way intentionally, so that is by design. The first arg is the Steam App ID of the program to be run by Steam. The second is the ini sectionname. Both are supplied as arguments when the script is run, in this case, by my frontend (Launchbox). Those parts both function fine. As are the While/RegRead portions. Since those work fine as-is, I didn't include those parts of the script in my thread. The problem is solely regarding the Process command, hence the focus of the thread.

megashub
Posts: 12
Joined: 10 Aug 2022, 00:28

Re: Process Command when Process Name Contains Spaces  Topic is solved

Post by megashub » 11 Aug 2022, 16:30

Update: I tested with a different app that also has spaces in its process name. Your revised code worked fine for that app. So I took a deep dive into my script and the ini and found a typo in the psname key in the ini specific to the Missile Command Recharged app that I've been struggling with. Once I fixed that, of course, the script now reliably finds PIDs like it should.

Thank you for your help and patience working through this with me. I really appreciate it. I hope whoever may find this thread later finds the scripting logic shared here useful.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Process Command when Process Name Contains Spaces

Post by BoBo » 11 Aug 2022, 22:54

:thumbup:

Post Reply

Return to “Gaming Help (v1)”