Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to capture "printfs" from unmanaged C++

90 views
Skip to first unread message

bar...@yahoo.com

unread,
Mar 7, 2008, 12:43:54 PM3/7/08
to
In my managed C# app we capture trace statements and direct them to a
log file using trace listeners. Part of the ap puses 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.

Thanks

Mitch

Jeroen Mostert

unread,
Mar 7, 2008, 4:03:42 PM3/7/08
to
If the C++ DLL can be modified, you might consider making it a C++/CLI DLL
(in other words, a managed DLL itself) by compiling it with /clr. This will
make it trivial for the DLL to use TraceListeners itself, while still
keeping the ability to use unmanaged code.

If making it a CLR DLL is not an option, you could set a logging facility of
your choosing to be used instead of printf() calls (which is not good design
for a library in any case). The simplest way to do this is to pass a
delegate to C++ to be called whenever logging is necessary. This can be a
little tricky, since passing delegates to be used as function pointers
requires that you be careful about garbage collection. The alternative is to
expose a COM object that will handle logging, and invoke that from C++. This
can be simpler or harder than the delegate solution, depending on how good
you are with COM.

--
J.


bar...@yahoo.com

unread,
Mar 7, 2008, 4:18:17 PM3/7/08
to

>
> If the C++ DLL can be modified, you might consider making it a C++/CLI DLL
> (in other words, a managed DLL itself) by compiling it with /clr. This will
> make it trivial for the DLL to use TraceListeners itself, while still
> keeping the ability to use unmanaged code.
>

Are you saying that if I make it a managed dll, then the output of the
printfs would get picked up by the existing trace listeners in the C#
code? Or would I need to change the printfs to Trace.WriteLine(...)
statements?

Thanks

Mitch

Jeroen Mostert

unread,
Mar 7, 2008, 8:13:35 PM3/7/08
to
bar...@yahoo.com wrote:
>> If the C++ DLL can be modified, you might consider making it a C++/CLI DLL
>> (in other words, a managed DLL itself) by compiling it with /clr. This will
>> make it trivial for the DLL to use TraceListeners itself, while still
>> keeping the ability to use unmanaged code.
>>
>
> Are you saying that if I make it a managed dll, then the output of the
> printfs would get picked up by the existing trace listeners in the C#
> code?

No. Unmanaged code (including printf() calls) would continue to work the same.

> Or would I need to change the printfs to Trace.WriteLine(...) statements?
>

Yes.

--
J.

0 new messages