windows: is it possible to dump (direct) a text file into a named pipe
I have a setup where a program gets its input like so:
1) user enters a command in a command prompt
2) the text from the command prompt is written to a named pipe
3) a process on the other side of the pipe is reading the input parse and execute the command
I would like to have the ability to store a set of commands in a text file and then have the named pipe feed of the text file.
is there some way to chine the pipe and the file together? or d开发者_如何学编程o I need to read the text file and split it into lines which I will write to the pipe one by one
If you use named pipe it might be possible,
if you take a look at this, you can see they use plain CreateFile
to open the pipe, taking a look at that, it seems you cannot redirect but you have to read and write, at least with the API is the same ReadFile
WriteFile
.
void WriteToPipe(void)
// Read from a file and write its contents to the pipe for the child's STDIN.
// Stop when there is no more data.
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
for (;;)
{
bSuccess = ReadFile(g_hInputFile, chBuf, BUFSIZE, &dwRead, NULL);
if ( ! bSuccess || dwRead == 0 ) break;
bSuccess = WriteFile(g_hChildStd_IN_Wr, chBuf, dwRead, &dwWritten, NULL);
if ( ! bSuccess ) break;
}
// Close the pipe handle so the child process stops reading.
if ( ! CloseHandle(g_hChildStd_IN_Wr) )
ErrorExit(TEXT("StdInWr CloseHandle"));
}
You should be able to use TYPE
:
TYPE "filename" | myprogram.exe
Though this won't work if you explicitly require a named pipe, or need to pipe into an already-running process.
Though in that case, you could use a stub program which writes its STDIN to the named pipe.
精彩评论