来自运行进程 Win32Api 的管道输出(stdout) [英] Pipe output(stdout) from running process Win32Api

查看:33
本文介绍了来自运行进程 Win32Api 的管道输出(stdout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 windows api 从已经运行的进程中获取(或管道)输出.

I need to get (or pipe) the output from a process that is already running, using the windows api.

基本上我的应用程序应该允许用户选择一个窗口来输入输入,并且所有输入都将显示在控制台中.稍后我还将研究如何在 stderr 上获取管道.

Basically my application should allow the user to select a window to pipe the input from, and all input will be displayed in a console. I would also be looking on how to get a pipe on stderr later on.

重要提示:我没有使用 CreateProcess() 或其他方式启动进程.进程已经在运行,我只有进程的句柄(从 GetWindowThreadProcessId() 返回).

Important: I did not start the process using CreateProcess() or otherwise. The process is already running, and all I have is the handle to the process (returned from GetWindowThreadProcessId()).

推荐答案

无论你想做什么,你都做错了.如果您正在与拥有源代码的程序交互,请为您的 IPC 创建一个定义的接口:创建一个套接字、一个命名管道、Windows 消息传递、共享内存段、COM 服务器,或者任何您喜欢的 IPC 机制.不要试图将 IPC 移植到一个不打算做 IPC 的程序上.

Whatever you're trying to do, you're doing it wrong. If you're interacting with a program for which you have the source code, create a defined interface for your IPC: create a socket, a named pipe, windows messaging, shared memory segment, COM server, or whatever your preferred IPC mechanism is. Do not try to graft IPC onto a program that wasn't intending to do IPC.

您无法控制该进程的标准输出是如何设置的,也不是您可以搞砸的.它由其父进程创建并传递给子进程,从那时起,它由子进程控制.你不要进去换别人家的地毯房子.

You have no control over how that process's stdout was set up, and it is not yours to mess with. It was created by its parent process and handed off to the child, and from there on out, it's in control of the child. You don't go in and change the carpets in somebody else's house.

甚至不要考虑进入该过程,尝试 CloseHandle 它的 stdout,以及 CreateFile 一个指向管道的新 stdout.这是灾难的秘诀,会导致古怪的行为和不可能的"崩溃.

Do not even think of going into that process, trying to CloseHandle its stdout, and CreateFile a new stdout pointing to your pipe. That's a recipe for disaster and will result in quirky behavior and "impossible" crashes.

即使你可以做你想做的事,如果两个程序这样做会发生什么?

Even if you could do what you wanted to do, what would happen if two programs did this?

这篇关于来自运行进程 Win32Api 的管道输出(stdout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