Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c++ - Grab Program's Console Output

I am creating a network administration program for a company I'm currently doing some contract work with (irrelevant I suppose)

As part of the program, I would like to have a console/telnet(type) clone. I have a working directory system, where I can essentially read all files in directories, and change directories. I also have an easy FTP protocol set up. However I would like to arbitrarily speak to different programs with the standard input/output procedures programmatically. I'm not opposed to DLL injection techniques, and other methods, as this is a benign program. If possible it would be awesome if I could just directly use the console easily. (system() would possibly work, however this doesn't allow continued communication or reading back of output)

I'm thinking so far of grabbing the PATH environment vars and utilizing that for std commands (ipconfig, netstat, etc) My networking library makes communication easy, but I'm just not sure how to interface with the program's console...

TL;DR:

Are there any predefined ways of standard console communication between programs?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As Jerry mentioned, what you're looking for is a pipe (CreatePipe). Two of them actually - one for input and one for output. You then create the new process with them set as stdin/stdout/stderr in STARTUPINFO passed to CreateProcess. You can then use WriteFile, ReadFile, PeekNamedPipe and friends to communicate.

A complete example is available on MSDN:

http://msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx

And make sure you don't fall into these common pitfalls (which luckily Raymond Chen just happened to document recently):

PS, this is sooooo much easier in Python with the subprocess module.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...