It is possible to put a hook catching messages that are send to console by child process?

偶尔善良 提交于 2019-12-25 05:08:03

问题


So I start an ordinary console program and then create another process within it. Next the child process tries to write something onto console that is owned by parent process.

Is it possible to put a hook on this console so when child process tries to write some text I could do search and replace on this text and put it changed onto the console.

(I know it is possible with pipes, but here I ask specifically of doing it at the console level, because some programs behave differently if they see they are attached to pipes instead of console.)

Has SetWindowsHookEx something to do with my question ?


回答1:


Just to avoid confusion, windows messages aren't involved with this so hooking is entirely the wrong concept.

If you want to change the child processes data written to STDOUT, you'll need to create your own pipes that you pass to the process when creating it. This allows you to read the data from it, change as you require then print to STDOUT yourself. There is no way to intercept data written direct to the console.




回答2:


So you want to install a winproc hook on the console? Sure, you can do it, first you have to get the HWND (read here) of the console window then you can call your SetWindowsHookEx() and hook it. Do note that in your hook procedure , calling defwinproc will call the console's origional winproc, so if you don't want to modify the entire behavior of the console your in the clear.



来源:https://stackoverflow.com/questions/11171785/it-is-possible-to-put-a-hook-catching-messages-that-are-send-to-console-by-child

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