AutoHotkey Community

It is currently May 27th, 2012, 3:56 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: January 20th, 2006, 9:17 am 
Offline

Joined: January 8th, 2006, 10:31 pm
Posts: 8
I have the following ahk script that is supposed to execute my php program, passing the value 5. Then the ahk script should receive a single value from the program.

None of the 3 methods I've tried (based on various threads I've read) seem to work. Help appreciated. And I'll admit I'm a total newb with this stuff.

Here' the ahk:

Code:
#SingleInstance

; 1st method
/*
ErrorLevel = 999

RunWait, C:\php\php.exe getVPIP.php 5, ,Hide UseErrorLevel

MsgBox, Result: %ErrorLevel%
*/


; 2nd method
/*
ret1 := CMDret("C:\php\php.exe getVPIP.php 5")

MsgBox, Result: *%ret1%*

CMDret(CMD)
{
  VarSetCapacity(StrOut, 10000)
  RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
  Return, %StrOut%
}
*/


; 3rd method
CMDout=
CMDerr=
ProgramName := "C:\php\php.exe getVPIP.php 5"

Ret := RunWaitEx(ProgramName, NULL, TextIn, CMDout, CMDerr)
MsgBox, Return Value: %Ret% `r`n`r`nStdError: `r`n%CMDerr%`r`nStdOutput: `r`n%CMDout%

RunWaitEx(CMD, CMDdir, CMDin, ByRef CMDout, ByRef CMDerr)
{
  VarSetCapacity(CMDOut, 100000)
  VarSetCapacity(CMDerr, 100000)
  RetVal := DllCall("cmdret.dll\RunWEx", "str", CMD, "str", CMDdir, "str", CMDin, "str", CMDout, "str", CMDerr)
  Return, %RetVal%
}


And here's the PHP (super simple):
Code:
<?

$inputVar = $argv[1];

// perform logic based on input, then return value

// trying 3 different methods to return a value
echo 3;
return 4;
exit(5);

?>


Last edited by fooz on January 20th, 2006, 6:43 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2006, 9:44 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
There is one obvious bug, so I do not search further. Please test if that is the problem. Anyway, please give feedback.

When you want a value to be returned from a function, you have to use no %. This this line
Code:
Return, %var%
has to be
Code:
Return, var
This is wrong in method 2 and 3.

Method 1 will not work since the ErrorLevel will not contain the output of the command you run, but if the run command could be run. You have to use a pipe "|" and cb.exe to achieve it.

And please use the [ code] [ /code] tags to format your posts. This helps us to read your post.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2006, 4:21 pm 
Offline

Joined: January 8th, 2006, 10:31 pm
Posts: 8
Thanks for the reply.

Changing the return syntax as you suggest doesn't seem to matter. I think the nuance may be whether or not you follow Return with a comma.
Code:
Return, %RetVal%


...as opposed to...
Code:
Return RetVal


I was copying and pasting code directly from corrupt, who seems to know what he's doing. The code came from this post on CMDret: http://www.autohotkey.com/forum/viewtopic.php?t=3687


And I attempted method 1 based on this statement in the AutoHotKey Help manual:
Quote:
UseErrorLevel: UseErrorLevel can be specified alone or in addition to one of the above words (by separating it from the other word with a space). When the launch fails, this option skips the warning dialog, sets ErrorLevel to the word ERROR, and allows the current thread to continue. If the launch succeeds, RunWait sets ErrorLevel to the program's exit code, and Run sets it to 0.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2006, 4:28 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
You are right. It seams to me that the StrOut is a pointer, so the %% are needed. But I'm not sure, since I do not know much about about DllCall.

Regarding method 1 you might be right.

I have now idea what is wrong. Sorry.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2006, 6:42 pm 
Offline

Joined: January 8th, 2006, 10:31 pm
Posts: 8
Ok. I got method 1 to work, but I'd still like to learn more about how CMDret works. The first method is acceptable because my passed value can be an integer, but in the future it would help to have the flexibility to pass floats and strings and multiple values.

Regarding CMDret, and the following code:

Code:
CMDout=
CMDerr=
ProgramName := "C:\php\php.exe getVPIP.php 5"

Ret := RunWaitEx(ProgramName, NULL, TextIn, CMDout, CMDerr)
MsgBox, Return Value: %Ret% `r`n`r`nStdError: `r`n%CMDerr%`r`nStdOutput: `r`n%CMDout%

RunWaitEx(CMD, CMDdir, CMDin, ByRef CMDout, ByRef CMDerr)
{
VarSetCapacity(CMDOut, 100000)
VarSetCapacity(CMDerr, 100000)
RetVal := DllCall("cmdret.dll\RunWEx", "str", CMD, "str", CMDdir, "str", CMDin, "str", CMDout, "str", CMDerr)
Return, %RetVal%
}


I am using it to call a PHP program that outputs values/data to standard out. Is this what is captured in "CMDout"? Or do I need to somehow access that variable by reference within the called PHP program?



