Console output in a window / text box

R

Roosty

Hello,

i've been dealing with this problem whole morning, but havent quite
figured out the solution. The thing is: i have a C# program that calls
a function from a C++ dll. While running, the function uses "cout" a
lot, to print out useful data. Now here is the problem: this data is
being written in an output window and i want to redirect this data to
an application window or a text box. If thats not possible, then the
data should be at least written to a console window, that would come
up, when this function is called.

Any help would be greatly appreciated, thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Roosty,

You can create a class that derives from TextWriter, overriding the
Write method (that takes a single character) and then having that fire an
event every time a character is written. Then, you can just call SetOut on
the Console class, passing an instance of your TextWriter.

You would then hook to an event on your text writer, which is fired
every time the character is written.

Overriding the Write method that takes a character is the minimum you
would have to do. However, this would result in a good number of events
being fired.

You could override some of the more specific methods, if you wish, for
the sake of performance.

This should capture the results of writing to cout from your unmanaged
DLL, and then you can display them anyway you wish.

Hope this helps.
 
R

Roosty

Hello Nicholas, thanks for replying so quickly. Well, im quite new to
C# , so would it be too much trouble for you to write the code of
exactly what you mean. I have seen quite a few similar questions, but
none of the answers offered the exact solution code.
 

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