[FUNCTION] StdoutToVar with exit code

Post a reply

Confirmation code
Enter the code exactly as it appears. All letters are case insensitive.
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

Post by burque505 » 15 May 2018, 07:53

:) Glad to hear it!
Regards,
burque505

Re: [FUNCTION] StdoutToVar with exit code

Post by zhanglei1371 » 15 May 2018, 07:35

burque505 wrote:Hi zhanglei1371, try this:

Code: [Select all]GeSHi © Codebox Plus

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


This works too:

Code: [Select all]GeSHi © Codebox Plus

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

Regards,
burque505


ManyThx,it works!

Re: [FUNCTION] StdoutToVar with exit code

Post by burque505 » 07 May 2018, 19:09

Hi zhanglei1371, try this:

Code: [Select all]GeSHi © Codebox Plus

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


This works too:

Code: [Select all]GeSHi © Codebox Plus

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

Regards,
burque505

Re: [FUNCTION] StdoutToVar with exit code

Post 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

Post by xuezhe » 16 Oct 2017, 08:05

perfect.

Re: [FUNCTION] StdoutToVar with exit code

Post 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

Post 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

Post 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

Post 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

Post 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] [Expand]GeSHi © Codebox Plus

Re: [FUNCTION] StdoutToVar with exit code

Post 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

Post 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

Post 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

Post by cyruz » 02 Jun 2014, 05:11

Corrected an error related to the exit code.

Re: [FUNCTION] StdoutToVar with exit code

Post by cyruz » 16 Mar 2014, 12:48

Updated with lexikos suggestion.

Re: [FUNCTION] StdoutToVar with exit code

Post 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

Post 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

Post by cyruz » 09 Mar 2014, 12:53

Code updated, removed input feature.

Re: [FUNCTION] StdoutToVar with exit code

Post 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

Post by magusneo » 23 Feb 2014, 09:08

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

Top