Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

output from a process: StandardOut, or Pipes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ghandi - 23 Mar 2008 00:50 GMT
I am trying to redirect output from a process that I didn't create.  I
see that the process class has the ability to redirect this through
the StandardOutput property.  I also noticed that .net 3.5 has pipes,
so I assume that I should be able to use those new classes as well.
What's the difference?  I also noticed that there is no example (at
least the I could find) that uses the new pipe classes with a process
that already exists.  How would you use the new pipe classes to do
this?
Thanks.
Peter Duniho - 23 Mar 2008 03:59 GMT
> I am trying to redirect output from a process that I didn't create.  I
> see that the process class has the ability to redirect this through
[quoted text clipped - 4 lines]
> that already exists.  How would you use the new pipe classes to do
> this?

I admit I'm not familiar with the .NET implementation of "pipes", but I  
assume it's there as a way of supporting named pipes, which is a  
inter-process communications technique.  Note that it's a _communications_  
technique; it relies on both ends being aware of the pipe and  
communicating over it.

Assuming the .NET implementation can expose a pipe as a Stream, then you  
could in fact probably hook a pipe up to the StandardOutput,  
StandardInput, or StandardError property, but the communications would  
still only be one-way.  It's also not something you'd need to do if all  
you want is for your own process to interact with some process it  
created.  It might be useful if you have some other application that is  
supporting that named pipe and you want to somehow connect the two, but  
otherwise the two aren't really related to each other.

Finally, as far as I know, you can only redirect the i/o from a process  
that you did create.  You have to set the ProcessStartInfo members to  
enable the redirection before you create the process, and you can only do  
that if you're the one creating the process.

Pete
Jeroen Mostert - 23 Mar 2008 11:12 GMT
> I am trying to redirect output from a process that I didn't create.

You can't. Output can only be redirected before the process starts, or by
the process itself. This is not a .NET limitation; it's the way the Win32
API works.

If you like dirty hacks, you can always inject a thread into the remote
process to do the redirecting, but a serious rethinking is a better idea.
And if you really do want to muck around with thread injection, I strongly
suggest doing it from unmanaged code, as trying to do it from .NET will only
invite more headaches.

Signature

J.

ghandi - 25 Mar 2008 05:18 GMT
Sorry guys.  When I said create, I meant wrote the code for, or
developed in house, not spawned or forked.  So I gather from the
comments, the best bet is still to use the StandardOut property.
Peter Duniho - 25 Mar 2008 07:50 GMT
> Sorry guys.  When I said create, I meant wrote the code for, or
> developed in house, not spawned or forked.  So I gather from the
> comments, the best bet is still to use the StandardOut property.

Maybe you could rephrase the whole question then.  As it stands, the only  
interpretation of "create" that really makes sense in your original post  
is that your code started another process (or rather, didn't start another  
process, since you wrote "didn't create").

Telling us that you didn't write the code for the other process doesn't  
seem to change the overall gist of the message (there's still the  
implication that your code also didn't start the other process), so it's  
hard to see how we'd change the answer or verify that the answers we've  
provided are still valid given that you feel you miscommunicated something  
in your original post.

If you could explain more clearly what it is you're trying to do, and why  
it is you think there's a possibility that either using the StandardOutput  
property or named pipes might address that goal, that might help us  
understand your question better.

Thanks,
Pete
ghandi - 26 Mar 2008 05:43 GMT
On Mar 25, 12:50 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > Sorry guys.  When I said create, I meant wrote the code for, or
> > developed in house, not spawned or forked.  So I gather from the
[quoted text clipped - 19 lines]
> Thanks,
> Pete

What I would like to do is to get the output, input, and error from a
process I start (namely Powershell).  I have tried three ways:
1. I start the process with the Process class and redirecting stdout,
stdin, and stderr using a handler to the OutputDataReceived Event, the
ErrorDataReceived Event, and a StreamWriter from the StandardInput
property.  This works with cmd.exe, but powershell only return the
first two lines and the prompt appears on the regular powershell
console.  The biggest down side to this way is that since I am
starting the process using a user's name and password, the process
class always starts a console for me.  I really want this program to
run as a service and do not want a console shown.  Plus, I don't seem
to be getting all the output and error.
2. I start the process with the win32 API call CreateProcessAsUser and
try to get the output, input, and error from a handle obtained by
creating a new AnonymousPipeServerStream for each, passing the handle
I receive from
AnonymousPipeServerStream.SafeFileHandle.DangerousGetHandle to the
start information of the process, and then try to read and write from
the AnonymousPipeServerStream.
3. I start the process with the win32 API call CreateProcessAsUser,
create a pipe for stdin, stdout, and stderr with another win32 API
call CreatePipe, and pass the SafeFileHandle received from that to a
FileStream that I try to read and write from.  With this method, I can
hide the window, but as soon as I call ReadLine() on the stdout
stream, it seems to return nothing.  I step through with the debugger,
and it just goes on with the rest of the code without giving me any
output.  The same thing happens with way #2.
So to sum up, I can't get the redirected output and input from
powershell and I'm just grasping at straws to find something else to
try.  Does that clarify things?  Do you need to see some code or is
the description enough?
Thanks.
Peter Duniho - 26 Mar 2008 07:38 GMT
> [...]
> So to sum up, I can't get the redirected output and input from
> powershell and I'm just grasping at straws to find something else to
> try.  Does that clarify things?  Do you need to see some code or is
> the description enough?

Your description does clarify things quite a lot, thanks.  Unfortunately,  
between the fact that you're trying to manage PowerShell and having  
problems specific to it, and the fact that you're trying to run this as a  
service and possibly having problems specific to that as well, what you're  
trying to do is outside my own experience.

Ultimately, if someone is going to be able to help you, you probably will  
have to post a concise-but-complete code sample that reliably demonstrates  
the problem.  But I hesitate to ask for it myself, since if I were able to  
answer the question at all, it would involve a fair amount of time on my  
part just getting oriented with respect to what you're doing.

I think that some of the other people here to know more about the specific  
kinds of things you're doing though, and hopefully at least one will not  
only notice the thread but also have time to address it.

Sorry I couldn't offer more specific advice.  However, I'll at least take  
credit for soliciting a more descriptive problem statement.  :)

Pete
ghandi - 26 Mar 2008 22:53 GMT
On Mar 26, 12:38 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > [...]
> > So to sum up, I can't get the redirected output and input from
[quoted text clipped - 22 lines]
>
> Pete

Thanks for the response.  I'll try to come up with some complete-but-
concise code for this.  That might take a bit.
Thanks again for the time.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.