[FUNCTION] StdoutToVar with exit code

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: [FUNCTION] StdoutToVar with exit code

Re: [FUNCTION] StdoutToVar with exit code

by burque505 » 15 May 2018, 07:53

:) Glad to hear it!
Regards,
burque505

Re: [FUNCTION] StdoutToVar with exit code

by zhanglei1371 » 15 May 2018, 07:35

burque505 wrote:Hi zhanglei1371, try this:

Code: Select all

#Include StdOutToVar.ahk
msgbox % StdoutToVar_CreateProcess("cmd.exe /c dir c:\")
This works too:

Code: Select all

msgbox % StdoutToVar_CreateProcess(Comspec " /c dir c:\")
Regards,
burque505
ManyThx,it works!

Re: [FUNCTION] StdoutToVar with exit code

by burque505 » 07 May 2018, 19:09

Hi zhanglei1371, try this:

Code: Select all

#Include StdOutToVar.ahk
msgbox % StdoutToVar_CreateProcess("cmd.exe /c dir c:\")
This works too:

Code: Select all

msgbox % StdoutToVar_CreateProcess(Comspec " /c dir c:\")
Regards,
burque505

Re: [FUNCTION] StdoutToVar with exit code

by zhanglei1371 » 07 May 2018, 18:23

I used the code below,but nothing get,why?

msgbox % StdoutToVar_CreateProcess("dir c:\")
but if I used :
msgbox % StdoutToVar_CreateProcess("ping www.baidu.com"),it works.

Re: [FUNCTION] StdoutToVar with exit code

by xuezhe » 16 Oct 2017, 08:05

perfect.

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 02 Nov 2016, 04:00

Hello, this is probably due to the fact that the ReadFile is blocking. It has something to do with how the pipe are managed on Windows 10 i suppose, but it's something hard to debug.

Just for info this code has a problem if the child process does not flush its buffers in a timely manner. The ReadFile will be stuck until there will be some data in the pipe buffer and the autohotkey thread will be frozen (GUIs will be unresponsive). I just updated the function with a new version that checks pipe content first so it avoids any freezings.

Re: [FUNCTION] StdoutToVar with exit code

by menteith » 16 Jul 2016, 05:22

It did the trick, thanks. It is quite weird, however, because my initial code "cmd /k" works fine on Win 7.

Re: [FUNCTION] StdoutToVar with exit code

by qwerty12 » 16 Jul 2016, 05:16

menteith wrote:Does this function work on Windows 10?
Well, no, in this case because you're telling cmd to remain present after running dir. This function can't get the exit code until the child process terminates. Use cmd /C.

EDIT: My reasoning probably isn't right, as I wrongly assumed this function waited on the process's termination via waiting on the handle. In any case, leaving cmd behind wouldn't be the right thing to do IMHO.

EDIT: Thank you, cyruz, for the proper explanation and for updating your function. I don't use this, but your TermWait is a god send.

Re: [FUNCTION] StdoutToVar with exit code

by Joe Glines » 15 Jul 2016, 12:27

It hung for me as well. I'm running 64 bit Win10 and 32-bit AutoHotkey_L.

Re: [FUNCTION] StdoutToVar with exit code

by menteith » 15 Jul 2016, 09:47

Does this function work on Windows 10? I tested it on Win 7 x64 and there is no problem but on Windows 10 32bit EN-GB the following script starts and does nothing. There is only freezed icon in the taskbar.

Code: Select all

out := StdoutToVar_CreateProcess("cmd.exe /k dir c:\","CP1")
MsgBox % out
Return

; ----------------------------------------------------------------------------------------------------------------------
; Function .....: StdoutToVar_CreateProcess
; Description ..: Runs a command line program and returns its output.
; Parameters ...: sCmd      - Commandline to execute.
; ..............: sEncoding - Encoding used by the target process. Look at StrGet() for possible values.
; ..............: sDir      - Working directory.
; ..............: nExitCode - Process exit code, receive it as a byref parameter.
; Return .......: Command output as a string on success, empty string on error.
; AHK Version ..: AHK_L x32/64 Unicode/ANSI
; Author .......: Sean (http://goo.gl/o3VCO8), modified by nfl and by Cyruz
; License ......: WTFPL - http://www.wtfpl.net/txt/copying/
; Changelog ....: Feb. 20, 2007 - Sean version.
; ..............: Sep. 21, 2011 - nfl version.
; ..............: Nov. 27, 2013 - Cyruz version (code refactored and exit code).
; ..............: Mar. 09, 2014 - Removed input, doesn't seem reliable. Some code improvements.
; ..............: Mar. 16, 2014 - Added encoding parameter as pointed out by lexikos.
; ..............: Jun. 02, 2014 - Corrected exit code error.
; ----------------------------------------------------------------------------------------------------------------------
StdoutToVar_CreateProcess(sCmd, sEncoding:="CP0", sDir:="", ByRef nExitCode:=0) {
    DllCall( "CreatePipe",           PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 )
    DllCall( "SetHandleInformation", Ptr,hStdOutWr, UInt,1, UInt,1                 )
 
            VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24,  0 )
    siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 )
    NumPut( siSz,      si,  0,                          "UInt" )
    NumPut( 0x100,     si,  (A_PtrSize == 4) ? 44 : 60, "UInt" )
    NumPut( hStdInRd,  si,  (A_PtrSize == 4) ? 56 : 80, "Ptr"  )
    NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 60 : 88, "Ptr"  )
    NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 64 : 96, "Ptr"  )
 
    If ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000
                                  , Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) )
        Return ""
      , DllCall( "CloseHandle", Ptr,hStdOutWr )
      , DllCall( "CloseHandle", Ptr,hStdOutRd )
 
    DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before reading the stdout.
    VarSetCapacity(sTemp, 4095)
    While ( DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,4095, PtrP,nSize, Ptr,0 ) )
        sOutput .= StrGet(&sTemp, nSize, sEncoding)
 
    DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), UIntP,nExitCode )
    DllCall( "CloseHandle",        Ptr,NumGet(pi,0)                  )
    DllCall( "CloseHandle",        Ptr,NumGet(pi,A_PtrSize)          )
    DllCall( "CloseHandle",        Ptr,hStdOutRd                     )
    Return sOutput
}

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 20 Dec 2014, 06:50

