Jump to content
Chimp

[solved] problem to drive a command prompt via a NamedPipe

Recommended Posts

Posted (edited)

I'm trying to drive a command prompt by sending instructions via a NamedPipe.
This way you can (should) be able to send commands to the command prompt and at the same time "view" the result in the same window.
This is not allowed if you run a command prompt with "opt_flag" parameters (redirected streams) because this will disable StdOut on the cmd itself.

This small (trivial) snippet works for the first command sent to the cmd, but further submissions will fail.

Maybe the problem is in how I use the run () command to start a new cmd with the StdIn redirect from the NamedPipe. It seems that the generated cmd will close automatically after the first reception of the command via NamedPipe.

suggestions on how to make it work are welcome
thanks

#include <NamedPipes.au3>
#include <WinAPI.au3>

; Creates an instance of a named pipe
Global $sPipeName = "\\.\pipe\pipename"
Global $hPipe = _NamedPipes_CreateNamedPipe($sPipeName, 1, 1)

MsgBox(0, "Debug", "Pipe created. Now open a CMD")

; run a cmd with only StdIn redirected (StdIn data incoming from a pipe)
Global $hCMD = Run(@ComSpec & " /K cmd < " & $sPipeName & @CRLF, "c:\") ; ok?

MsgBox(0, "Debug", "now Send a command to the cmd via a NamedPipe")
_StdInPipeWrite("dir" & @CRLF)

MsgBox(0, 'Debug', "further commands will fail" & @CRLF & "now send command 'dir c:\windows'")
$sMessage = "Dir c:\windows" & @CRLF
_StdInPipeWrite($sMessage)

MsgBox(0, 'Debug', "send another command (will also fail)" & @CRLF & "now send command 'echo Hello'")
_StdInPipeWrite("echo Hello" & @CRLF)

MsgBox(0, "Debug", "end of test")
ProcessClose($hCMD)

Func _StdInPipeWrite($sMessage)

    ; ===============================================================================================================================
    ; This function writes a message to the pipe
    ; ===============================================================================================================================
    Local $iWritten, $iBuffer, $pBuffer, $tBuffer

    $iBuffer = StringLen($sMessage) + 1
    $tBuffer = DllStructCreate("char Text[" & $iBuffer & "]")
    $pBuffer = DllStructGetPtr($tBuffer)
    DllStructSetData($tBuffer, "Text", $sMessage)
    If Not _WinAPI_WriteFile( _
            $hPipe, _ ; ...... Handle to the file to be written
            $pBuffer, _ ; .... Pointer to the buffer containing the data to be written
            $iBuffer, _ ; .... Number of bytes to be written to the file
            $iWritten, _ ; ... The number of bytes written
            0 _ ; ............ [optional] A $tagOVERLAPPED structure or a pointer to it
            ) Then
        ConsoleWrite("WriteMsg: _WinAPI_WriteFile failed" & @CRLF & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("WriteMsg: write OK" & @CRLF & _WinAPI_GetLastErrorMessage() & @CRLF)
    EndIf
EndFunc   ;==>_StdInPipeWrite

 

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

... it seems that at the end of each command sent to cmd, there is a request pending from the pipe asking for something to the 'caller'. (something like'do you need to send more data?') I have seen that sending an @CRLF as the first character of the next sending allows you to send further commands to cmd via NomedPipe. As if the @CRLF accepts the default answer which I suppose is "yes, I have more data to send, stay connected" (?) ... or something similar.
However, although this workaround works (see line 30 in listing), it is not clear to me what happens under the hood.
if anyone has a clearer idea than mine, i would be glad to know it.
Thanks for any suggestions

#include <NamedPipes.au3>
#include <WinAPI.au3>

; Creates an instance of a named pipe
Global $sPipeName = "\\.\pipe\pipename"
Global $hPipe = _NamedPipes_CreateNamedPipe($sPipeName, 1, 2)
Global $sPrefix = ''

MsgBox(0, "Debug", "Pipe created. Now open a CMD")

; run a cmd with only StdIn redirected (StdIn data incoming from a pipe)
Global $hCMD = Run(@ComSpec & " /K cmd /K < " & $sPipeName & @CRLF, "c:\") ; ok? (maybe a better way?)

_StdInPipeWrite("@echo off" & @CRLF)
MsgBox(0, "Debug", "Result: @error:" & @error & @TAB & "$hCMD:" & $hCMD & @CRLF & "now Send a command to the cmd via a NamedPipe")
_StdInPipeWrite("cls & @Echo. & Echo Hello from AutoIt & Echo ----------------- & @Echo." & @CRLF)

MsgBox(0, 'Debug', "further commands" & @CRLF & "now send command 'dir c:\windows'")
$sMessage = "Dir c:\windows" & @CRLF
_StdInPipeWrite($sMessage)

MsgBox(0, 'Debug', "send another command)" & @CRLF & "now send command 'cls & HELP'")
_StdInPipeWrite("cls & HELP" & @CRLF)

MsgBox(0, "Debug", "end of test")
ProcessClose($hCMD)

Func _StdInPipeWrite($sMessage)

    $sMessage = @CRLF & $sMessage ; <----------- Workaround

    ; ===============================================================================================================================
    ; This function writes a message to the pipe
    ; ===============================================================================================================================
    Local $iWritten, $iBuffer, $pBuffer, $tBuffer

    $iBuffer = StringLen($sMessage) + 1
    $tBuffer = DllStructCreate("char Text[" & $iBuffer & "]")
    $pBuffer = DllStructGetPtr($tBuffer)
    DllStructSetData($tBuffer, "Text", $sMessage)
    If Not _WinAPI_WriteFile( _
            $hPipe, _ ; ...... Handle to the file to be written
            $pBuffer, _ ; .... Pointer to the buffer containing the data to be written
            $iBuffer, _ ; .... Number of bytes to be written to the file
            $iWritten, _ ; ... The number of bytes written
            0 _ ; ............ [optional] A $tagOVERLAPPED structure or a pointer to it
            ) Then
        ConsoleWrite("WriteMsg: _WinAPI_WriteFile failed" & @CRLF & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("WriteMsg: write OK" & @CRLF & _WinAPI_GetLastErrorMessage() & @CRLF)
    EndIf
EndFunc   ;==>_StdInPipeWrite

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Posted (edited)

Hi, i was just playing with this, searching for the solution.

Then i saw your workaround, and then it clicked !

 

If you change the $iBuffer = StringLen($sMessage) + 1

and remove the +1, it should work. (from your 1st post)

 

Logical explanation:

Looks like the cmd is waiting for data, but got nothing, because the string length was too short.

Edited by Dan_555

~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

Share this post


Link to post
Share on other sites
Posted (edited)

Thanks @Dan_555 

Your explanation clarifies!
I was completely out of the way.
my hypothesis was due to the fact that among the different requests, on the cmd appears a string that says "Ancora?" that is the Italian word for "More?" hence my (incorrect) thesis. (Sometimes things are simpler than it seems)
Thanks a lot for your solution! 👍

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By cruisepandey
      Hi There ! 
      I have a script here : 
       
      ;Launch CMD
      Run("C:\Windows\System32\cmd.exe")
      sleep(2000)
      $cmdHandle = WinActivate("C:\Windows\System32\cmd.exe")
      Sleep(2000)
      ;Sending document
      ControlSend($cmdHandle, "", "", "ftp" & @CRLF)
      ControlSend($cmdHandle, "", "", "open" & @CRLF)
      Sleep(2000)
      ControlSend($cmdHandle, "", "", "first command" & @CRLF)
      Sleep(2000)
      ControlSend($cmdHandle, "", "", "second-coomand" & @CRLF)
       
      first-command and second-command I can't provide cause it's internal. I have complied this .au3 file into an exe and it does the work. But I need to invoke this with Java. Java code I have tried is : 
       
          ProcessBuilder pb = new ProcessBuilder("C:\\Users\\username\\eclipse-workspace\\Examples\\src\\com\\own\\examples\\etc.exe");
          pb.start();
          Thread.sleep(5000);
       
      Through java it just launches the cmd and nothing happens after that. Please help !!
    • By Burgs
      Greetings,
        I would like to be able to write a script to send commands to the console for creation of Gstreamer pipelines.  I was thinking of something similar to this:
      Local $iPID = Run("C:\Windows\System32\cmd", "", @SW_MAXIMIZE) ;THIS OPENS THE CONSOLE...!!! if $iPID == 0 Then ConsoleWrite(@CRLF & "I DID NOT OPEN CMD...error: " & @error & @CRLF) if $iPID <> 0 Then ConsoleWrite(@CRLF & "I OPENED CMD...!!!" & @CRLF) $hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe") if $hCmd <> 0 Then WinActivate($hCmd) ;ensure command console is active... $sOutput = Send("cd C:\gstreamer\1.0\x86_64\bin" & @CRLF, $SEND_RAW) $sOutput = Send("gst-launch-1.0 videotestsrc ! autovideosink" & @CRLF, $SEND_RAW) Sleep(3000) ControlSend($hCmd, "", "", "exit" & @CR) EndIf ;$hCmd NOT "0"... I don't really know if this is the best way to open the console and send commands into it.  I'm also not sure about how to best catch any errors that may occur...likely this needs to be accomplished with the STDOUTREAD command however I've not had experience using it before and therefore would appreciate some advice that anybody may offer. 
      Basically I'm seeking guidance on how to best automate the opening of the console, sending lines of commands to be executed, and handling any potential errors in the execution of those commands...I thank you in advance.  Regards.
    • By MadhaN
      Hi all,
      I have a csv file as below, I wand to find srno from csv and send corresponding ip and pass to commend cmd prompt. 
      Please guide me to create script .
      srno,name,ip,pass
      1,name1,ip1,pass1
      2,name2,ip2,pass2
       
       
       
       
    • By nacerbaaziz
      Hi dear
      With this script you can print Unicode text in the CMD screen
      the script is  easy to use
      just you  write the text that contains Unicode in the first input
      and the script automatically reflect the code in the second input
       you can copy the text to the clipboard
      or you can try printing the text in the CMD window
      I apologize to everyone for colors and shape if not appropriate
      I'm a blind man and I do not see
      Thank you for your understanding
      Greetings to all of youCmdUtM.au3
    • By Kevin Finnegan
      Hi all,
      Long time lurker and now forum poster! I'm writing a relatively simple backup script for my firm that automates the copy, compression and organization of Leaver's data on one of our secured NAS systems. I personally found the best method to do this so far was to use 7zG.exe (GUI version of 7Zip which can use command-line too) and it functions quite well!
      I would like to retrieve more info on whether any warnings or errors happen in 7Zip during the backup, but I can't quite get my head around the syntax and switches for reading out, it seems any adjustment I make to the RunWait call's string seems to break the backup or give unexpected repercussions! Hopefully its something silly I'm doing as I don't code very often.
      Here is the working version:
      ; Compress the directories one by one in the zip using the listfile.... Local $iPID = RunWait(@ScriptDir & "\bin\7zG.exe a -mx" & $compressionQuality & " -v" & $compressSplitFileSize & " -wc:\temp " _ & $backupToLocation & "\" & $userDirectory & ".7z @bin\listfile.txt -x@bin\excludefile.txt", "", @SW_SHOWDEFAULT, $STDOUT_CHILD) Ultimately I would love to switch entirely to 7za.exe (standalone) so that I can read the progress percentage, current file being uploaded and any warnings or errors could be processed and output to the AutoIT script's GUI I've created rather than jumping in and out of two applications per se.
       
×
×
  • Create New...