printf and Console.WriteLine - problems with Console.SetOut?

Posted by Matt Jacobsen on Stack Overflow See other posts from Stack Overflow or by Matt Jacobsen
Published on 2010-04-15T14:59:23Z Indexed on 2010/04/19 23:13 UTC
Read the original article Hit count: 361

Filed under:
|
|

i have a bunch of Console.WriteLines in my code that I can observe at runtime. I communicate with a native library that I also wrote.

I'd like to stick some printf's in the native library and observe them too. I don't see them at runtime however.

I've created a convoluted hello world app to demonstrate my problem. When the app runs, I can debug into the native library and see that the hello world is called. The output never lands in the textwriter though. Note that if the same code is run as a console app then everything works fine.

C#:

    [DllImport("native.dll")]
    static extern void Test();

    StreamWriter writer;

    public Form1()
    {
        InitializeComponent();

        writer = new StreamWriter(@"c:\output.txt");
        writer.AutoFlush = true;
        System.Console.SetOut(writer);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Test();
    }

and the native part:

__declspec(dllexport) void Test()
{
    printf("Hello World");
}

Despite my earlier ramblings (see edits) I actually think this is a problem in C# (or rather my understanding of it).

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#