luckyluke

Read output from CMD using PID

15 posts in this topic

Hello,

I have process id of CMD and i want to read output from it. i have used StdoutRead but it does not work.

for example: i start CMD without using Autoit, then i get the process ID, then i use Autoit to read the output using pID, but it does not work.

Pls help

thanks

Share this post


Link to post
Share on other sites



I lost my crystal ball so if you would post your code and the error or what exactly is not working will help a lot until i find it

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

14 hours ago, Juvigy said:

I lost my crystal ball so if you would post your code and the error or what exactly is not working will help a lot until i find it

#include <Constants.au3>

Local $iPID = InputBox("Input process id of CMD", "Input process id of CMD")
Local $sStdOut = ""
While 1
    $sStdOut &= StdoutRead($iPID)
    ConsoleWrite(StdoutRead($iPID) & @CRLF)
    If @error Then ExitLoop
Wend

Here is the code

Edited by luckyluke

Share this post


Link to post
Share on other sites

try this...

Local $iPID = Run('cmd /?', '', @SW_HIDE, 2); $STDOUT_CHILD
If @error Or Not $iPID Then Exit
Local $sStdOut = ""

Do
    Sleep(10)
    $sStdOut &= StdoutRead($iPID)
Until @error

MsgBox(0, '', $sStdOut)

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
8 hours ago, ripdad said:

try this...

Local $iPID = Run('cmd /?', '', @SW_HIDE, 2); $STDOUT_CHILD
If @error Or Not $iPID Then Exit
Local $sStdOut = ""

Do
    Sleep(10)
    $sStdOut &= StdoutRead($iPID)
Until @error

MsgBox(0, '', $sStdOut)

Thank you, but the point is i want the script to read from CMD, i dont want it run CMD command and read.

If the script run CMD, it will read the data out, but is there any way i can read it by using process ID?

 

Share this post


Link to post
Share on other sites
StdoutRead($iPID)

Doesn't work like that - it needs the PID returned from a child process and a RUN command. What exactly do you want to accomplish in more details?

Share this post


Link to post
Share on other sites
On 1/26/2017 at 9:14 PM, Juvigy said:
StdoutRead($iPID)

Doesn't work like that - it needs the PID returned from a child process and a RUN command. What exactly do you want to accomplish in more details?

Hi, here is the steps:

1. i start cmd in windows. open task manager to get the process ID of CMD

2. I run autoit script, then put the ID i got from step 1, then the script will need to read output from cmd. for example in cmd i type "help", then the script will read the output from cmd.

Is that ok?

Share this post


Link to post
Share on other sites
1 hour ago, luckyluke said:

Hi, here is the steps:

1. i start cmd in windows. open task manager to get the process ID of CMD

2. I run autoit script, then put the ID i got from step 1, then the script will need to read output from cmd. for example in cmd i type "help", then the script will read the output from cmd.

Is that ok?

Why not just run "wmic process list as part of the script.  The command will list detailed information about each process, including PID.  That may allow you to associate the process name to the pid:

$proc = Run ( @ComSpec & " /c wmic process list", @SystemDir, Default, $STDOUT_CHILD )
ProcessWaitClose ( $proc )
$list = StdoutRead ( $proc )
MsgBox ( 1, "Processes", $list )

 

Share this post


Link to post
Share on other sites

I told you - it doesnt work like this. You have to start the CMD from the script with run and then it will work. You cant attach to manually started CMD.

I found and old topic where this was implemented , but i cant make it work with the new AutoIt. It is here:

 

1 person likes this

Share this post


Link to post
Share on other sites

#10 ·  Posted

23 hours ago, MattHiggs said:

Why not just run "wmic process list as part of the script.  The command will list detailed information about each process, including PID.  That may allow you to associate the process name to the pid:

$proc = Run ( @ComSpec & " /c wmic process list", @SystemDir, Default, $STDOUT_CHILD )
ProcessWaitClose ( $proc )
$list = StdoutRead ( $proc )
MsgBox ( 1, "Processes", $list )

 

The point is i want to get text from CMD which is run by other program. so we cant start CMD using autoit.

Share this post


Link to post
Share on other sites

#11 ·  Posted

14 hours ago, Juvigy said:

I told you - it doesnt work like this. You have to start the CMD from the script with run and then it will work. You cant attach to manually started CMD.

I found and old topic where this was implemented , but i cant make it work with the new AutoIt. It is here:

Thank you, Juvigy. That is really help. I checked it and tested, it worked for me. 

The script using clipget function to get information from CMD command. But one small thing is that if a user using copy paste command (by press Ctrl+C) then it will be messed up.

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

Can you post your code? I was not able to make it work. What is your Autoit version ?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Please stop the quoting, just reply, quotes are meant for parts you want to refer to.

Even weirder is including your answer inside the quote.

Anyway, here's my thought on the way to solve it.

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
$hCmd = InputBox("Input", "Input title of CMD window"&@crlf&"(doesn't have to be complete)")
If @error Or $hCmd = '' Then Exit
WinActivate($hCmd)
SendKeepActive($hCmd)
$cmdtext2 = ClipGet()
Send( "! es{Enter}" )
$cmdtext = ClipGet()
ClipPut($cmdtext2)
MsgBox(64, '', $cmdtext)

This keeps your copy/paste text, because it copies to variable does it's thing and copies stuff back again, so it won't mess up copy paste.

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

#14 ·  Posted

Juvigy check the careca 's code, it worked for me.

 

Share this post


Link to post
Share on other sites

#15 ·  Posted

Gr8.  Thanks careca

It works for me too.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now