2

I am trying to learn how to write a simple .net profiler using profiling API.

For the first step, I just want to be able to load the profiler dll and to write to a log file from ICorProfilerCallback::Initialize.

In a test .net console app I set followning environment variables:

COR_ENABLE_PROFILING="1"

COR_PROFILER="ProfilerTest" --> This is where I think the problem is. I don't know how to find the GUID and ProfilerTest is the name od my dll. Here it says that starting with the .NET Framework 4, profilers do not have to be registered. Does this mean that this environment variable doesn't need to be set?

In the CLRProfiler source code from microsoft, they also set COR_PROFILER_PATH.

Just to be complete here is the initialize function from dll:

HRESULT STDMETHODCALLTYPE Profiler::Initialize(IUnknown* pICorProfilerInfoUnk)
{
    // A macro that writes to a log file.
    LOG(INFO);
    auto queryInterfaceResult = pICorProfilerInfoUnk->QueryInterface(__uuidof(ICorProfilerInfo), reinterpret_cast<void **>(&this->corProfilerInfo));

    if (FAILED(queryInterfaceResult))
        return E_FAIL;

    DWORD eventMask = COR_PRF_ALL;

    auto hr = this->corProfilerInfo->SetEventMask(eventMask);

    if (hr != S_OK)
        printf("ERROR: Profiler SetEventMask failed (HRESULT: %d)", hr);

    printf("ERROR: Profiler SetEventMask failed (HRESULT: %d)", hr);
    LOG(INFO);

    return S_OK;
}
pr12015
  • 67
  • 7
  • The guid you put in `COR_PROFILER` environment variable is the CLSID for the profiler com object. Setting `COR_PROFILER_PATH` means you don't need to register the CLSID in the registry, but you still need to provide the CLSID so that the runtime can extract the com object from the specified dll. I suggest checking the event log. The .NET Framework logs the process of loading the profiler with the '.NET Runtime' source. – Brian Reichle May 04 '19 at 13:18
  • Thanks for the reply. Makes sense. In the meantime I realized I didn't implement most of what I needed, but this is first time I'm dealing with COM. Do you happen to know what can cause this error: There was an unhandled exception while trying to instantiate the profiler COM object. Please ensure the CLSID is associated with a valid profiler designed to work with this version of the runtime. Profiler CLSID: '{4C20F663-CF29-4F26-9FF5-2C28BB884511}'. I used guidgen.exe to generate the GUID. – pr12015 May 05 '19 at 18:13
  • @BrianReichle just to bring to attention – pr12015 May 05 '19 at 18:40
  • Try adding a break point to your `DllGetClassObject` and `IClassFactory::CreateInstance` implementations and stepping through the code. – Brian Reichle May 06 '19 at 01:46
  • @BrianReichle I just tried that they all work, i get the access violation exception in clr.dll, don't really know how to go about that. Any ideas? Thanks. – pr12015 May 06 '19 at 10:30
  • Found the error. I was releasing where i shouldnt have. If you want, you can write your comments as an answer and Ill accept. Thanks for the help. – pr12015 May 06 '19 at 10:46

0 Answers0