title: What governs the version of the .NET CLR that gets loaded by CorBindToRuntimeEx?
body: <p>I'm using the following unmanaged C++ code to instantiate the CLR from an Excel 2003 add-in (a COM shim for a .NET add-in):</p>
  
  <pre><code>    hr = CorBindToRuntimeEx(
          0, // version, use default
          0, // flavor, use default
          0, // domain-neutral"ness" and gc settings 
          CLSID_CorRuntimeHost, 
          IID_ICorRuntimeHost, 
          (PVOID*) &amp;m_pHost);
  </code></pre>
  
  <p>and for the vast majority of the machines in our organisation (a few hundred) this works perfectly, even those with multiple CLR versions installed; however for a few machines a wrong (older) version of the CLR is instantiated which then fails to load the assembly as it requires the .NET 2 runtime. </p>
  
  <p>Yesterday for the first time I ran Process Explorer and this was quite revealing showing the following on one of the problem machines:</p>
  
  <pre><code>process     pid   type   Handle or DLL
  -------     ---   ----   -------------
  procexp.exe 5056  DLL    c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorworks.dll
  EXCEL.EXE   7180  DLL    c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorworks.dll
  </code></pre>
  
  <p>i.e. Excel has loaded the wrong version of the runtime even though a newer one is availble. Now I need to find out why.</p>
  
  <p>A few possibilities that come to mind:</p>
  
  <ol>
  <li>There is something odd with the 'priority' of CLR instantiation on the specific machine, even though the MS docs (http://msdn.microsoft.com/en-us/library/ms231419.aspx) appear to indicate that you'll always get the newest unless you request a specific version.</li>
  <li>Another add-in in Excel has already (deliberately) instantiated a .NET 1 CLR and Excel can't host more than one. </li>
  </ol>
  
  <p>I strongly suspect the second of these but don't know how to prove / fix it.</p>
  
  <p>Has anyone seen similar behaviour? Any suggestions on what is going on?</p>
  
  <p>A few other notes:</p>
  
  <ul>
  <li>All workstations are running Windows XP SP3</li>
  <li>Excel 2003 SP3 is the only version of Excel in our organisation</li>
  </ul>
  
  <p>I can't change either of these so a newer Excel version is not an option.</p>