Ajdaha wrote:Hi and thanks for this usefull function.

I just want to know is there a way to this function runs process with admin privilages? (I don't want to run my entire script as admin!)

I mean something like this "Run *RunAs ..." happen inside of the function!
(I'm genius and I know it! :v)
It should be possible, but you need to replace the CreateProcess system call with CreateProcessAsUser. You need to create a new token... It's not simple.

Re: [FUNCTION] StdoutToVar with exit code

by Ajdaha » 19 Dec 2014, 20:01

Hi and thanks for this usefull function.

I just want to know is there a way to this function runs process with admin privilages? (I don't want to run my entire script as admin!)

I mean something like this "Run *RunAs ..." happen inside of the function!
(I'm genius and I know it! :v)

Re: [FUNCTION] StdoutToVar with exit code

by Cubex » 06 Sep 2014, 06:38

wzxiaodu wrote:StdoutToVar_CreateProcess("ping http://www.sina.com.cn")
;;;;;;;;(my english is poor! sorry!)

result :
???? Ping auriga.sina.com.cn [61.172.201.194] ???? 32 ????????:
???? 61.172.201.194 ????: ???=32 ???=13ms TTL=247
???? 61.172.201.194 ????: ???=32 ???=13ms TTL=247
???? 61.172.201.194 ????: ???=32 ???=13ms TTL=247
???? 61.172.201.194 ????: ???=32 ???=12ms TTL=247

61.172.201.194 ?? Ping ??????:
?????: ????? = 4??????? = 4????? = 0 (0% ???)??
?????г????????(????????λ):
??? = 12ms???? = 13ms????? = 12ms
Hi wzxiaodu,
I have "Win7 32bit czech" and helped me:
StdoutToVar_CreateProcess(sCmd, sEncoding:="CP1", sDir:="", ByRef nExitCode:=0){
.....
You try it.

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 02 Jun 2014, 05:11

Corrected an error related to the exit code.

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 16 Mar 2014, 12:48

Updated with lexikos suggestion.

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 15 Mar 2014, 19:54

lexikos wrote:FYI, there's no single solution for decoding the output of a process, since a process can output in whatever encoding it wants. Programs can (but mostly don't, I think) set the codepage used by the console, and can output UTF-16 directly to the console, but neither works with redirected output (like this script).

I think mostly programs (especially ones which use C runtime functions like printf) just output in the system's default ANSI codepage, which happens to be the default for A_FileEncoding. However, if the script changes the default file encoding, it will fail. It would be better for the script to specify (as a parameter) the codepage that it expects the program to output, and default to "cp0".
Thank you lexikos. This encoding/codepage stuff has always been difficult for me. I will update the script with your suggestion soon.

Re: [FUNCTION] StdoutToVar with exit code

by lexikos » 14 Mar 2014, 20:58

FYI, there's no single solution for decoding the output of a process, since a process can output in whatever encoding it wants. Programs can (but mostly don't, I think) set the codepage used by the console, and can output UTF-16 directly to the console, but neither works with redirected output (like this script).

I think mostly programs (especially ones which use C runtime functions like printf) just output in the system's default ANSI codepage, which happens to be the default for A_FileEncoding. However, if the script changes the default file encoding, it will fail. It would be better for the script to specify (as a parameter) the codepage that it expects the program to output, and default to "cp0".

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 09 Mar 2014, 12:53

Code updated, removed input feature.

Re: [FUNCTION] StdoutToVar with exit code

by cyruz » 23 Feb 2014, 11:35

magusneo wrote:replace ""UTF-8"" as A_FileEncoding works under chinese win7 64.
Nice, I'll update.

Re: [FUNCTION] StdoutToVar with exit code

by magusneo » 23 Feb 2014, 09:08

replace ""UTF-8"" as A_FileEncoding works under chinese win7 64.

Top