COM Trace Listener

Dennis Tabako 36 Reputation points
2021-06-01T20:18:39.557+00:00

Is it possible to write a custom .NET trace listener that can be exposed as part of a COM server and used by a native C++ COM client? Such that the trace listener can be registered in the C++ app and Trace output will be directed to the .NET trace listener? I currently have a legacy C# logging library and a legacy C++ app and I'm trying to use them together if possible.

Thanks,
Dennis

Developer technologies | C++
Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Dennis Tabako 36 Reputation points
    2021-06-03T15:32:28.85+00:00

    OK, so after some further digging it looks like I'm probably not approaching this the right way. If I am getting this right now, the TRACE in C++ is simply a Visual Studio macro used for debugging. It looks like it does not allow one to direct the output elsewhere (and the output doesn't even print out anywhere for Release builds).

    If I am getting all of that correct, it looks like my best bet here is probably to identify the most important trace points in the C++ side and provide a mechanism for passing the info to the C# code for trace logging.

    Unless someone corrects me, I will mark this as the answer.

    2 people found this answer helpful.

  2. RLWA32 49,551 Reputation points
    2021-06-01T23:15:31.23+00:00

    If you expose your C# logging through an interface in a COM object hosted by a COM server written in managed code (i.e., C#) an unmanaged C++ COM client should be able to instantiate the object, obtain an interface pointer and call its methods.

    Another way to make the managed C# logging accessible is to use a DLL written in C++/CLI to provide the interoperability between unmanaged C++ and a managed C# class library.

    1 person found this answer helpful.
    0 comments No comments

  3. Dennis Tabako 36 Reputation points
    2021-06-02T12:55:26.707+00:00

    Hi, thanks for your answer! Yes, I have some experience doing that and in fact I have been able to wire it up so that I can call the C# COM server from the C++ client. My question is more specifically about the trace listener part. It doesn't seem to work (i.e. I use the C# code to hook up the trace listener but it doesn't catch trace statements from the C++ side). I'm wondering if I'm trying to hook up two incompatible mechanisms - meaning this isn't something I can make work and should try something else - or if it should be workable if I just figure out the right way to do it.

    Thanks,
    Dennis

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.