For some custom requirement, we want to capture the each loaded class details in .Net. To do so, we have implemented .Net profiler in C++ using CorProfiler APIs. Here, we are subscribing to COR_PRF_MONITOR_CLASS_LOADS events and implemented ClassLoadFinished callback. Everything works fine. We are able to find the class name along with corresponding assembly and modules. So far so good.
Addition to this, we store some contextual information (like user id) in the .Net. We want to capture such context information as well. For example, under whose context a class got loaded?
The problem is contextual information is available in managed C# while class load event is available in unmanaged C++ (through cor profiler callback). I thought of the following options to get the contextual information in the C++:
-
Store contextual information in the CallContext.LogicalSetData in C#. Create a separate managed C++ DLL using /clr, called as a ManagedCpp. Read the contextual information using System::Runtime::Remoting::Messaging::CallContext::LogicalGetData("UserID"). Add reference of the ManagedCpp project into the unmanaged C++ profiler project. And finally, call above method in ManagedCPP from the ClassLoadFinished callback to get the contextual information.
-
Call C# from C++ using COM
But as per the Microsoft guidelines, .Net profiler should be a pure unmanaged C++ library. There should not be COM communication or managed code reference.
So, how do I get the C# contextual information in C++?
Aucun commentaire:
Enregistrer un commentaire