> 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++
[quoted text clipped - 7 lines]
> not using printfs. The important thing is that the C++ printfs and the
> C# trace messages end up in the same file.
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.

Signature
J.
barker7@yahoo.com - 07 Mar 2008 21:18 GMT
> 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 - 08 Mar 2008 01:13 GMT
>> 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
[quoted text clipped - 4 lines]
> 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.

Signature
J.