重发监视控制台程序输出的问题
老板让我做个远程监控制,还要监控其他进程的输出,昏,窗口中的输出用hook问题不大,但控制台进程就比较麻烦。
查了好多资料,距离解决已经不远了。
我的解决方法是,先列出系统的进程快照,让用户选择进程的标识符。
然后,直接用FreeConsole()释放掉监控进程的,然后再把监控进程和被监控进程的
控制台绑定上,这一步用的是AttachConsole(DWORD dwProcessId)函数,VC7里有,VC6里只能到kernal.dll去找了.这样,他们共用控制台了,于是几可以用GetStdHandle(STD_INPUT_HANDLE)得到当前控制台的输出句柄,最后用ReadConsoleOutput()就可以直接读出另外一个进程的在控制台的输出了。
有两个问题需要解决:
1,对于高手可能这不是什么大问题
HANDLE hStdout;
SMALL_RECT srctReadRect;
CHAR_INFO chiBuffer[160]; // [2][80];
COORD coordBufSize;
COORD coordBufCoord;
BOOL fSuccess;
// Set the source rectangle.
srctReadRect.Top = 0; // top left: row 0, col 0
srctReadRect.Left = 0;
srctReadRect.Bottom = 1; // bot. right: row 1, col 79
srctReadRect.Right = 79;
// The temporary buffer size is 2 rows x 80 columns.
coordBufSize.Y = 2;
coordBufSize.X = 80;
// The top left destination cell of the temporary buffer is
// row 0, col 0.
coordBufCoord.X = 0;
coordBufCoord.Y = 0;
// Copy the block from the screen buffer to the temp. buffer.
fSuccess = ReadConsoleOutput(
hStdout, // screen buffer to read from
chiBuffer, // buffer to copy into
coordBufSize, // col-row size of chiBuffer
coordBufCoord, // top left dest. cell in chiBuffer
&srctReadRect); // screen buffer source rectangle
if (! fSuccess)
MyErrorExit("ReadConsoleOutput");
这个是MSDN里的例子,现在我的想法是,用户按一次键之后,我让它向下滚动,下边是我写的部分,当然我是用循环实现的,在这里是个示例
srctReadRect.Top += 2; // top left: row 0, col 0
srctReadRect.Bottom += 2; // bot. right: row 1, col 79
coordBufCoord.Y = 0;
memset(chiBuffer,0,160*sizeof(CHAR_INFO));
fSuccess = ReadConsoleOutput(
hStdout, // screen buffer to read from
chiBuffer, // buffer to copy into
coordBufSize, // col-row size of chiBuffer
coordBufCoord, // top left dest. cell in chiBuffer
&srctReadRect); // screen buffer source rectangle
if (! fSuccess)
MyErrorExit("ReadConsoleOutput");
可是读出来的chiBuffer为什么没有变呢?哪里有问题呢?
2).这个问题估计很难解决
据说win32中对控制台有两种I/O操作级别,一种是WriteFile(HANDLE HstdIn),ReadFile(HANDLE HstdOut),其实就是以前的dos21中断中的i/o操作,另外一种低级别的WriteConsoleOutput()其实就是以前的dos10中断。
我上述的方法只能得到writeConsoleOutput()中的输出,不能得到WriteFile输出的结果,如果想得到WriteFile的输出,必须用匿名管道,管道我也用了,那边灌进去,这边却出不来。真受不了。一团烟雾。
有兴趣的朋友,可以来探讨探讨呀。
关于第二个问题,这里有个英文网址链接,大家看看,看我的理解对不对
http://homepages.tesco.net/~J.deBoynePollard/FGA/capture-console-win32.html
问题点数:40、回复次数:2Top
1 楼strip(阿飞 - Mozilla●CSDN●痛)回复于 2002-04-03 03:25:05 得分 40
关于第二种方法:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/prothred_4uus.asp
http://www.codeguru.com/misc/RedirectOutputToPipe.shtml
Top
2 楼strip(阿飞 - Mozilla●CSDN●痛)回复于 2002-04-03 03:26:15 得分 0
关于第一种方法,费时费力,没有研究过Top




