Home > Software engineering >  How to solve the problem of under Windows pipes blocked?
How to solve the problem of under Windows pipes blocked?

Time:09-22

The program is very simple:
Program A:
# include "stdio.h"
Int main ()
{
int index=0;
int i=0;
For (I=1; i <9988998; I++) {
Printf (" Current Count is % d \ n ", index++);

Sleep (10 * 999);
}
}

Would be stuck through pipeline access to stdout, investigate its reason, is because Windows stdout is fully buffered output, output only buffer is full, but I want to achieve A line will output A line, under the command line can be so, but the pipe has been not achieve them, search on the Internet A lot of code, though, asynchronous access, but also must to output after buffer is full, the question now is I can't modify A program, the above is just A example, the actual function more complicated than it, but the principle is same, also can't change the Windows command line to line buffer, everybody has what good method?

CodePudding user response:

Output a newline character, or brush the fflush function force can?

CodePudding user response:

Using PeekNamedPipe data query the pipeline, have read, no, don't read, main is to prevent the ReadFile congestion,

CodePudding user response:


Should be able to realize, similar to the back door binding the CMD rebound, the console input and output are taken over,

CodePudding user response:

reference 1st floor qq_36751214 response:
output a newline, or brush the fflush function force can?

I have no program a code, the code above is only for delay output function, actual output there is a delay, but the contents of the output and I write is not the same, it is useful to the data, I can do to modify the pipeline read program or modify the system registry

CodePudding user response:

Fyi:
 # pragma comment (lib, "user32") 
#include
#include
#include
Int main () {
SECURITY_ATTRIBUTES sa={0};
STARTUPINFO si={0};
PROCESS_INFORMATION PI={0};
HANDLE hPipeOutputRead=NULL;
HANDLE hPipeOutputWrite=NULL;
HANDLE hPipeInputRead=NULL;
HANDLE hPipeInputWrite=NULL;
BOOL bTest=0;
DWORD dwNumberOfBytesRead=0;
DWORD dwNumberOfBytesWrite=0;
CHAR szMsg [100].
CHAR szBuffer [256].

Sa. NLength=sizeof (sa);
Sa. BInheritHandle=TRUE;
Sa. The lpSecurityDescriptor=NULL;

//Create pipe for standard output redirection.
The CreatePipe (& amp; HPipeOutputRead,//read handle
& HPipeOutputWrite,//write handle
& Sa,//security attributes
0//number of bytes reserved for pipe - 0, the default
);

//Create pipe for standard input redirection.
The CreatePipe (& amp; HPipeInputRead,//read handle
& HPipeInputWrite,//write handle
& Sa,//security attributes
0//number of bytes reserved for pipe - 0, the default
);

//Make the child process use hPipeOutputWrite as standard out,
//and make sure it does not show on screen.
Si. Cb=sizeof (si);
Si. DwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
Si. WShowWindow=SW_HIDE;
Si. HStdInput=hPipeInputRead;
Si. HStdOutput=hPipeOutputWrite;
Si. HStdError=hPipeOutputWrite;

CreateProcess (
NULL, "CMD. Exe,"
NULL, NULL,
TRUE, 0,
NULL, NULL,
& Si, & amp; PI);

//Now that handles have had inherited, close it to be safe.
//You don 't want to read or write to them accidentally.
The CloseHandle (hPipeOutputWrite);
The CloseHandle (hPipeInputRead);

//Now test to capture the DOS application output by reading
//hPipeOutputRead. Could also write the to DOS application
//standard input by writing to hPipeInputWrite.
Sprintf (szMsg, "ver \ n");
WriteFile (
HPipeInputWrite,//handle of the write end of our pipe
& SzMsg,//address of buffer that send data
Strlen (szMsg),//the number of bytes to write
& DwNumberOfBytesWrite,//the address of the number of bytes read
NULL//non - overlapped.
);

While (TRUE)
{
BTest=ReadFile (
HPipeOutputRead,//handle of the read end of our pipe
& SzBuffer,//the address of the buffer that receives the data
256,//the number of bytes to read
& DwNumberOfBytesRead,//the address of the number of bytes read
NULL//non - overlapped.
);

if (! BTest) {
Sprintf (szMsg, "pipe. Error # % d reading", GetLastError ());
Printf (" % s ", szMsg);
break;
}

//do something with the data.
SzBuffer [dwNumberOfBytesRead]=0;//null terminate
Printf (" % s ", szBuffer);
If (' & gt; '==szBuffer] [dwNumberOfBytesRead - 1) break;
}

Sprintf (szMsg, "CHCP \ nexit \ n");
WriteFile (
HPipeInputWrite,//handle of the write end of our pipe
& SzMsg,//address of buffer that send data
Strlen (szMsg),//the number of bytes to write
& DwNumberOfBytesWrite,//the address of the number of bytes read
NULL//non - overlapped.
);

While (TRUE)
{
BTest=ReadFile (
HPipeOutputRead,//handle of the read end of our pipe
& SzBuffer,//the address of the buffer that receives the data
256,//the number of bytes to read
& nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related