windows:是否可以将文本文件转储(直接)到命名管道中

发布于 2024-08-13 23:20:15 字数 212 浏览 6 评论 0原文

我有一个程序获取输入的设置,如下所示:

1)用户在命令提示符中输入命令

2)命令提示符中的文本写入命名管道

3)管道另一侧的进程正在读取输入解析并执行命令

我希望能够在文本文件中存储一组命令,然后获得文本文件的命名管道提要。

有什么方法可以将管道和锉刀连接在一起吗?或者我是否需要读取文本文件并将其分成几行,然后将其一一写入管道

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 do I need to read the text file and split it into lines which I will write to the pipe one by one

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽手叙旧 2024-08-20 23:20:15

如果您使用命名管道,

如果您查看 这个,你可以看到他们使用普通的CreateFile来打开管道,看看那个,看来你不能重定向但你必须读写,至少与API是一样的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")); 
} 

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")); 
} 
夏见 2024-08-20 23:20:15

您应该能够使用TYPE

TYPE "filename" | myprogram.exe

尽管如果您明确需要命名管道,或者需要通过管道连接到已经运行的进程,则这将不起作用。

不过在这种情况下,您可以使用存根程序将其 STDIN 写入命名管道。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文