Redirect cout from C++ dll to a textbox in C# -



Redirect cout from C++ dll to a textbox in C# -

i'm trying display console output (cout) of method in dll textbox in c# program. every time phone call method, console output displayed in output pane of visual studio. there way redirect content of output pane textbox?

the dll written else in c++ , have no command in changing it. dll wrapped using swig can called c# code.

after next link suggested david, decided write solution problem more specific case. version allows receive stdout in gui via backgroundworker propertychangedeventhandler phone call back.

here's code consoleredirector:

public class consoleredirector : idisposable { private static consoleredirector _instance; public static void attach(progresschangedeventhandler handler, bool forceconsoleredirection) { debug.assert(null == _instance); _instance = new consoleredirector(handler, forceconsoleredirection); } public static void detatch() { _instance.dispose(); _instance = null; } public static bool isattached { { homecoming null != _instance; } } private static void resetconsoleoutstream() { //force console recreate output stream next time write/writeline called typeof(console).getfield("_out", system.reflection.bindingflags.static | system.reflection.bindingflags.nonpublic).setvalue(null, null); } private const int period = 500; private const int buffer_size = 4096; private volatile bool _isdisposed; private backgroundworker _worker; private readonly intptr _stdout; private readonly mutex _sync; private readonly system.threading.timer _timer; private readonly char[] _buffer; private readonly anonymouspipeserverstream _outserver; private readonly textreader _outclient; private readonly bool _forceconsoleredirection; private streamwriter _consolestandardout; private consoleredirector(progresschangedeventhandler handler, bool forceconsoleredirection) { bool ret; _forceconsoleredirection = forceconsoleredirection; if (!_forceconsoleredirection) { //make sure console._out initialized before redirect stdout, redirection won't impact textwriter temp = console.out; } anonymouspipeclientstream client; _worker = new backgroundworker(); _worker.progresschanged += handler; _worker.dowork += _worker_dowork; _worker.workerreportsprogress = true; _stdout = getstdhandle(std_output_handle); _sync = new mutex(); _buffer = new char[buffer_size]; _outserver = new anonymouspipeserverstream(pipedirection.out); client = new anonymouspipeclientstream(pipedirection.in, _outserver.clientsafepipehandle); debug.assert(_outserver.isconnected); _outclient = new streamreader(client, encoding.default); ret = setstdhandle(std_output_handle, _outserver.safepipehandle.dangerousgethandle()); debug.assert(ret); if (_forceconsoleredirection) { resetconsoleoutstream(); //calls console.write/writeline made against redirected stream } _worker.runworkerasync(_outclient); _timer = new system.threading.timer(flush, null, period, period); } void _worker_dowork(object sender, doworkeventargs e) { backgroundworker worker = (backgroundworker)sender; textreader client = (textreader)e.argument; seek { while (true) { int read = client.read(_buffer, 0, buffer_size); if (read > 0) worker.reportprogress(0, new string(_buffer, 0, read)); } } grab (objectdisposedexception) { // pipe closed... terminate } grab (exception ex) { } } private void flush(object state) { _outserver.flush(); } public void dispose() { dispose(true); } ~consoleredirector() { dispose(false); } private void dispose(bool disposing) { if (!_isdisposed) { lock (_sync) { if (!_isdisposed) { _isdisposed = true; _timer.change(timeout.infinite, timeout.infinite); _timer.dispose(); flush(null); seek { setstdhandle(std_output_handle, _stdout);} grab (exception) { } _outclient.dispose(); _outserver.dispose(); if (_forceconsoleredirection) { resetconsoleoutstream(); //calls console.write/writeline redirected original stdout stream } } } } } private const int std_output_handle = -11; [dllimport("kernel32.dll")] [return: marshalas(unmanagedtype.bool)] private static extern bool setstdhandle(int nstdhandle, intptr hhandle); [dllimport("kernel32.dll")] private static extern intptr getstdhandle(int nstdhandle); }

here's link finish sample form demonstrates how utilize it: consoleredirector illustration form

c# c++ dll console

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -