在另一个进程中生成命令提示符并在Windows上发送/接收命令(spawning a command prompt in a different process and sending/receivi

编程入门 行业动态 更新时间:2024-10-23 20:22:15
在另一个进程中生成命令提示符并在Windows上发送/接收命令(spawning a command prompt in a different process and sending/receiving commands on Windows)

我手头有一个问题,需要我将命令提示符作为一个不同的进程生成并向它发送一些命令并捕获/解析命令输出。 这种交互需要采用父子进程的形式,其中所有命令都可以放在ruby文件中,并且在运行ruby文件时,命令被发送到控制台(命令提示符)并从中接收输出并在ruby脚本中处理。

我将遵循的一般逻辑是:

通过使用fork生成不同的进程并获取进程ID 获取流程的流 写入进程的输入流并从输出流中读取。

我使用的环境是安装了Ruby 1.9.2的Windows XP机器。 我下载了这里找到的win32-process库。 通过使用该库,我可以按如下方式执行步骤1

require 'win32/process' APP_NAME = "C:\\Windows\\system32\\cmd.exe" process_info = Process.create(:app_name => APP_NAME, :creation_flags => Windows::Process::CREATE_NEW_CONSOLE, :process_inherit => false, :thread_inherit => true, :cwd => "C:\\" )

由于win32-process库基于在Windows上使用进程和线程,我尝试通过MSDN帮助。 在阅读创建控制台文章时,我发现可以使用GetStdHandle方法获取输入和输出流的句柄。 但是,我无法在win32-process中的任何地方找到此方法。

有人可以就如何进行第2步和第3步提供一些指导吗?

另外,还有其他方法可以用来解决手头的问题吗?

此外,我想了解更多关于进程间通信或一般产生和分析过程的信息,那么有人可以告诉我一些好的参考资料,我可以在哪里学习它们吗?

提前致谢

I have a problem at hand which requires me to spawn a command prompt as a different process and send some commands to it and capture/parse the command output. This interaction needs to be in the form of a parent-child process where say all the commands can be put in a ruby file and upon running the ruby file, the commands are sent to the console(command prompt) and output is received from it and processed in the ruby script.

The general logic which I would follow is:

Spawn a different process by using a fork and get a process id Obtain streams for the process Write to the input stream of the process and read from the output stream.

The environment which I am using is Windows XP machine with Ruby 1.9.2 installed on it. I downloaded the win32-process library found over here. By using that library, I could do step 1 as follows

require 'win32/process' APP_NAME = "C:\\Windows\\system32\\cmd.exe" process_info = Process.create(:app_name => APP_NAME, :creation_flags => Windows::Process::CREATE_NEW_CONSOLE, :process_inherit => false, :thread_inherit => true, :cwd => "C:\\" )

Since the win32-process library is based on using processes and threads on windows, I tried to go through the MSDN help for it. While reading the Creation of a Console article, I found that the GetStdHandle method could be used to get the handles to the input and output streams. But, i could not find this method implemented anywhere in win32-process.

Can someone provide me with some guidance on how to proceed with steps 2 and 3?

Also, is there any other way which can be used to solve the problem at hand?

Also, I would like to learn more about inter-process communication or in general spawning and forking of processes, so can somebody please tell me some good references where I could study them?

Thanks in advance

最满意答案

这里有一个在windows中使用IO.popen的例子,如果它与stdlib一起使用,则不使用gems

IO.popen("other_program", "w+") do |pipe| pipe.puts "here, have some input" pipe.close_write # If other_program process doesn't flush its output, you probably need to use this to send an end-of-file, which tells other_program to give us its output. If you don't do this, the program may hang/block, because other_program is waiting for more input. output = pipe.read end # You can also use the return value from your block. (exit code stored in $? as usual) output = IO.popen("other_program", "w+") do |pipe| pipe.puts "here, have some input" pipe.close_write pipe.read end

Here an example using IO.popen in windows, imo if it works with the stdlib don't use gems

IO.popen("other_program", "w+") do |pipe| pipe.puts "here, have some input" pipe.close_write # If other_program process doesn't flush its output, you probably need to use this to send an end-of-file, which tells other_program to give us its output. If you don't do this, the program may hang/block, because other_program is waiting for more input. output = pipe.read end # You can also use the return value from your block. (exit code stored in $? as usual) output = IO.popen("other_program", "w+") do |pipe| pipe.puts "here, have some input" pipe.close_write pipe.read end

更多推荐

本文发布于:2023-07-22 18:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   命令提示符   进程   命令   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!