Send string to a non-ahk process that has stdin enabled Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
poodly

Send string to a non-ahk process that has stdin enabled

04 Feb 2015, 10:30

RetroArchhas an option to "enable stdin". If I understand correctly I can with that enabled send strings from other applications to that stdin which in turn will do commands. AFAIT the supported strings for stdin are the same as for the network command feature, the list is here
https://github.com/libretro/RetroArch/w ... k-Commands

How do from within an ahk script send a string to the stdin of retroarch? I've searched and read multiple threads on stdin/stdout but, I'm said to said, I haven't gotten my head around it. Please give some examples that a stdin/stdout noob can understand.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Send string to a non-ahk process that has stdin enabled

04 Feb 2015, 12:01

It's simple to send some text through the standard input of a program that you launch, using the WScript.Shell object. AutoHotkey allows you to push scripts through stdin, so I'll use that as an example.

Code: Select all

Shell := ComObjCreate("WScript.Shell")
Exec := Shell.Exec(A_AhkPath " *") ; Passing * as the first parameter to AHK tells it to read from StdIn
Exec.StdIn.Write("MsgBox, Hello")
Exec.StdIn.Close()
MsgBox
poodly

Re: Send string to a non-ahk process that has stdin enabled

04 Feb 2015, 18:08

Not simple for the stdin noob writing this sentence I'm afraid.

Your example, if I understand, is a script that sends itself a command.

I want to make an ahk script that sends commands to the external, non-ahk program retroarch.exe

I tried replacing A_AhkPath with a variable that contains the full path to retroarch.exe . That quickly opens a cmd box (like always on retroarch start) but then closes in less than a second.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Send string to a non-ahk process that has stdin enabled

04 Feb 2015, 23:29

My example shows creating a second script by running a new instance of AutoHotkey.exe, and giving it the script through StdIn

As for why it's not working with retroarch, did you take out the " *"? Everything in the parameter of the Shell.Exec() is part of the thing to run, not just the variable. For AutoHotkey.exe, * as the first command line parameter means to read the script from StdIn instead of a file. It's likely just confusing retroarch.
poodly

Re: Send string to a non-ahk process that has stdin enabled  Topic is solved

05 Feb 2015, 02:58

I did not take that out " *". Will try without that when I get to the computer with retroarch.

Your example created a new instance of Autohotkey. I want to send multiple commands over time with stdin to a single instance of retroarch. I want that instance to keep running until I close it manually. To do that do I have to first start the retroarch instance from withing AutoHotkey, keep sending Exec.StdIn.Write() commands from that same autohotkey script (and keep that script running) and never send Exec.StdIn.Close() ? Or is there a way to send StdIn commands to an already running process that wasn't started by the ahk script?
expert_vision
Posts: 21
Joined: 13 Jan 2014, 10:47

Re: Send string to a non-ahk process that has stdin enabled

05 Feb 2015, 05:45

You could try something like this:

Code: Select all

PID := ; obtain the PID of the process to which you want to send the text
DllCall("AttachConsole", "Int", PID)
;~ or
;~ DllCall("AttachConsole", "Int", -1) ; attach to parent's console
hStdIn := DllCall("GetStdHandle", "Int", -10) ; STD_INPUT_HANDLE = -10
Text := "Hello"
DllCall("WriteFile"
        , "UInt", hStdIn ; hFile
        , "Ptr", &Text ; lpBuffer
        , "UInt", StrLen(text) ; nNumberOfCharsToWrite
        , "PtrP", bytesWritten ; lpNumberOfCharsWritten
        , "UInt", 0) ; lpOverlapped

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 345 guests