output from a process: StandardOut, or Pipes

G

ghandi

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.
 
J

Jeroen Mostert

ghandi said:
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.
 
G

ghandi

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.
 
G

ghandi

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

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.
 
G

ghandi

[...]
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top