Archived

This topic is now archived and is closed to further replies.

Nervo

Creating a Pipe?

Recommended Posts

So I''ve made headway through my little telnet server application (C++) and the key to making it happen was using _popen to send commands that came over the network to a spawned dos emulator window with its output captured and sent back out over the network to the client side..essentially just what a telnet login should do. The only thing that is not so great about it is that when the command is output through _popen() in win32 it literally opens a window on the server side. I don''t know much about pipes at all, but I just wanted to know whether it is 1) Possible to create a pipe or some mechanism to send commands without popping up any windows and 2) what exactly should I be googling for to get it done if so. Thanks a bunch!

Share this post


Link to post
Share on other sites
I don''t know very much about pipes, but here''s a link to the MSDN page on pipes. That can give you a lot of detailed information.

Share this post


Link to post
Share on other sites
The windows API for creating and using text pipes is a pain in the ass. If you must use C++, try something like wxWindows (or wxWidgets, or whatever it's called now). You can open a process and read from its standard output and write to its standard input.

http://www.wxwindows.org

Check out the wxProcess class. That will be way easier than using the Win32 API, and then it will also be portable.

[edited by - Russell on April 3, 2004 11:44:41 PM]

Share this post


Link to post
Share on other sites
Thanks all for the suggestion and thank you as well Russell for the link, I''ll give it a shot if this doesn''t work out. I''m having to sketch diagrams to correlate some of these functions and their parameters to "pipes". No pain no gain with this slightly cryptic stuff.

Share this post


Link to post
Share on other sites
quote:
Original post by Nervo
Thanks all for the suggestion and thank you as well Russell for the link, I''ll give it a shot if this doesn''t work out. I''m having to sketch diagrams to correlate some of these functions and their parameters to "pipes". No pain no gain with this slightly cryptic stuff.
On the other hand, no pain, no pain!

Share this post


Link to post
Share on other sites
You will want to create two pipes if you want to redirect stdin and stdout. This will leave your program with four file handles - make sure they''re the right way around.

I noticed a common pitfall when I wrote a telnet daemon in windows 95 (some years ago).

Under Windows 9x file handles that aren''t inheritable can still be used as stdin / stdout for child processes. This is a bug, and doesn''t happen on NT (or NT based ones like win2k, XP etc).

So ensure that the file handles (the ones you want to pass to the child process) are inheritable. Unfortunately the handles won''t be by default, so you may need to duplicate the handles using DuplicateHandle (close the originals of course).

Other than that, it should work.

Oh yes, and on win9x, processes with redirected input / output can cause serious system stability issues (if they hang or do unexpected stuff). But who cares

Mark

Share this post


Link to post
Share on other sites
quote:
Original post by markr
You will want to create two pipes if you want to redirect stdin and stdout. This will leave your program with four file handles - make sure they''re the right way around.

I noticed a common pitfall when I wrote a telnet daemon in windows 95 (some years ago).

Under Windows 9x file handles that aren''t inheritable can still be used as stdin / stdout for child processes. This is a bug, and doesn''t happen on NT (or NT based ones like win2k, XP etc).

So ensure that the file handles (the ones you want to pass to the child process) are inheritable. Unfortunately the handles won''t be by default, so you may need to duplicate the handles using DuplicateHandle (close the originals of course).

Other than that, it should work.

Oh yes, and on win9x, processes with redirected input / output can cause serious system stability issues (if they hang or do unexpected stuff). But who cares

Mark


Hi.

I was able to pull it off without any issues, which is just great! I''m using XP so there wasn''t any issues like you cautioned with 9x.

MSDN was an ok resource, but I found that going to this page had an example that I could easily understand and apply. If anyone wants to know how to create a pipe, especially for the dos emulator then the code is worth studying on that site. The important functions were CreatePipe(), CreateProcess(), and then to communicate data over the file is just WriteFile() and ReadFile().

Share this post


Link to post
Share on other sites
A little help please. I got the pipe to work and it outputs a command fine, but it doesn't exit the loop I created after the output is sent. I think after all is output it might be hanging on a subsquent ReadFile call. I'm using anonymous pipes as directed by SiCrane's linked article on MSDN. Any tips?


while(true)
{
if(!ReadFile(read_stdout,&c[0],1,&blah,NULL) || blah ==0) break;
send(*iter,&c[0],1,NULL);
}


[edited by - nervo on April 4, 2004 10:37:17 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Nervo
A little help please. I got the pipe to work and it outputs a command fine, but it doesn''t exit the loop I created after the output is sent. I think after all is output it might be hanging on a subsquent ReadFile call. I''m using anonymous pipes as directed by SiCrane''s linked article on MSDN. Any tips?


while(true)
{
if(!ReadFile(read_stdout,&c[0],1,&blah,NULL) || blah ==0) break;
send(*iter,&c[0],1,NULL);
}


[edited by - nervo on April 4, 2004 10:37:17 PM]


You could run it through a debugger and find out where it actually hangs. Then you wouldn''t have to guess

Share this post


Link to post
Share on other sites