How to capture "printfs" from unmanaged C++

  • Thread starter Thread starter barker7
  • Start date Start date
B

barker7

I posted this to the interop group. I'm reposting to troll for a few
more ideas.

In my managed C# app we capture trace statements and direct them to a
log file using trace listeners. Part of the app uses unmanaged C++
dlls, and unfortunately, the C++ printfs don't get picked up and are
simply printed to the console window.

Is there any way I can capture the output of the printfs and then
redirect them to the same file that the trace listener is writing to?

I do have access to the unmanaged C++ code if the solution involves
not using printfs. The important thing is that the C++ printfs and the
C# trace messages end up in the same file.

One solution from the interop group was to compile the C++ with /clr
and then replace the printfs with Trace.WriteLine(). Any less invasive
techniques. Maybe somehow redirecting the printfs to someplace that
can be picked up by .Net.

Thanks

Mitch
 
[...]
Is there any way I can capture the output of the printfs and then
redirect them to the same file that the trace listener is writing to?

I do have access to the unmanaged C++ code if the solution involves
not using printfs. The important thing is that the C++ printfs and the
C# trace messages end up in the same file.

It's been some time now, but my recollection is that the standard CRT has
a provision for overriding the default stdout stream. You could do that
in a single place in the C++ code, and then in that overridding output
redirect the output back up to the managed log file via the usual managed
mechanisms.

I apologize for not having the details as to how to redirect stdout from
within the process but this is, after all, a newsgroup for managed code.
:) I assume that if you posted the same question to a forum specifically
about using C++ and the standard CRT, they'd be able to tell you more
details. Or you might be able to find it in the C++ reference docs on
MSDN.

Pete
 
Back
Top