views:

76

answers:

2

In my code I need to set a process to connect it to a certain profiler. I understand that this is what I would have to do:

ProcessStartInfo processStartInfo = new ProcessStartInfo(exePath);
processStartInfo.EnvironmentVariables["Cor_Enable_Profiling"] = "0x1";
processStartInfo.EnvironmentVariables["COR_PROFILER"] = "{B146457E-9AED-4624-B1E5-968D274416EC}";
processStartInfo.UseShellExecute = false;

The issue now is that I don't know, for the profiler of my choice, how to set processStartInfo.EnvironmentVariables["COR_PROFILER"], is there any place that I can find out the mapping between the profiler application name and the profiler GUID?

A: 

The COR_PROFILER environment variable has to be set to the GUID of the coclass that implements the ICorProfilerCallback2 interface.

This would be set by you in the IDL if you implemented a custom profiler.

This is the way to reference the profiler, no need to know the actual path, but for this behavior to be possible, the COM dll with the profiler must be registered.

kek444
A: 

I got it figure out.

Here's how you can do it:

    public class RunProfiler
    {
     public void RunProfiler()
     {
         ProcessStartInfo processStartInfo = new ProcessStartInfo(exePath);
         processStartInfo.EnvironmentVariables["Cor_Enable_Profiling"] = "0x1";
         processStartInfo.EnvironmentVariables["COR_PROFILER"] = RegistryCode. GetRegistry();
         processStartInfo.UseShellExecute = false;
      }
    }
    public static class RegistryCode
    {

        public static string GetRegistry()
        {
            RegistryKey objectMe = Registry.ClassesRoot.OpenSubKey("CLSID", false);
            string[] valueName = objectMe.GetSubKeyNames();
            for (int i = 0; i < valueName.Length; i++)
            {
                var registryValue= objectMe.OpenSubKey(valueName[i], false).OpenSubKey("InprocServer32", false);
                if(registryValue!=null)
                {
                    var valueName1 = (string)registryValue.GetValue("");
                    if (valueName1!=null&&valueName1.Contains(@"C:\Program Files\Typemock\Isolator\")) // or any other path that your profiler dll is located on 
                        return valueName[i];

                }
            }
            return null;
        }


    }
Ngu Soon Hui