Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text not getting to the console

Tags:

c#

stdout

I have a c# project of type "console" it starts into it's own app domain from within a parent app, and it spawns a console window. However any text written to the console doesn't show up. Console.ReadKey() does work however. Any ideas what could be wrong ?

Note: Echo's to the parent process are showing in the parent process' embedded console (which isn't a win32 console but a directx GUI widget of some sort) Console.Out and System.Console.Out are not a "null" stream.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using ParentApi;

namespace LSTest4
{   
    class Program
    {

        static void Main(string[] args)
        {
            Console.OpenStandardOutput();
            ParentApi.Echo(System.Console.Out.ToString());
            Console.Out.WriteLine("monkeys");
            Console.WriteLine("app start");
            Console.Write("lalala");
            Console.ReadKey();
            ParentApi.Echo("app start");

    }
}

edit:

Upon further investigation:

The code in question is injected into another application, the injection is hostile and is managed by the "ParentApi", and it seems injection target is remapping the standard streams. In this case the best thing to do is to have the tracing handled in some other way. I might use WCF to push out the tracing output to an external process.

like image 392
Hassan Syed Avatar asked Feb 01 '13 16:02

Hassan Syed


1 Answers

I think your issue is going to be something about the parent app; you mention that the parent doesn't display the real Win32 Console, which is where I imagine the child Console.WriteLine calls are going.
I did a little POC (happy to share the code), which showed that a parent can redirect Console output using Console.SetOut, but a different AppDomain call to Console.WriteLine is still going to the main console instead of the new stream.

Further, the underlying Win32 API states that a process can be associated with only one console: http://msdn.microsoft.com/en-us/library/ms681944%28VS.85).aspx

If you really need a child Console, you might have to spin off a new process and use interprocess communication.

Edit
Disproving myself slightly, changing my parent app from Console to Windows (so that it's still runnable) meant that my child AppDomain could print to the Console it defined, and my parent could still write to the redirected stream. Although, this would still only allow one child; as above.
Maybe if you could provide the code on how you spawned your AppDomain it would help?

like image 64
Ian Yates Avatar answered Sep 30 '22 19:09

Ian Yates