The way I got method 1 to work was by:
- ensuring the PHP program was passing specifically an INTEGER in its exit code
- and, using the full path to reference both the php.exe and the .php file


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 12:56 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2545
fooz wrote:
I am using it to call a PHP program that outputs values/data to standard out. Is this what is captured in "CMDout"? Or do I need to somehow access that variable by reference within the called PHP program?
CMDout should contain the text that was output to standard out :) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 1:55 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Although CmdRet is a fine tool, I never needed it. I use one of a few very simple approaches:
- I start "cmd.exe /c" with console programs, which accept a parameter, an expression to be evaluated, and redirect the output to a file. This file is read back by the AHK script. Because the file remains in the Windows buffer, this is not slower than any other method. Here is an example, which evaluates the marked expression and writes the result back to the same application.
Code:
!x::
   Send ^x
   calc = %ClipBoard%
   StringReplace calc, calc, ^, ^^, All
   StringReplace calc, calc, <, ^<, All
   StringReplace calc, calc, >, ^>, All
   RunWait %Comspec% /c "C:\Program Files\GnuWin32\bin\calc.exe" -p %calc% > c:\calc.tmp,,HIDE
   FileRead Result, c:\calc.tmp
   StringTrimRight Result, Result, 2
   StringReplace Result, Result, `r`n, `;, All
   Send ^v = %Result%
Return

- There are console programs, like LUA, which take input from a file. Cmd.exe allows writing the result to another file. The input file can be set up in AHK, and the result is read and processed in AHK.
Code:
AHKvar = 14

FileDelete C:\LUA.IN
FileAppend,
(
   c = 1+2*3-%AHKvar%
   print(c)
)
, C:\LUA.IN
RunWait %COMSPEC% /c C:\LUA\bin\lua.exe C:\LUA.IN > C:\LUA.OUT,,HIDE
FileRead result, C:\LUA.OUT
MsgBox %result%

- If a console application does not take input from a file, you can still use cmd.exe with "< file.in".
- If an application needs to start up and waits for the input, we can use Send from a Timer subroutine to give it its input. This example runs the Diehard randomness test with some parameters, and the result is processed in an editor, moving the cursor to the first failed result.
Code:
SetTimer Keys
RunWait %DieHard%\DIEHARD.EXE, %DieHard%,,PID
Run C:\t\notepad2\Notepad2.exe %DieHard%\%file%.res,,,np2PID
WinActivate ahk_pid %np2PID%
WinWaitActive ahk_pid %np2PID%
ControlSend,,{F3},ahk_pid %np2PID%
Sleep 250
ControlSend,Edit1,.0000{Enter},ahk_pid %np2PID%
Return

Keys:
   IfWinNotExist ahk_pid %PID%
      Return
   SetTimer Keys, Off
   ControlSend,,%file%.dat{Enter},ahk_pid %PID%
   ControlSend,,%file%.res{Enter},ahk_pid %PID%
   ControlSend,,111111111111111{Enter},ahk_pid %PID%
Return

- If a console application does not allow redirecting the output, you can always use the Clipboard to get the content of the terminal window. It is the case with interactive programs, where you want to see the output and also capture it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 4:35 pm 
Offline

Joined: January 8th, 2006, 10:31 pm
Posts: 8
Both the file and clipboard methods seem sloppy to me.

The php file I'm calling is a database api that I run many times a second to pull information from the database. Using file i/o would certainly slow this process down. And as for the clipboard, what if I'm copying and pasting in another app, couldn't I potentially lose either the programs data or the manual data I'm copying and pasting?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 6:13 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
fooz wrote:
Both the file and clipboard methods seem sloppy to me.
Several months ago we benchmarked all the return value capture methods we could think of (including modifications of the program to be run):
- ErrorLevel as return value
- Exit code
- Clipboard
- Process memory access (dll)
- Registry I/O
- Temporary files
- CmdRet dll
The temporary file method in the root directory of the boot disk was the fastest, for the reasons I mentioned: the file is created in the Windows buffer and accessed from there (delayed write). If it is deleted in the script, it never gets written to the disk, so it is just accessing fast internal memory. An idle disk is not even spun up.

Accessing a dll is not fast, even if it is cached. Also, it requires to install an extra file in all the machines your script is run, and update that, too, when the OS or the compiled script changes.

fooz wrote:
...as for the clipboard, what if I'm copying and pasting in another app, couldn't I potentially lose either the programs data or the manual data I'm copying and pasting?
You could save the clipboard content to an AHK variable before using the clipboard, and restore it at the end. I don't normally do it, because it could be slow if you have a gigabyte clipboard. Instead I use my own clipboard management script, which saves automatically, what I need.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 6:15 pm 
Offline

Joined: January 8th, 2006, 10:31 pm
Posts: 8
Great to hear about the file i/o benchmarking results. Thanks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 9:54 pm 
Offline

Joined: September 25th, 2005, 4:31 pm
Posts: 610
Laszlo wrote:
Accessing a dll is not fast, even if it is cached.


Accessing a DLL file loaded in memory is fast, which is the case for many of the core libraries.

Quote:
Also, it requires to install an extra file in all the machines your script is run, and update that, too, when the OS or the compiled script changes.


Given a careful selection of functions (easily accomplished), the library file(s) will exist on all Windows platforms and this will not be an issue.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 10:18 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
shimanov wrote:
Accessing a DLL file loaded in memory is fast, which is the case for many of the core libraries.
The benchmarks showed, that dll calls are not faster than buffered disk I/O, that is, dll calls accessing different processes or windows are slow.
shimanov wrote:
Given a careful selection of functions (easily accomplished), the library file(s) will exist on all Windows platforms and this will not be an issue.
We are speaking about CmdRet, a custom dll to capture the standard output in an AHK variable.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Leef_me and 38 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group