Redirecting stderr/stdout

Discussion in 'Microsoft C# .NET' started by zaperaj@gmail.com, Apr 9, 2012.

  1. Guest

    I have a C# application in which I'm trying to redirect some text written on to stderror. The text which is written to stderror is actually written bycalling a function that is present in some other dll. I have used CreatePipe, SetStdHandle and ReadFile (in that order) to achieve the mentioned functionality. While I am able to redirect any text that is written in the sameC# file using Console.SetError, the text that is written to stderr by the other dll still gets written on the native stderr and is not redirected.
    Additionally, I'm not executing a new process and hence will not be able tochange its start info.
    Is there any way to catch/redirect this text?
    Also, I have read on the net some people mentioning about consoles being different for managed (in this case, my C# application) and unmanaged (in this case, where the dll might be using win32 functions to write to stderror) codes. If this is indeed the issue here, can it be overcome?
    Thanks.
     
    , Apr 9, 2012
    #1
    1. Advertisements

  2. On 09.04.12 06.27, wrote:
    > I have a C# application in which I'm trying to redirect some text written on to stderror. [...]
    > I have used CreatePipe, SetStdHandle and ReadFile (in that order) to achieve the mentioned functionality.
    > While I am able to redirect any text that is written in the same C# file using Console.SetError,
    > the text that is written to stderr by the other dll still gets written on the native stderr and is not redirected.
    > Additionally, I'm not executing a new process and hence will not be able to change its start info.


    AFAIK SetStdHandle applies only to the .NET runtime. If the DLL uses
    another runtime or is not even .NET is has no effect. At least not if it
    is already running and has cached the I/O handles.

    > Is there any way to catch/redirect this text?


    You need to do the redirection at Kernel level with the Win32 API.

    > Also, I have read on the net some people mentioning about consoles being different for managed (in this case, my C# application) and unmanaged (in this case, where the dll might be using win32 functions to write to stderror) codes.


    Exactly.

    > If this is indeed the issue here, can it be overcome?


    You need to replace the I/O handles of your own process. But be careful.
    One process may hold more than one application apartment. Think of an
    application pool. So you may not hit the nail on the head.

    I would recommend to start the unmanaged code in a separate process and
    pass redirected I/O handles before creation. The separate process could
    still be a managed one using marshalling to the unmanaged DLL API. But
    it is a separate process which has replaced I/O handles from the first
    time. The concept is similar to plugin containers of modern browsers. Of
    course, the drawback is that you have to pass all data through IPC and
    maybe security issues. But yo have to concern with the latter anyway
    with unmanaged DLLs.


    Marcel
     
    Marcel Müller, Apr 9, 2012
    #2
    1. Advertisements

  3. Guest

    On Monday, April 9, 2012 3:05:42 PM UTC+5:30, Marcel Müller wrote:
    > On 09.04.12 06.27, wrote:
    > > I have a C# application in which I'm trying to redirect some text written on to stderror. [...]
    > > I have used CreatePipe, SetStdHandle and ReadFile (in that order) to achieve the mentioned functionality.
    > > While I am able to redirect any text that is written in the same C# file using Console.SetError,
    > > the text that is written to stderr by the other dll still gets written on the native stderr and is not redirected.
    > > Additionally, I'm not executing a new process and hence will not be able to change its start info.

    >
    > AFAIK SetStdHandle applies only to the .NET runtime. If the DLL uses
    > another runtime or is not even .NET is has no effect. At least not if it
    > is already running and has cached the I/O handles.
    >
    > > Is there any way to catch/redirect this text?

    >
    > You need to do the redirection at Kernel level with the Win32 API.
    >
    > > Also, I have read on the net some people mentioning about consoles being different for managed (in this case, my C# application) and unmanaged (inthis case, where the dll might be using win32 functions to write to stderror) codes.

    >
    > Exactly.
    >
    > > If this is indeed the issue here, can it be overcome?

    >
    > You need to replace the I/O handles of your own process. But be careful.
    > One process may hold more than one application apartment. Think of an
    > application pool. So you may not hit the nail on the head.
    >
    > I would recommend to start the unmanaged code in a separate process and
    > pass redirected I/O handles before creation. The separate process could
    > still be a managed one using marshalling to the unmanaged DLL API. But
    > it is a separate process which has replaced I/O handles from the first
    > time. The concept is similar to plugin containers of modern browsers. Of
    > course, the drawback is that you have to pass all data through IPC and
    > maybe security issues. But yo have to concern with the latter anyway
    > with unmanaged DLLs.
    >
    >
    > Marcel




    On Monday, April 9, 2012 3:05:42 PM UTC+5:30, Marcel Müller wrote:
    > On 09.04.12 06.27, wrote:
    > > I have a C# application in which I'm trying to redirect some text written on to stderror. [...]
    > > I have used CreatePipe, SetStdHandle and ReadFile (in that order) to achieve the mentioned functionality.
    > > While I am able to redirect any text that is written in the same C# file using Console.SetError,
    > > the text that is written to stderr by the other dll still gets written on the native stderr and is not redirected.
    > > Additionally, I'm not executing a new process and hence will not be able to change its start info.

    >
    > AFAIK SetStdHandle applies only to the .NET runtime. If the DLL uses
    > another runtime or is not even .NET is has no effect. At least not if it
    > is already running and has cached the I/O handles.
    >
    > > Is there any way to catch/redirect this text?

    >
    > You need to do the redirection at Kernel level with the Win32 API.
    >
    > > Also, I have read on the net some people mentioning about consoles being different for managed (in this case, my C# application) and unmanaged (inthis case, where the dll might be using win32 functions to write to stderror) codes.

    >
    > Exactly.
    >
    > > If this is indeed the issue here, can it be overcome?

    >
    > You need to replace the I/O handles of your own process. But be careful.
    > One process may hold more than one application apartment. Think of an
    > application pool. So you may not hit the nail on the head.
    >
    > I would recommend to start the unmanaged code in a separate process and
    > pass redirected I/O handles before creation. The separate process could
    > still be a managed one using marshalling to the unmanaged DLL API. But
    > it is a separate process which has replaced I/O handles from the first
    > time. The concept is similar to plugin containers of modern browsers. Of
    > course, the drawback is that you have to pass all data through IPC and
    > maybe security issues. But yo have to concern with the latter anyway
    > with unmanaged DLLs.
    >
    >
    > Marcel


    Thanks Mr. Müller, very well explained.
     
    , Apr 18, 2012
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Dodds

    child process with redirected stdout and stderr

    Jonathan Dodds, Jan 19, 2004, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    299
    Jonathan Dodds
    Jan 19, 2004
  2. Guest

    reroute stderr/stdout from a monitored process

    Guest, Mar 26, 2004, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    548
    Guest
    Mar 26, 2004
  3. Jacek
    Replies:
    2
    Views:
    704
    Jacek
    Jul 20, 2004
  4. Guest

    How to get stdout/stderr from unmanaged DLL?

    Guest, Jun 14, 2005, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    849
    Guest
    Jun 15, 2005
  5. Replies:
    0
    Views:
    1,019
Loading...

Share This Page