title: COM Registration free (SxS) and proxy stub for STA->MTA
body: <p>I'm trying to use my C++ COM API (loaded inside a C++ application) from C# (as an addin) without any registration.
  <a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms973913(v=msdn.10)?redirectedfrom=MSDN" rel="nofollow noreferrer">I follow steps from this tutorials</a></p>
  <p>So what I did is:</p>
  <ol>
  <li>to create an activation context from my C++ application at startup. I'm passing my COM API manifest file to CreateActCtx and I enable it by ActivateActCtx</li>
  <li>My C# addin is loaded by my C++ application (inside a custom AppDomain)</li>
  <li>Inside my C# addin I'm trying 3 differents scenario.</li>
  </ol>
  <ul>
  <li><p>The first one is to create COM object inside the application main thread --&gt; This is working</p>
  </li>
  <li><p>The second is to create COM object and casting COM object inside a separated STA thread --&gt; This is working</p>
  </li>
  <li><p>The last case is to create COM object and casting COM object inside a separated <strong>MTA</strong> thread. And here I have a problem. I'm able to create an object but I'm not able to cast it.
  I received and exception like:</p>
  <pre><code>Unable to cast COM object of type 'System.__ComObject' to interface type 'API.MyComObjectX'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{DE35BA2A-0566-40D3-AF1D-AD79D1133B09}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))
  </code></pre>
  </li>
  </ul>
  <p>So bellow, the code is the one I used for tested</p>
  <pre><code>/*
  Manifest file loaded by my C++ application
  
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
  &lt;assembly xmlns=&quot;urn:schemas-microsoft-com:asm.v1&quot; manifestVersion=&quot;1.0&quot;&gt;
  
    &lt;assemblyIdentity type=&quot;win32&quot; name=&quot;ApiX.X&quot; version=&quot;1.0.0.0&quot;/&gt;
    
    &lt;file name=&quot;ApiX.dll&quot;&gt;
      &lt;comClass clsid=&quot;{76125B7A-E5FC-4362-93F8-3B912EB85D51}&quot; threadingModel=&quot;Apartment&quot;/&gt; &lt;!-- This define the MyComObjectX object --&gt;
      &lt;typelib tlbid=&quot;{4714D4C0-5AA9-47F7-82FF-458B341E7052}&quot; version=&quot;1.0&quot; helpdir=&quot;&quot;/&gt;
    &lt;/file&gt;
  
    &lt;comInterfaceExternalProxyStub name=&quot;IMyComObjectX&quot; iid=&quot;{DE35BA2A-0566-40D3-AF1D-AD79D1133B09}&quot; proxyStubClsid32=&quot;{00020424-0000-0000-C000-000000000046}&quot; tlbid=&quot;{4714D4C0-5AA9-47F7-82FF-458B341E7052}&quot; baseInterface=&quot;{00000000-0000-0000-C000-000000000046}&quot; /&gt; &lt;!-- interface which is based on IDispatch --&gt;
  
  &lt;/assembly&gt;
  
  */
  
  public void testComFreeRegistration(object myComObjectX)
  {
      try
      {
          MyComObjectX myComObjectX2 = new MyComObjectX();
  
          MyComObjectX myComObjectX3 = (MyComObjectX)myComObjectX; // Throw the exception
  
          MessageBox.Show(&quot;working&quot;);
      }
      catch (Exception e)
      {
          MessageBox.Show(&quot;fail&quot;);
          e.ToString();
      }
  }
  
  
  public void EntryPoint(object myComObjectX) // &lt;-- Sent from my C++ application
  {
  
      // The STA test which are working
      // var sta = new Thread(() =&gt; testComFreeRegistration(myComObjectX));
      // sta.SetApartmentState(ApartmentState.STA);
      // sta.Start();
      // sta.Join();
  
      var mta = new Thread(() =&gt; testComFreeRegistration(myComObjectX));
      mta.SetApartmentState(ApartmentState.MTA);
      mta.Start();
      mta.Join();
  }
  </code></pre>
  <p>Another strange behavior is if I uncomment my STA test. Both test will work !!
  So I don't know what happen. It seems I have something not proerly initialized.</p>
  <p>Do you have any idea of what happen or what I should check ?
  Why the STA &lt;--&gt; MTA mechanism seems to not load the <code>comInterfaceExternalProxyStub</code> configuration ?</p>
  <p>Thanks in advance for your help ;)</p>