skip to Main Content

This is my simple AHK script:

MsgBox % "Hello world"
ExitApp, 7777

I run this AHK from PHP by following code:

$result = exec('start pathahk.exe', $output, $result_code);

And now I want to get some data from ahk.exe. But when I dump variables:

var_dump($result, $output, $result_code);

The result is:

string(0) "" array(0) { } int(0)

All variables are empty. So, how I can get some usefull data after execution ahk.exe?

2

Answers


  1. EDIT: Looks like I got it in reverse. I thought you wanted your PHP result in AHK but looks like you want your AHK result in PHP. Still, the concept below should help you since AHK can write to StdOut using the FileAppend command with the asterisk (*) for the Filename which causes Text to be sent to standard output (stdout) (but check help – not in the current console window which doesn’t matter with my technique).

    It can be done, but I’m not sure what you are trying to accomplish. Just sending "Hello World" to the console? Can PHP use StdOut?

    If so, you could use the WScript.Shell then pass the command in the .Exec(ComSpec " /C cscript " command) method and return the exec.StdOut.ReadAll()

    I have a working example as follows, maybe it helps you:

    ;// The following AHK script runs a command (add.vbs but yours would be the PHP script?) 
    ;// and retrieves its output via StdOut:
    
    InputBox, x,,"Enter two numbers" ;//asks for two ints to pass to script
    MsgBox % """" RunWaitOne("add.vbs " x) """" ;// result in quotes in a ahk msgbox
    ExitApp
    
    RunWaitOne(command) ;// this is the function
    {
        shell := ComObjCreate("WScript.Shell")
        exec := shell.Exec(ComSpec " /C cscript /nologo " command)
        return exec.StdOut.ReadAll()
    }
    
    /* This is the external "add.vbs" command used in example (replace with yours):
    a=3
    b=4
    if WScript.Arguments.Count > 0 Then
        a=cint(WScript.Arguments.Item(0))
        b=cint(WScript.Arguments.Item(1))
    End If
    Dim StdOut : Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
    x=a+b
    StdOut.Write x
    */
    

    Hth,

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search