title: Why cannot I cast my COM object to the interface it implements in C#?
body: <p>I have this interface in the dll (this code is shown in Visual Studio from metadata):</p>
  
  <pre><code>#region Assembly XCapture.dll, v2.0.50727
  // d:\svn\dashboard\trunk\Source\MockDiagnosticsServer\lib\XCapture.dll
  #endregion
  
  using System;
  using System.Runtime.InteropServices;
  
  namespace XCapture
  {
      [TypeLibType(4160)]
      [Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
      public interface IDiagnostics
      {
          [DispId(1)]
          void GetStatusInfo(int index, ref object data);
      }
  }
  </code></pre>
  
  <p>So I created a COM server with such class:</p>
  
  <pre><code>[ComVisible(true)]
  [Guid(SimpleDiagnosticsMock.CLSID)]
  [ComDefaultInterface(typeof(IDiagnostics))]
  [ClassInterface(ClassInterfaceType.None)]
  public class SimpleDiagnosticsMock : ReferenceCountedObject, IDiagnostics
  {
      public const string CLSID = "281C897B-A81F-4C61-8472-79B61B99A6BC";
  
      // These routines perform the additional COM registration needed by 
      // the service. ---- stripped from example
  
      void IDiagnostics.GetStatusInfo(int index, ref object data)
      {
          Log.Info("GetStatusInfo called with index={0}, data={1}", index, data);
  
          data = index.ToString();
      }
  }
  </code></pre>
  
  <p>Server seems to work fine, and I am able to use the object from a VBScript. But then I try to use it from another C# client:</p>
  
  <pre><code>    [STAThread]
      static void Main(string[] args)
      {
          Guid mockClsId = new Guid("281C897B-A81F-4C61-8472-79B61B99A6BC");
          Type mockType = Type.GetTypeFromCLSID(mockClsId, true);
          IDiagnostics mock = (IDiagnostics)Activator.CreateInstance(mockType);
  
          //var diag = mock as IDiagnostics;
  
          object s = null;
          mock.GetStatusInfo(3, ref s);
  
          Console.WriteLine(s);
          Console.ReadKey();
      }
  </code></pre>
  
  <p>And it fails with </p>
  
  <blockquote>
    <p>Unable to cast COM object of type 'System.__ComObject' to interface
    type 'XCapture.IDiagnostics'. This operation failed because the
    QueryInterface call on the COM component for the interface with IID
    '{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}' failed due to the following
    error: No such interface supported (Exception from HRESULT: 0x80004002
    (E_NOINTERFACE)).</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>I have also tried to use InvokeMember, and that kinda worked except that I wasn't able to get the ref-returned <strong>data</strong> parameter.</p>
  
  <p><strong>EDIT:</strong> added STAThread attribute to my Main procedure. This does not solve the issue, but you <em>really should</em> use STAThread with COM unless you're absolutely sure you don't need it. See Hans Passant's answer below.</p>