5

If I know the process identifier, is there some mechanism in .net (or pinvoke as last resort) to capture/redirect the standard output of a process that I did not launch?

Suraj
  • 35,905
  • 47
  • 139
  • 250

3 Answers3

1

You could AttachConsole your process to the console of the other process, and then use the usual console I/O functions to read and write from it.

Remember to use FreeConsole to detach from the process' current console first.

CesarGon
  • 15,099
  • 6
  • 57
  • 85
  • @CesarGon - I gave it a whirl but AttachConsole returned false (marshaled from 0) – Suraj Jun 06 '11 at 23:22
  • @SFun28: Try `GetLastError` to find out why it failed. – CesarGon Jun 06 '11 at 23:26
  • @CesarGon - it returns 5. Per the pinvoke site, I'm using Marshal.GetLastWin32Error to get that number. – Suraj Jun 06 '11 at 23:32
  • @SFun28 It's an 'Access is denied' error. If you use `GetLastError`, you can see error codes [here](http://msdn.microsoft.com/en-us/library/ms681382(v=vs.85).aspx). See [this link](http://www.pinvoke.net/default.aspx/kernel32.attachconsole) for more information on the error. – Centro Jun 06 '11 at 23:38
  • @Centro - thanks for the links. I followed the code-sample but doesn't work for me. I think I'll look for another approach. – Suraj Jun 07 '11 at 00:31
  • @SFun28: If you read the documentation page for AttachConsole, you'll see that error 5 (access denied) is returned if the process is already attached to a console. You need to detach from the current console first through FreeConsole (http://msdn.microsoft.com/en-us/library/ms683150(v=vs.85).aspx) – CesarGon Jun 07 '11 at 00:37
  • @SFun28 From MSDN: _" If the calling process is already attached to a console, the error code returned is ERROR_ACCESS_DENIED (5)."_ So your calling process is already attached to a console. Try first call [FreeConsole Function](http://msdn.microsoft.com/en-us/library/ms683150(v=vs.85).aspx) and `AttachConsole` after that. – Centro Jun 07 '11 at 00:39
  • @Centro/CesarGon - one step further. Called FreeConsole and now I can call AttachConsole with no error. Unfortunately the output of the process whose console I'm attaching is not appearing in standard output of the running process. maybe its a .net thing (the calling process is .net, not the process I'm trying to attach to)? – Suraj Jun 07 '11 at 00:49
0

As far as I know you can't do this in .NET, you can redirect outputs for processes you start only via ProcessStartInfo Class.

Also as far as I know you cannot redirect I/O externally at all after a process has started, only the process itself can do it.

You can attach the calling process to the console of the specified process via AttachConsole Function.

Centro
  • 3,892
  • 2
  • 25
  • 31
  • I've tried AttachedConsole, but doesn't seem to work. please see the thread under @CesarGon's answer. – Suraj Jun 06 '11 at 23:33
0

EDIT: Looks like it indeed does not work, as is being pointed out by another answer in this thread.

I have not personally tried it, but it seems, since the answer was accepted, that this works.

Community
  • 1
  • 1
Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • another answer in that post points out that the solution does not work – Suraj Jun 08 '11 at 02:25
  • @SFun28 - Thanks for pointing this out. I should have read the whole thread first (I figured that the accepted answer looked suspiciously simple ;-) – Christian.K Jun 08 '11 at 10:16