How to redirect the input and output of cmd to the pipeline (this method can be used to write the command into cmd and capture the output)

Source: Internet
Author: User

//////////////////////////////////////// /////////////////////////////////
////
// ------- How to redirect the input and output of cmd to the pipeline -------//
// ------- This method can be used to write commands into cmd and capture the output -------//
////
//////////////////////////////////////// ////////////////////////////////

// --------- Chendana's implements, modified by chenth ---------//
# Include <windows. h>
# Include <iostream>
# Include <string>

Using namespace STD;
Handle hread1, hwrite1; // read handle, write handle
Handle hread2, hwrite2; // read handle, write handle
Handle hcmd;
Int createcmd (handle & hreadpipe1, handle & hwritepipe1, // read/write handle of anonymous pipeline 1
Handle & hreadpipe2, handle & hwritepipe2, // read/write handle of anonymous pipeline 2
Handle & hprocess) // The cmd. EXE process handle created
{
Int ret;
Security_attributes SA;
SA. nlength = 12;
SA. lpsecuritydescriptor = 0;
SA. binherithandle = true;
Ret = createpipe (& hreadpipe1, & hwritepipe1, & SA, 0 );
If (ret = 0)
{
Return-1;
}
Ret = createpipe (& hreadpipe2, & hwritepipe2, & SA, 0 );
If (ret = 0)
{
Return-1;
}

Startupinfo Si;
Zeromemory (& Si, sizeof (SI ));
Si. dwflags = startf_useshowwindow | startf_usestdhandles;
Si. wshowwindow = sw_show; // sw_hide

// Create a process and redirect standard input/output

// Set the standard input to hreadpipe2
// Set the standard output and error output to hwritepipe1
// For the communication logic, see
Si. hstdinput = hreadpipe2;
Si. hstdoutput = Si. hstderror = hwritepipe1;
Char character line [] = "cmd ";
Process_information processinformation;
Ret = CreateProcess (null, (lpstr) using line, null, & Si, & processinformation );
If (ret = 0)
{
Return-1;
}
Hprocess = processinformation. hprocess;
Return 0;
}

//************************************** **************************************
// This function is used to write the created Pipeline
Int writecmd (char * cmdbuffer, handle hwritepipe2)
{
DWORD lbyteswrite;
Char 1__tmp [1024] = "";
Printf ("writing.../N ");
Sprintf_s (pai_tmp, 1024, "% S % s", cmdbuffer, "/R/N ");
If (! Writefile (hwritepipe2, (lptstr) pai_tmp, strlen (pai_tmp), & lbyteswrite, null ))
{
Return-1;
}
Return 0;
}
//************************************** **************************************
// This function is used to read the created Pipeline
Int readcmd (handle hreadpipe1, string & cmdresult ){
Bool ret;
DWORD lbytesread;
Char * buffer = new char [1024];
While (true)
{
Printf ("reading.../N ");
Sleep (99 );
Memset (buffer );
Ret = ReadFile (hReadPipe1, buffer, 1023, & lBytesRead, 0 );
If (ret = 0)
{
Delete buffer;
Return-1;
}
Buffer [lBytesRead] = '/0 ';
CmdResult + = buffer;
If (buffer [lBytesRead-1] = '> ')
{
Delete buffer;
Return 0;
}
}
Delete buffer;
Return 0;
}
//************************************** ***************************************
// This function is used to close the MPs queue.
Void closecmd (handle hreadpipe1, handle hwritepipe1, // read/write handle of anonymous pipeline 1
Handle hreadpipe2, handle hwritepipe2, // anonymous pipeline 2 read/write handle
Handle hprocess) // The cmd. EXE process handle created
{
Closehandle (hreadpipe1 );
Closehandle (hwritepipe1 );
Closehandle (hwritepipe2 );
Closehandle (hreadpipe2 );
TerminateProcess (hProcess, 0 );
}
//************************************** ***************************************
// This function is used to clear MPs queue data.
Void EmptyPipe (HANDLE hReadPipe1 ){
BOOL ret;
DWORD bytesRead;
Char * buffer = new char [1024];
While (true)
{
Memset (buffer );
Ret = PeekNamedPipe (hReadPipe1, buffer, 1024, & bytesRead );
If (bytesRead = 0 |! Ret)
{
Delete buffer;
Return;
}
ReadFile (hReadPipe1, buffer, bytesRead, & bytesRead, 0 );
}
}

// ------------------ Sample codes by chenth ------------------//
Int main (int argc, char * argv [])
{
String result;
DWORD dw;

If (CreateCMD (hRead1, hWrite1, hRead2, hWrite2, hCmd) <0)
{
Printf ("CreateCMD failed ");
Dw = GetLastError ();
Printf ("GetLastError --> % u/n", dw );
ExitProcess (dw );
Return-1;
}
If (WriteCMD ("dir", hWrite2) <0)
{
Printf ("WriteCMD failed ");
Dw = GetLastError ();
Printf ("GetLastError --> % u/n", dw );
ExitProcess (dw );
Return-1;
}
If (ReadCMD (hRead1, result) <0)
{
Printf ("ReadCMD failed ");
Dw = GetLastError ();
Printf ("GetLastError --> % u/n", dw );
ExitProcess (dw );
Return-1;
}
Printf ("************************************* * ********/n ");
Printf ("% s/n", result. c_str ());
Printf ("************************************* * ********/n ");
CloseCMD (hRead1, hWrite1, hRead2, hWrite2, hCmd );
Return 0;
}

 

// It is very important to set the encoding to "Multi-byte character set (MBCS)" instead of UNICODE in IDE! I spent a long time on this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.