Is it possible to intercept calls to console from another process?

走远了吗. 提交于 2019-12-23 03:52:48

问题


The situation is that I have program started through system() or CreateProcess().

Now, is it possible to do stuff as that program outputs data into console. I mean as the program outputs it. That is not wait for the end, gather data and then process it, but just in the moment that this external program calls console with data that it wants to print, and then get hold of that data, process it and output something else on the console.


回答1:


The easiest way is usually to start the program with _popen(your_program, "r");. That will return a FILE * you can read from, and what it reads will be whatever the child writes to its standard output. When you read EOF on that file, it means the child process has terminated. This makes it relatively easy to read and process the output from the child in real time.




回答2:


On Linux, create a named pipe:

system("mkfifo pipename")

Then open the pipe in the first program, and start the program with:

system("program > pipename")

I'm not sure how to do this on Windows.




回答3:


Call AllocConsole before creating child process, or use AttachConsole(ChildPID) function (in parent process). After that, you may use any ReadConsoleXXX or WriteConsoleXXX functions.



来源:https://stackoverflow.com/questions/10935745/is-it-possible-to-intercept-calls-to-console-from-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!